gpt4 book ai didi

asp.net-mvc - Asp.net MVC ModelState.Isvalid 为 Id 返回 false

转载 作者:行者123 更新时间:2023-12-04 11:27:38 24 4
gpt4 key购买 nike

我在看这个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组成 CustomerMembershipType楷模。
    public class CustomerFormViewModel {
public IEnumerable<MembershipType> MembershipTypes { get; set; }
public Customer Customer { get; set; }
}
我有一个创建 new Customer 的 View 与 Name , DateOfBirth , MembershipTypeIsSubscribedToNewsLetter领域。它需要 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 Controller :
    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()方法总是错误的;结果第一个 if statementSave方法正确。所以我不能存储任何新客户。
我试过 debug通过放置 breakpoint 申请在 if (!ModelState.IsValid)并看到 Id字段正在创建错误(说“Id 字段是必需的”)。为什么说 Id不需要时需要吗?是否 ModelState.IsValid方法在数据库级别检查模型?我不这么认为。
如果我更改 Customer模型 Id像这样的属性: public int? Id { get; set; }并通过此更改 if 语句, if ((!ModelState.IsValid) && (customer.Id==null) )该应用程序运行良好。
是否有其他解决方案 Id问题?

最佳答案

我看了同样的类(class),我猜作者在你看过之后更新了,因为他展示了这种确切类型的问题。问题是,当将 View 模型返回到新操作的 View 时,客户属性未初始化,因此 Id 为空,因此在尝试保存时 ModelState 失败

只需更改如下,以便在设置 viewModel 时,您初始化 Customer,然后 Id 为 0:

 public ActionResult New()
{
var memberShipTypes = _context.MembershipTypes.ToList();

var viewModel = new CustomerViewModel
{
Customer = new Customer(),
MembershipTypes = memberShipTypes
};

return View("CustomerForm", viewModel);
}

关于asp.net-mvc - Asp.net MVC ModelState.Isvalid 为 Id 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46108887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com