没有JavaScript的部分视图验证

时间:2012-02-01 22:12:24

标签: asp.net-mvc-3 validation

我有一个部分视图,其中有一个表单。我使用PRG模式发布此表单。我正在使用AjaxHelper来创建我的表单。我还需要这个表单才能在没有javascript的情况下工作。问题是当模型验证失败时,它总是将url更改为我的局部视图。

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

以下是我的尝试:

return PartialView("_PostForm", postFormModel);

这只是渲染局部视图,不包含任何父级内容。

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

这实际上产生了正确的结果。它显示父视图,但URL是部分http://localhost:22485/Controller/PostForm的URL!我觉得这非常接近解决方案。现在怎么办?

1 个答案:

答案 0 :(得分:0)

如果要更改网址,则应重定向到其他操作(使用PRG模式)。插入下一个代码而不是'//我该怎么做?':

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

新动作应如下所示:

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

希望它有所帮助。

相关问题