2017-06-20 71 views
0

我的工作是爲學校管理系統建立一個網站。我有可以登錄系統的學生和管理員。學生可以註冊。我使用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); 
    } 
+0

嘗試在您的Account帳戶模型中將數據屬性'[Key]'放在'AccountID'屬性的上方。如果您使用Code First或Database First,也會有所幫助。 –

+0

另一個說明,不確定你的開發有多遠,但ASP.NET MVC 5擁有Identity,並且它是從用戶帳戶和身份驗證開始的好方法。對於您和您而言,是否有很多東西不必擔心所有認證的細微差別,如醃製和散列密碼。 –

+0

@David Lee Database First –

回答

1

戶頭型號

嘗試添加[Key]屬性並確保你的數據庫設置爲自動遞增領域。

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

控制器

你也可以從你的[Bind(Include = "")]刪除AccountID,因爲這不是由職位設置和服務器上同時管理。這不應該導致錯誤,但是會是一個很好的做法。

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

SQL服務器

在SQL Server上的AccountID應設置爲Yes身份規範與(標識)應該是肯定的。身份增量通常是1,身份種子通常是1,但這些不應該導致您的插入任何問題。

+0

它幫助感謝!但是,我發現所有模型的另一個問題在Model.edmx中做了一些更改後被覆蓋...有什麼辦法可以防止它? –

+0

不客氣,我張貼在關於模型覆蓋問題的評論中。不知道我能否在那裏幫助你。 –