views:

152

answers:

1

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!!

+1  A: 

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.

tvanfosson
Thanks for the quick answer. I have debugged it, and the action calls are:- Page Get Method - PartialView Get Methodthen I press submit- Page Post Method - PartialView Post MethodI am doing a return View() in the page post method. But that should call the GET PartialView Action, not the POST Partial View Method, or? Isn´t it a good idea to put the RenderAction within a submit?
Pascal
@Pascal - The second invocation of the partial view action comes as the page is being rendered as a result of the post action. Because it comes from a POST event, it will call the version of the method decorated with the post method attribute. By this time, though, the post action is complete. Normally, you'd not have separate get/post versions of an action you'd render in your view. It would typically be a ChildActionOnly -- i.e., only called to render a snippet of HTML and not actually callable via a request. All of your validation must be done before the view is rendered on post
tvanfosson
ok I see. But then I have no idea how i could figure that. So what I want to do: I have a Settings panel in the Admin Area. And I want the same Settings Panel in a page of a registration process. So I have to different pages, and I want to show the same partial view. But I have different submits on the page. The registration one shall go to the next view, and the settings page shell go to the index page. I tried do make the submit inside the partial view. But then doesn´t work, as child action cant do redirects. When I have the submit on my pages, how can I first validate the partial view?
Pascal
@Pascal - You can have a form inside the partial view as long as the partial itself isn't already contained in a form in the including page. i.e., no nested forms allowed. I would probably just restructure the page so that the partial is outside the including view's form and have the partial post back to a different action via its own form.
tvanfosson
yes. that is what i tried. the pages just as container, without a form. and the partial has the form, gets the post and makes the validation. the problem is now, that when I Do the submit and the post is done, I have to redirekt to different actions (in the registration to the next step, in the settings page to the index). But redirection is not allowed in nested (child) actions. Do you have an Idea to handle that? THANKS very much for your help!!
Pascal
@Pascal -- don't post back to the same action that renders the partial view, post back to a different action that can do the redirect.
tvanfosson
oh well, of course!! To many trees in my forest!! Thanks very much, you saved my day!
Pascal