我在摆弄NerdDinner教程晚餐编辑控件。
我得到了一个FormCollection作为参数之一,我可以在使用UpdateModel()之前修剪其中的数据吗?
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
Dinner dinner = dinnerRepository.GetDinner(id);
try
{
UpdateModel(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.DinnerID });
}
catch
{
foreach (var issue in dinner.GetRuleViolations())
{
ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
return View(dinner);
}
}或者我必须通过遍历Request.Form键来手动完成此操作?
发布于 2009-06-02 15:02:46
您可以使用模型绑定器来绑定到自定义对象,而不是使用原始表单值。
您可以通过实现IModelBinder接口来创建自己的模型绑定器。在IModelBinder.BindModel方法中,您可以裁剪或执行所需的任何其他字符串操作。
完成此操作后,您的操作将以您希望的方式接收格式化的数据。
有关更多信息,K Scott Allen和Scott Hanselman有一些关于IModelBinder的文章。
https://stackoverflow.com/questions/939890
复制相似问题