我的工作是爲學校管理系統建立一個網站。我有可以登錄系統的學生和管理員。學生可以註冊。我使用Entity Framework的模板爲ASP.NET MVC 5創建了控制器。它已創建帳戶模型,控制器和創建,刪除,詳細信息,編輯,索引視圖。帳戶的字段有:ASP.NET MVC 5,INSERT在實體框架中不起作用
public int AccountID { get; set; }
public Nullable<int> TypeID { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ICNumber { get; set; }
public string Telephone { get; set; }
public string email { get; set; }
public string Adress { get; set; }`
我已經改變了我的看法,以隱藏不需要顯示的信息,ID,密碼等。(我的代碼=>)
@{
ViewBag.Title = "Register";
}
@model WebApplication9.Account
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Account</h4>
<hr />
<div class="form-group">
@Html.LabelFor(model => model.TypeID, "TypeID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("TypeID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TypeID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ICNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ICNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ICNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Telephone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Telephone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Adress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Adress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Adress, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
帳戶ID應該是在數據庫autogenereted,我也用身份作爲帳戶型號StoreGenerated模式,但即時得到錯誤,而試圖registrate新的學生:
的SQLException:無法將NULL值插入列「的AccountID」
控制器代碼:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register([Bind(Include = "AccountID,TypeID,Password,FirstName,LastName,ICNumber,Telephone,email,Adress")] Account account)
{
if (ModelState.IsValid)
{
db.Accounts.Add(account);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.TypeID = new SelectList(db.AccountTypes, "TypeID", "TypeName", account.TypeID);
return View(account);
}
嘗試在您的Account帳戶模型中將數據屬性'[Key]'放在'AccountID'屬性的上方。如果您使用Code First或Database First,也會有所幫助。 –
另一個說明,不確定你的開發有多遠,但ASP.NET MVC 5擁有Identity,並且它是從用戶帳戶和身份驗證開始的好方法。對於您和您而言,是否有很多東西不必擔心所有認證的細微差別,如醃製和散列密碼。 –
@David Lee Database First –