【发布时间】:2018-02-16 22:22:12
【问题描述】:
我正在看这个ASP.NET MVC course。我有一个 customer model 具有以下属性。
public class Customer {
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
[Display(Name = "Date of Birth")]
public DateTime? DateOfBirth { get; set; }
public bool IsSubscribedToNewsLetter { get; set; }
public MembershipType MembershipType { get; set; }
[Display(Name="Membership Type")]
public byte? MembershipTypeId { get; set; }
}
请注意,Id 没有 Required 数据注释。但在我的数据库中,Id 是主键,Identity 是该列的真值。
有一个ViewModel 由Customer 和MembershipType 模型组成。
public class CustomerFormViewModel {
public IEnumerable<MembershipType> MembershipTypes { get; set; }
public Customer Customer { get; set; }
}
我有一个使用Name、DateOfBirth、MembershipType 和IsSubscribedToNewsLetter 字段创建new Customer 的视图。它需要CustomerFormViewModel。
@using Vidly.Models
@model Vidly.ViewModel.CustomerFormViewModel
@{
ViewBag.Title = "New";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.Message</h2>
@using (Html.BeginForm("Save", "Customer")) {
<div class="form-group">
@Html.LabelFor(m => m.Customer.Name,new{@class="control-label"})
@Html.TextBoxFor(m => m.Customer.Name, new { @class = "form-control" })
@Html.ValidationMessageFor(m=>m.Customer.Name)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Customer.DateOfBirth, new { @class = "control-label"})
@Html.TextBoxFor(m => m.Customer.DateOfBirth,"{0:d MMM yyyy}", new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Customer.MembershipTypeId, new { @class = "control-label"})
@Html.DropDownListFor(m => m.Customer.MembershipTypeId,new SelectList(Model.MembershipTypes,"Id","Name"), "Select Membership Type", new { @class = "form-control" })
</div>
<div class="checkbox">
<label>
@Html.CheckBoxFor(m => m.Customer.IsSubscribedToNewsLetter) Subscribed To Newsletter?
</label>
</div>
@Html.HiddenFor(m=>m.Customer.Id)
<button type="submit" class="btn btn-primary">Save</button>
}
这是我的Save 控制器:
public ActionResult Save(Customer customer) {
if (!ModelState.IsValid) {
var viewModel = new CustomerFormViewModel {
Customer = customer,
MembershipTypes = _context.MembershipTypes.ToList()
};
return View("CustomerForm", viewModel);
}
if (customer.Id == 0 || customer.Id==null) {
_context.Customers.Add(customer);
}
else {
var CustomerInDb = _context.Customers.Single(c => c.Id == customer.Id);
CustomerInDb.Name = customer.Name;
CustomerInDb.DateOfBirth = customer.DateOfBirth;
CustomerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
CustomerInDb.MembershipTypeId = customer.MembershipTypeId;
}
_context.SaveChanges();
return RedirectToAction("Index", "Customer");
}
当我填充CustomerForm 视图并单击提交按钮时,ModelState.Isvalid() 方法总是返回 false;导致Save 方法的第一个if statement 为真。所以我不能存储任何新客户。
我尝试通过将breakpoint 放在if (!ModelState.IsValid) 上来debug 应用程序,并看到Id 字段正在创建错误(说“需要ID 字段”)。为什么说Id 不是必需的? ModelState.IsValid 方法是否在数据库级别检查模型?我不这么认为。
如果我像这样更改Customer 模型的Id 属性:public int? Id { get; set; } 并以此更改 if 语句,if ((!ModelState.IsValid) && (customer.Id==null) ) 应用程序可以正常工作。
这个Id问题还有其他解决方案吗?
【问题讨论】:
-
它是必需的。它的
int和int不能是null。但是视图模型不包含数据模型,尤其是用于编辑数据。仅包含视图中需要的数据模型的属性。参考What is ViewModel in MVC?
标签: asp.net-mvc model modelstate