当我向Customer/Edit/id
页进行请求时,出现以下错误
System.FormatException:'输入字符串的格式不正确。'
我被困在这里,你能帮我吗?
模型
public class CustomerModel
{
[Required(ErrorMessage = "{0} alanı boş geçilemez!")]
[DisplayName("Doğum Tarihi")]
[DisplayFormat(DataFormatString = "{0dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DateOfBirth { get; set; }
}
查看
<div class="form-group row">
@Html.LabelFor(model => model.DateOfBirth, htmlAttributes: new {@class = "col-md-2 col-form-label" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control datepicker" } })
@Html.ValidationMessageFor(model => model.DateOfBirth, "", new { @class = "text-danger" })
</div>
</div>
控制器
public ActionResult Edit(int id)
{
var customer = _customerService.GetCustomerById(id);
if (customer == null || customer.Deleted)
return RedirectToAction("List");
var model = new CustomerModel()
{
DateOfBirth = customer.DateOfBirth,
};
return View(model);
}
[HttpPost]
public ActionResult Edit(CustomerModel model)
{
if (ModelState.IsValid)
{
var customer = _customerService.GetCustomerById(model.Id);
if (customer == null || customer.Deleted)
return RedirectToAction("List");
customer.DateOfBirth = model.DateOfBirth;
_customerService.UpdateCustomer(customer);
}
return RedirectToAction("List");
}
答案 0 :(得分:0)
我想您将id作为整数传递,在任何情况下ID都将为空或字符串,请检查。 我希望这会有所帮助。