私の仕事は、学校管理システム用のウェブサイトを作ることです。システムにログインできる学生と管理者がいます。学生は登録することができます。 Entity Framework用のテンプレートを使用してASP.NET MVC 5のコントローラを作成しました。アカウントモデル、コントローラ、作成、削除、詳細、編集、インデックスビューを作成しました。アカウントのフィールドは次のとおりです。私は等のID、パスワードとして、示さに必要ない情報を隠すために、私の見解を変更したASP.NET MVC 5、INSERTがEntity Frameworkで動作しない
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; }`
(私のコードは=>)
@{
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でなければなりません
のSQLException:データベースにautogenereted、私はまた、アカウントのモデルが、イムでStoreGeneratedパターン新しい学生をregistrateしようとしているときにエラーを取得としてのアイデンティティを使用している列にNULL値を挿入することはできません「アカウントID」
コントローラーコード:
[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'モデルの' AccountID'プロパティの上にデータ属性 '[Key]'を置いてみてください。また、コードファーストまたはデータベースファーストを使用しているかどうかを知るのに役立ちます。 –
もう1つの注意点として、開発の進捗状況はわかりませんが、ASP.NET MVC 5にはIDがあり、ユーザーアカウントと認証で始めることができます。あなたとあなたのための多くのものは、パスワードの塩漬けやハッシングのような認証のニュアンスを心配する必要はありません。 –
@David Lee Database First –