视图中的表单一直告诉我,尽管我输入了名称,但该字段仍然是必需的。我不能自己解决这个问题!可能有什么不对?它要求的字段是FirstName。
这是Controller中的Action方法:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateNewAccount([Bind(Include = "ID,FirtsName,LastName,BillingAddress,BillingPostalCode,BillingCity,DeliveryAddress,DeliveryPostalCode,DeliveryCity,EmailAddress,PhoneNumber")] Customers customers)
{
if (ModelState.IsValid)
{
// Add users entity GUID
//customers.EntityUserInfo = User.Identity.GetUserId();
db.Customers.Add(customers);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(customers);
}
这是模特:
public class Customers
{
public int ID { get; set; }
[Required]
[Display(Name = "Förnamn")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Efternamn")]
public string LastName { get; set; }
[Required]
[Display(Name = "Adress - faktura")]
public string BillingAddress { get; set; }
[Display(Name = "Postnummer - faktura")]
public string BillingPostalCode { get; set; }
[Display(Name = "Stad - faktura")]
public string BillingCity { get; set; }
[Display(Name = "Adress - försändelse")]
public string DeliveryAddress { get; set; }
[Display(Name = "Postnummer - försändelse")]
public string DeliveryPostalCode { get; set; }
[Display(Name = "Stad - försändelse")]
public string DeliveryCity { get; set; }
[Display(Name = "E-Post")]
public string EmailAddress { get; set; }
[Display(Name = "Telefonnummer")]
public string PhoneNumber { get; set; }
public virtual ICollection<Orders> Orders { get; set; }
}
最后是视图:
@model WebShop_2.Models.Customers
@{
ViewBag.Title = "CreateNewAccount";
}
<h2>CreateNewAccount</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Customers</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<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.BillingAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BillingAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BillingAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.BillingPostalCode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BillingPostalCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BillingPostalCode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.BillingCity, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BillingCity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BillingCity, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DeliveryAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DeliveryAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DeliveryAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DeliveryPostalCode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DeliveryPostalCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DeliveryPostalCode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DeliveryCity, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DeliveryCity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DeliveryCity, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmailAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EmailAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PhoneNumber, "", 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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
答案 0 :(得分:1)
你拼错了'FirstName',检查你的模特:
[Required]
[Display(Name = "Förnamn")]
public string FirstName { get; set; }
使用[Bind]
进行的动作调用:
[Bind(Include = "ID,FirtsName,
将其更改为以下内容应该解决它:
[Bind(Include = "ID,FirstName,
虽然正如其他人所指出的,你应该使用ViewModel并完全摆脱[Bind]
。 Example here