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);
}
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. –
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. –
@David Lee Database First –