以下是我的控制器的代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, Actor actor)
{
try
{
actorRepository.Save(actor);
return RedirectToAction("Index");
}
catch
{
return View("Edit");
}
}
调用具有强类型为Actor类的局部视图的视图。出于某种原因,有一些字段没有绑定到类。有问题的字段是Address,Address2和ZipCode。它们填充在页面上,但它们返回null。其他所有领域都在那里,而不是这些。
有什么想法吗?另外,我怎么能写单元测试来复制这种行为?
更新 公共类演员 { public string MiddleName {get;设置;}
[Required(ErrorMessage = "First Name is Required")]
[RegularExpression(@"\w*", ErrorMessage = "Last Name Contains Illegal Characters")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name is Required")]
[RegularExpression(@"\w*", ErrorMessage = "Last Name Contains Illegal Characters")]
public string LastName { get; set; }
[DataType(DataType.PhoneNumber, ErrorMessage = "Please Enter a Valid Phone Number")]
[Required(ErrorMessage = "Phone Number is Required")]
public string PhoneNumber { get; set; }
[Required(ErrorMessage = "Address is Required")]
[RegularExpression(@"\w*", ErrorMessage = "Address Contains Illegal Characters")]
public string Address { get; set; }
[RegularExpression(@"\w*", ErrorMessage = "Address2 Contains Illegal Characters")]
public string Address2 { get; set; }
[RegularExpression(@"\w*",ErrorMessage = "State Contains Illegal Characters")]
[Required(ErrorMessage = "State is Required")]
public string State { get; set; }
[Required(ErrorMessage = "Zip Code is Required")]
[RegularExpression("\b[0-9]{5}(?:-[0-9]{4})?\b",ErrorMessage = "Please Enter a Valid Zip Code")]
public string ZipCode { get; set; }
[Required(ErrorMessage = "Even in theater, you have to choose a gender")]
public bool? Gender { get; set; }
[Required(ErrorMessage = "Cell Phone Number is Required")]
public string CellPhone { get; set; }
public int ActorId { get; set; }
[DataType(DataType.MultilineText, ErrorMessage = "Illegal Characters in Notes")]
public string Notes { get; set; }
[Required(ErrorMessage = "Email Address is Required")]
[DataType(DataType.EmailAddress)]
public string EMail { get; set; }
[Required(ErrorMessage = "City Is Required")]
public string City {get; set;}
}
<fieldset>
<legend>Fields</legend>
<p>
<label for="MiddleName">MiddleName:</label>
<%= Html.EditorFor(m=>m.MiddleName) %>
<%= Html.ValidationMessage("MiddleName", "*") %>
</p>
<p>
<label for="FirstName">FirstName:</label>
<%=Html.EditorFor(m=>m.FirstName) %>
<%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
<label for="LastName">LastName:</label>
<%= Html.TextBox("LastName", Model.LastName) %>
<%= Html.ValidationMessage("LastName", "*") %>
</p>
<p>
<label for="PhoneNumber">PhoneNumber:</label>
<%= Html.TextBox("PhoneNumber", Model.PhoneNumber) %>
<%= Html.ValidationMessage("PhoneNumber", "*") %>
</p>
<p>
<label for="Address">Address:</label>
<%=Html.EditorFor(m=>m.Address) %>
<%= Html.ValidationMessage("Address", "*") %>
</p>
<p>
<label for="Address2">Address2:</label>
<%=Html.EditorFor(m=>m.Address2) %>
<%= Html.ValidationMessage("Address2", "*") %>
</p>
<p>
<label for="State">State:</label>
<%= Html.TextBox("State", Model.State) %>
<%= Html.ValidationMessage("State", "*") %>
</p>
<p>
<label for="ZipCode">ZipCode:</label>
<%= Html.TextBox("ZipCode", Model.ZipCode) %>
<%= Html.ValidationMessage("ZipCode", "*") %>
</p>
<p>
<label for="Gender">Gender:</label>
<%= Html.TextBox("Gender", Model.Gender) %>
<%= Html.ValidationMessage("Gender", "*") %>
</p>
<p>
<label for="CellPhone">CellPhone:</label>
<%= Html.TextBox("CellPhone", Model.CellPhone) %>
<%= Html.ValidationMessage("CellPhone", "*") %>
</p>
<p>
<label for="ActorId">ActorId:</label>
<%= Html.TextBox("ActorId", Model.ActorId) %>
<%= Html.ValidationMessage("ActorId", "*") %>
</p>
<p>
<label for="Notes">Notes:</label>
<%= Html.TextBox("Notes", Model.Notes) %>
<%= Html.ValidationMessage("Notes", "*") %>
</p>
<p>
<label for="EMail">EMail:</label>
<%= Html.TextBox("EMail", Model.EMail) %>
<%= Html.ValidationMessage("EMail", "*") %>
</p>
<p>
<label for="City">City:</label>
<%= Html.TextBox("City", Model.City) %>
<%= Html.ValidationMessage("City", "*") %>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
答案 0 :(得分:2)
有一个名为ConvertEmptyStringsToNull的ModelMetaData属性(默认值= true),它在ASP.MVC的DefaultModelBinder类中将空字符串转换为NULL。
您可以通过每个属性上的DataAnnotation更改此内容,如下所示:
[DisplayFormat(ConvertEmptyStringToNull=false)]
或者您可以创建自己的ModelBinder并从DefaultModelBinder继承并只是覆盖GetPropertyValue方法以忽略此设置:
ModelBinders.Binders.DefaultBinder = new Delphi.Mvc.KeepStringsBinder(); ModelBinders.Binders.Add(typeof(ObjectBase),new Delphi.Mvc.KeepStringsBinder());
答案 1 :(得分:0)
以下是测试模型粘合剂的方法:
How to test custom Model Binders in ASP.NET MVC?
只有你会测试DefaultModelBinder。
对于字段,您尚未显示Actor源代码。但是,既然你谈到字段,我猜你真的使用字段 - 但是ASP.NET MVC模型绑定器不会绑定到字段 - 只能绑定到属性。所以,如果你有
public class Actor
{
public string ZipCode;
}
您需要更改为
public class Actor
{
public string ZipCode { get; set; }
}
另一个原因可能是你有
public class Actor
{
public Address Address { get; set; }
}
并使用partial来显示地址;所以它被命名为“ZipCode”,但必须命名为“Address.ZipCode”。或者也许你的地址没有无参数构造函数......换句话说,如果没有看到源代码那就太难以猜测了。
使用更新信息,这是无意义的。坏字段看起来和好字段一样。但是尝试使用Html.TextBox而不是EditorFor。尝试使用FireBug(或IE Dev)查看POST数据,检查字段名称和值是否正确。如果他们这样做 - 问题在于模型绑定器,如果他们是坏/不在 - 页面有问题。另外,尝试检查控制器操作中的ModelState.IsValid以及ModelState错误。