【发布时间】:2021-08-12 08:09:28
【问题描述】:
希望是一个基本问题,我为联系人创建了一个“创建”页面,该页面创建了系统中所有客户的列表。我使用 ViewModel 来执行此操作,这很好。但是现在在客户模型上,我已将 CustomerName 字段指定为必填项,这导致 ModelState.IsValid 为假,我无法弄清楚原因。
客户
public class Customer
{
public int CustomerId { get; set; }
[Required]
[Display(Name = "Customer Name")]
public string? CustomerName { get; set; }
public ICollection<Contact> Contacts { get; set; }
public ICollection<Job> Jobs { get; set; }
}
联系方式
public class Contact
{
public int ContactId { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Email Address")]
public string EmailAddress { get; set; }
public string Telephone { get; set; }
public Customer Customer { get; set; }
}
视图模型
public class ContactDetailViewModel
{
public Contact Contact { get; set; }
public Customer Customer { get; set; }
public List<SelectListItem> Customers { get; set; }
}
表格
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Contact.FirstName" class="control-label"></label>
<input asp-for="Contact.FirstName" class="form-control" />
<span asp-validation-for="Contact.FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.LastName" class="control-label"></label>
<input asp-for="Contact.LastName" class="form-control" />
<span asp-validation-for="Contact.LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.EmailAddress" class="control-label"></label>
<input asp-for="Contact.EmailAddress" class="form-control" />
<span asp-validation-for="Contact.EmailAddress" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.Telephone" class="control-label"></label>
<input asp-for="Contact.Telephone" class="form-control" />
<span asp-validation-for="Contact.Telephone" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Customer.CustomerName" class="control-label"></label>
<select class="form-control dropdown" asp-for="Customer.CustomerId" asp-items="@Model.Customers">
<option></option>
</select>
<span asp-validation-for="Customer.CustomerId" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
控制器
public async Task<IActionResult> Create([Bind("ContactId,FirstName,LastName,EmailAddress,Telephone")] Contact contact, ContactDetailViewModel contactDetailViewModel)
{
contact.Customer = await _context.Customers.FirstOrDefaultAsync(c => c.CustomerId == contactDetailViewModel.Customer.CustomerId);
//Has to be a better way of doing this??? Why is it faling without... 06/03/2021
//ModelState.Remove("Customer.CustomerName");
if (ModelState.IsValid)
{
_context.Add(contact);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(contact);
}
如果我不包括 ModelState.Remove("Customer.CustomerName");然后它不起作用。但是我不想对 CustomerName 做任何事情,因为我只想将 Contact.Customer 更新为“新”选定的客户。
谢谢!
【问题讨论】:
-
你的
Customer.CustomerName根本没有绑定,你绑定的是Customer.CustomerId:)。所以客户端验证甚至通过了。CustomerName的验证错误也适用于CustomerId。
标签: c# .net-core asp.net-core-mvc