MVC keeping the PartialView in its own context - ignore the main view holding the partial view
- by Mike
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 inherits) is handled as well. Rather then having all the form submitting code in the main action for the view.
am i do this wrong?