开发者

MVC keeping the PartialView in its own context - ignore the main view holding the partial view

I'm looking at the partialview components of the MVC Framework.

i want my partial view to be handled in its own action and for the rest of the view to handle itself, but i'm getting an exception because the main page is not getting its view fired.

Am i going around this the wrong way?

My Main View (Jobs/Index.aspx):

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication3.Models.JobViewModel>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.RenderPartial("JobListing", Model.Jobs); %> 
</asp:Content>

The partialview (Jobs/JobListing.ascx):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<MvcApplication3.Models.Job>>" %>

<table>
<tr>
    <td> Job Title  </td>
    <td>   Job Location</td>
</tr>
<%
    foreach (var job in Model)
    {
%>
<tr>
    <td>
        <%= job.Title %>
    </td>
    <td>
        <%= job.Location %>
    </td>
</tr>
<%  
    } 
%>

<% Html.BeginForm("DoSomeStuff", "Job", null, FormMethod.Post); %>

   <%= Html.TextBox("SomeInfo") %>

    <button type="submit" id="submit" />
<% Html.EndForm(); %>

The main controller for both the main view (Index) and the partialview (DoSomeStuff())

    public class JobController : Controller
{
    public ActionResult Index()
    {
        JobProvider provider = new JobProvider(Session);

        JobViewModel vm = new JobViewModel();

        vm.Jobs = provider.GetJobs();

        return View(vm);           
    }

    public PartialViewResult DoSomeStuff()
    {
        return PartialView("JobListing");
    }
}

As you can see in the partial view, it has its own form that posts to the Action called DoSomeStuff(). i want this action to handle any data submitted from that form. but when the form is submitted the main action (Index) does not fire and then i get an exception as the Model (.Models.JobViewModel) is not passed to the view that the partialview (JobListings) lives in.

basically what im saying is, if i have a myview.aspx with lots of html.RenderPartialView('apartialview') that have forms in them, can i get it so these forms post to their own actions and the main view (with what ever model it inhe开发者_如何学运维rits) is handled as well. Rather then having all the form submitting code in the main action for the view.

am i do this wrong?


I would follow POST-REDIRECT-GET pattern and would define DoSomeStuff like:

public class JobController : Controller
{
    [HttpPost]
    public RedirectToRouteResult DoSomeStuff(DoSomeStuffModel model)
    {
        //Do some stuff with model
        return RedirectToAction("Index");
    }
} 

If you don't want to reload whole form, you can use jQuery Form Plugin. If you use it, you can stay with PartialViewResult. After posting partial view form, if you specify target option, content of div containing partial view code will be replaced with returned html.


Is the DoSomeStuff() action being called?

If so it is not surprising it isn't working. RenderPartial is to call a partial page directly without having a controller action. But perhaps because one exists it is calling that method. However, that method doesn't receive Jobs and is also not passing a Model back to the view, so it makes sense that it can't get the view model.

Really, you should be using RenderPartial if you have no need for an action to execute. If you do, you should use Html.RenderAction or Html.Action

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜