MVC2:在包含部分视图的页面表单提交之前验证部分视图
我正在使用 asp.net mvc2 并有一个基本页面,其中包含表单中的部分视图
<% using (Html.BeginForm())
{ %>
<% Html.RenderAction("partialViewActionName", "Controllername"); %>
<input type="submit" value="Weiter" />
<% } %>
当我提交表单时,我的页面的 httpPost 操作被调用,并且在 httpPost 操作之后 我的部分视图被称为
[HttpPost]
public virtual ActionResult PagePostMethod(myModel model)
{
// here I should know about the validation of my partial View
// If partialView.ModelState is valid then
// return View("success");
// else return View(model)
}
[HttpPost]
public virtual ActionResult partialViewActionName(myModel model)
{
ModelState.AddModelError("Error");
return View(model);
}
但是当我在部分视图的 httpPost 方法中进行验证时(因为我想在几个地方使用我的部分视图),我无法决定我的洞页面是否有效。
有谁知道我该如何做到这一点?在一个页面中有多个部分视图不是很常见的任务吗 但是页面操作方法中有有关验证的信息吗?
非常感谢您的帮助!
I am using asp.net mvc2 and having a basic Page that includes a Partial View within a form
<% using (Html.BeginForm())
{ %>
<% Html.RenderAction("partialViewActionName", "Controllername"); %>
<input type="submit" value="Weiter" />
<% } %>
When I submit the form, the httpPost Action of my Page is called, and AFTER that the httpPost Action
of my Partial View is called
[HttpPost]
public virtual ActionResult PagePostMethod(myModel model)
{
// here I should know about the validation of my partial View
// If partialView.ModelState is valid then
// return View("success");
// else return View(model)
}
[HttpPost]
public virtual ActionResult partialViewActionName(myModel model)
{
ModelState.AddModelError("Error");
return View(model);
}
But as I am doing the Validation in the httpPost Method of my Partial View (because I want to use my Partial View in several Places) I cant decide if my hole page is valid or not.
Has anyone an Idea how I could do this? Isn´t it a common task to have several partial Views in a page
but have the information about validation in the page action methods?
Thanks very much for your help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的分部视图方法将在呈现页面时调用,而不是在页面发布时调用。发布完成后将调用的唯一操作是 BeginForm 中指定的操作。验证将在 ModelBinder 中进行——假设您正在为模型使用 DataAnnotations。模型中由部分视图中的输入表示的任何部分都将在绑定期间与模型的其余部分一起进行验证。如果您手动处理验证,那么您将需要在帖子上调用的操作中验证模型的所有部分。
Your partial view method will be called when the page is rendered, not when the page is posted. The only action that will be called when the post is done is the action specified in BeginForm. The validation will happen in the ModelBinder -- assuming you're using DataAnnotations for your model. Any part of your model that is represented by inputs in the partial view will be validated along with the rest the model during binding. If you are handling the validation manually, then you will need to validate all of the parts of the model in the action that is being invoked on the post.