1
我在页面上出现了一个包含我的模型验证消息的问题。ASP.NET MVC验证预填充时的问题模型
页面具有搜索窗体,模型是表示窗体中项目的对象。当用户访问该页面时,邮政编码会自动填充到控制器中,然后将模型传递到视图中。
这会导致邮政编码字段的验证邮件显示,即使邮政编码格式正确。
这里是在控制器中的代码:
public ActionResult WhereToBuyIndex(LocatorSearch searchOptions)
{
if (searchOptions == null)
{
searchOptions = new LocatorSearch();
}
//ATTEMPT TO GET USER ZIP CODE
AppLib.Geolocation.Web.WebGeolocationService geoService = new AppLib.Geolocation.Web.WebGeolocationService();
searchOptions.Address = geoService.GeolocateToZipCode(Request.ServerVariables["REMOTE_ADDR"]);
if (searchOptions.Address == "00000") { searchOptions.Address = "90210"; }
ViewBag.Countries = LocatorService.Instance.LoadCountriesWithCustomers();
return View("Index", searchOptions);
}
和在涉及zip字段的视图中仅线如下:
@Html.TextBoxFor(model => model.Address, new { id = "fAddress" })
<span class="failure">@Html.ValidationMessageFor(model => model.Address)</span>
如果我尝试预填任何其他字段,我得到相同的验证问题 - 所有的预填场错误消息都显示出来
任何帮助都非常感谢,
谢谢。
谢谢,这工作像一个魅力。我的一部分要求是,这个页面只使用一个get,以便它可以链接到,所以我不得不以丑陋的方式去做。 :( – jsmith