开发者

ASP.NET MVC3 Partial View on HTTPPost

I Have a View and Partial View. The layout for the view is something like this:

<html>
...
<div id="MainView">@RenderBody()</div>

<!--Partial View-->
<div id="partialView">@Html.Action("PartialViewForm", "Main")</div>
...
</html>

My Partial View (named as _Register) is something like this:

@model PartialViewModel    
<div id="form">
@using (Html.BeginForm("PartialViewForm", "Main", FormMethod.Post))
{
    @Html.ValidationSummary(true)
    <table  >
        <tbody>
            <tr>
                <td align="left">@Html.LabelFor(model => model.Name)*</td>
                <td>@Html.EditorFor(model => model.Name)</td>
                <td align="left">@Html.ValidationMessageFor(model => model.Name, "")</td>
            </tr>            
            <tr>
                <td align="left"><input type="submit" value="Go" class="submit2"/></td>
            </tr>

        </tbody>
    </table>
}
</div>

In my MainController I have methods like this:

public class MainController  : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpGet]
    public ActionResult PartialViewForm()
    {
        var partialViewModel= new PartialViewModel();   
        return PartialView("_Register", partialViewModel);
    }

    [HttpPost]
    public ActionResult PartialViewForm(PartialViewModel partialViewModel )
    {
        // if Validation is not successfull 
        return PartialView("_Register", partialViewModel);

       //  else
       ....
    }
 }

This is what I want to do... when validation fails on the partial view I want to g back to the main view... however in my case on the post action when validation fails all I can see is the partialview... there is no main page content. There are posts on the forum that show the same kind of behavior but I am not able to solve my issue. Can anyone please tell me how to fix it (it will be really helpful if you can modify my example and show开发者_JAVA百科 it)

Thanks


I'm not sure if I totally understand what you are trying to do but if what I'm thinking is right, you should just use

[HttpPost]
public ActionResult PartialViewForm(PartialViewModel partialViewModel )
{
    // if Validation is not successfull
    model = _db.getBlah(); //get the original model for the main view
    return View("MainView", model);

   //  else
   ....
}

However I think your issue might be that you really should have your form submission in your main view and not in your partial - the partial is just there to render the editors for your Create/Edit views, etc; the data should be submitted to the main view's action so that it can create/update the proper model.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜