Validación de vista parcial sin JavaScript

I have a partial view in which there is a form. I POST this form using the PRG pattern. I am using the AjaxHelper to create my form. I also need this form to work without javascript. The problem is that when model validation fails, it always changes the url to my partial view.

public ActionResult PostForm(PostFormModel postFormModel)
{
    if (ModelState.IsValid)
    {
        return RedirectToAction("SomewhereElse");
    }
    else
    {
        if (Request.IsAjaxRequest())
        {
            return PartialView("_PostForm")
        }
        else
        {
            // What do I do here?
        }
    }
}

Esto es lo que he probado:

return PartialView("_PostForm", postFormModel);

This just renders the partial view and doesn't contain any of the parent stuff.

return View("Index", new ParentModel() { PostFormModel = postFormModel });

This actually produces the correct result. It displays the parent view, but the URL is that of the partial http://localhost:22485/Controller/PostForm! I feel like this is really close to the solution. What now?

preguntado el 01 de febrero de 12 a las 22:02

Why 'return PartialView("_PostForm", postFormModel);' goes after method body? Is it misprint? -

Yep, sorry. Edited formatting. -

1 Respuestas

If you want to change url, you should redirect to another action (using PRG pattern). Insert next code instead of '// What do I do here?':

postModelService.Save(postFormModel); //to Session or to DB
return RedirectToAction("Parent");

New action should look like this:

public ActionResult Parent()
{
    var postFormModel = postModelService.Load();
    return View("Index", new ParentModel() { PostFormModel = postFormModel });
}

Espero eso ayude.

Respondido 02 Feb 12, 05:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.