2017-06-20 20 views
0

私の仕事は、学校管理システム用のウェブサイトを作ることです。システムにログインできる学生と管理者がいます。学生は登録することができます。 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); 
    } 
+0

あなたの 'Account'モデルの' AccountID'プロパティの上にデータ属性 '[Key]'を置いてみてください。また、コードファーストまたはデータベースファーストを使用しているかどうかを知るのに役立ちます。 –

+0

もう1つの注意点として、開発の進捗状況はわかりませんが、ASP.NET MVC 5にはIDがあり、ユーザーアカウントと認証で始めることができます。あなたとあなたのための多くのものは、パスワードの塩漬けやハッシングのような認証のニュアンスを心配する必要はありません。 –

+0

@David Lee Database First –

答えて

1

Accoutモデル

[Key]属性を追加してみてください、あなたのデータベースが自動インクリメントフィールドとして設定されていることを確認します。

[Key] 
public int AccountID { get; set; } 

コントローラ

これはポストによって設定されていないと、サーバー上で管理されているとしてまた、あなたの[Bind(Include = "")]からAccountIDを削除することができます。これはエラーを引き起こしてはいけませんが、行うのがよい習慣です。あなたのSQL Server上

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Register([Bind(Include = "TypeID,Password,FirstName,LastName,ICNumber,Telephone,email,Adress")] Account account) 
{ 
    // stuff 
} 

SQL Serverの

AccountIDは、Identity仕様のために[はい]に設定する必要があり、(アイデンティティです)はいなければなりません。アイデンティティインクリメントは通常1で、アイデンティティシードは通常1ですが、それらは挿入に問題を引き起こしてはいけません。

+0

ありがとう!しかし、私がModel.edmxでいくつかの変更を加えた後、すべてのモデルが上書きされるというもう一つの問題...それを防ぐ方法はありますか? –

+0

大歓迎です、モデルの上書きに関するご質問にコメントを投稿しました。私があなたを助けることができるかどうかは分かりません。 –

関連する問題