2017-06-20 1 views
0

Meine Aufgabe ist es, eine Website für ein Schulverwaltungssystem zu machen. Ich habe Studenten und Admins, die sich in das System einloggen können. Studenten können sich anmelden. Ich habe Controller für ASP.NET MVC 5 mit Vorlage für Entity Framework erstellt. Es hat Account-Modell, Controller und Erstellen, Löschen, Details, Bearbeiten, Index Ansichten erstellt. Felder Konto:ASP.NET MVC 5, INSERT nicht in 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; }` 

ich meine Ansichten Informationen zu verbergen haben sich geändert, die entspricht nicht benötigen, wie ID, Passwort usw. (My code =>)

@{ 
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> 
} 

Konto-ID sein autogenereted in der Datenbank habe ich Identität als StoreGenerated Muster in Kontenmodell aber ich bin immer Fehler auch verwendet beim Versuch, neue Schüler anmelden:

SQLException: Kann den Wert NULL in Spalte ‚AccountID‘ nicht einfügen

Controller-Code:

[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

Versuchen Sie, das Datenattribut '[Key]' über Ihre 'AccountID'-Eigenschaft in Ihrem' Account'-Modell zu setzen. Auch wäre es hilfreich zu wissen, ob Sie Code First oder Database First verwenden. –

+0

Auf einem anderen Hinweis, nicht sicher, wie weit in Entwicklung Sie sind, aber ASP.NET MVC 5 hat Identität und es ist eine gute Möglichkeit, mit Benutzerkonten und Authentifizierung zu starten. Es gibt viele Dinge für Sie und Sie müssen sich nicht um alle Nuancen der Authentifizierung wie das Salzen und das Hashing von Passwörtern kümmern. –

+0

@David Lee Database First –

Antwort

1

Accout Modell

Versuchen Hinzufügen des [Key] Attribut und stellen Sie sicher, dass Ihre Datenbank-Setup als Auto erhöht Feld ist.

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

-Controller

Sie auch die AccountID von Ihrem [Bind(Include = "")] entfernen könnte, da dies durch die Post nicht gesetzt wird und auf dem Server verwaltet werden. Dies sollte nicht den Fehler verursachen, wäre aber eine gute Vorgehensweise.

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

SQL Server

auf dem SQL-Server die AccountID sollte auf Ja für Identity-Spezifikation festgelegt werden und (Ist Identity) sollte ja sein. Identity Increment ist in der Regel 1 und Identity Seed ist in der Regel 1, aber diese sollten keine Probleme mit Ihrem Einsatz verursachen.

+0

Es half, danke! Aber ein weiteres Problem, das ich fand, dass alle Modelle überschrieben werden, nachdem ich einige Änderungen in Model.edmx vorgenommen habe ... Gibt es eine Möglichkeit, es zu verhindern? –

+0

Gern geschehen, habe ich im Kommentar zu Ihrer Frage nach dem Modell überschrieben. Ich bin mir nicht sicher, ob ich dir da helfen kann. –

Verwandte Themen