我在Global.asax中有以下代码:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
//ValueProviderFactories.Factories.RemoveAt(0);
//I have tried replacing 0 above with 1 to 6.
//The following Factories are present in ValueProviderFactories.Factories:
//ChildActionValueProviderFactory
//FormValueProviderFactory
//JsonValueProviderFactory
//RouteDataValueProviderFactory
//QueryStringValueProviderFactory
//HttpFileCollectionValueProviderFactory
//JQueryFormValueProviderFactory
ValueProviderFactories.Factories.Clear();
}
}
我注意到清除所有ValueProviderFactory
会导致表单数据作为null
进入一种操作方法,但是当我尝试通过取消注释RemoveAt
一次删除一个表单时行,并注释掉ValueProviderFactories.Factories.Clear();
表单数据以非null
的形式通过。
然后ValueProviderFactory
负责获取表单数据?
这是视图:
@using MvcModels.Models
@model IList<AddressSummary>
@{
Layout = null;
}
<h2>Addresses</h2>
@using (Html.BeginForm())
{
<fieldset>
<legend>Address</legend>
<div><label>City:</label>@Html.Editor("[0].City")</div>
<div><label>Country:</label>@Html.Editor("[0].Country")</div>
</fieldset>
<button type="submit">Submit</button>
}
操作方法:
public ActionResult Address(IList<AddressSummary> addresses)
{
addresses = addresses ?? new List<AddressSummary>();
return View(addresses);
}
型号:
public class AddressSummary
{
[Required]
public string City { get; set; }
[Required]
public string Country { get; set; }
}