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

不客气,我张贴在关于模型覆盖问题的评论中。不知道我能否在那里帮助你。 –