I am converting a Real Estate Application from ASP.net to MVC Razor and have hit an impass. Several of the pages use a menu down the left hand side showing the properties assigned to the agent that is logged in. When a selection is made, it brings up
the property on the right allowing them to do every thing from add/edit/delete to printing sale flyers. My thought is to make the left and right sides partial views, with each having its own data model. My problem is that I can't get the both of them to
render in the same view. Errors are thown when I do.
I am presuming that a new master layout is not required, but realize this could be wrong. Could someone please point me in the right direction.
and the model for the Menu, which is a collection retrieved frm the database with the Entity Framework.
public class PropertyMenuModel
{
public Int64 PropertyID { get; set; }
public string PictureUrl { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
}
I have been experimenting with different techniques and can get it to work so long as the models represent a single empty record. But I need to send different sets of data to each of these views and that is where it breaks. From my research, my current
view looks like this:
<table style="text-align: Left; vertical-align: Top" border="0" cellpadding="5">
<tr>
<td>@Html.Partial("_PropertyMenu", new PMP.Models.PropertyMenuItem())</td>
<td>
@Html.Partial("_Property", new PMP.Models.PropertyModel())
</td>
</tr>
</table>
Based upon everything I have read, not only is the technique above what I should be using, but I should also be able to pass an IEnumerable collection of properties to the menu. How do I get it to read from the database and then pass those records to the
partial view?
After beating my head for the last several days, the solution appears to be "Html.Action" because Html.Partial only works with static content. My page now looks like the code below.
ARTIS
Member
69 Points
94 Posts
Creating 2 partial views with different models
May 07, 2012 04:27 AM|LINK
I am converting a Real Estate Application from ASP.net to MVC Razor and have hit an impass. Several of the pages use a menu down the left hand side showing the properties assigned to the agent that is logged in. When a selection is made, it brings up the property on the right allowing them to do every thing from add/edit/delete to printing sale flyers. My thought is to make the left and right sides partial views, with each having its own data model. My problem is that I can't get the both of them to render in the same view. Errors are thown when I do.
I am presuming that a new master layout is not required, but realize this could be wrong. Could someone please point me in the right direction.
ignatandrei
All-Star
135047 Points
21654 Posts
Moderator
MVP
Re: Creating 2 partial views with different models
May 07, 2012 04:37 AM|LINK
What errors?
raduenuca
All-Star
24675 Points
4250 Posts
Re: Creating 2 partial views with different models
May 07, 2012 07:53 AM|LINK
In this book:
http://www.amazon.com/Pro-ASP-NET-MVC-3-Framework/dp/1430234040/ref=sr_1_1?s=books&ie=UTF8&qid=1336376878&sr=1-1
at chapter 8 there a part about navigation. You could extend that very easily.
Radu Enuca | Blog
ARTIS
Member
69 Points
94 Posts
Re: Creating 2 partial views with different models
May 07, 2012 03:26 PM|LINK
I am getting: Compiler Error Message: CS0119: 'PMP.Controllers.PropertyMenuController' is a 'type', which is not valid in the given context
My main page looks like:
<table style="text-align: Left; vertical-align: Top" border="0" cellpadding="5"> <tr> <td>@Html.Partial("_PropertyMenu", PMP.Controllers.PropertyMenuController)</td> <td> @Html.Partial("_Property") </td> </tr> </table>The Property Menu
@model IEnumerable<PMP.Models.PropertyMenuModel> <script language="javascript" type="text/javascript"> var menuColorSelected = '#99FF99'; var menuColorNormal = '#eeeeff'; var menuColorRejected = '#FFA07A'; var menuColorHighlight = '#FFDA79'; ****Java script functions **** </script> <div id="RoundedContainer" style="width: 300px; height: 650px; overflow: auto;"> @foreach (var item in Model) { <li id='@item.PropertyID' style="cursor: pointer; list-style-type: none;" onmouseout='menuMouseOut(this)' onmouseover= 'menuMouseOver(this)' onclick='LoadListing_Click(this, @item.PropertyID)'> <table style="background-color: transparent; text-align: center; vertical-align: middle" border="0" cellpadding="5"> <tr> <td style="width: 83px; text-align: center"> <table id="TablePhoto" runat="server" border="0" cellpadding="0"> <tr> <td> <img alt='' src='@item.PictureUrl' /> </td> </tr> </table> </td> <td style="text-align: left"> @item.StreetAddress <br /> @item.City <br /> @item.State </td> </tr> </table> </li> } <br /> </div>and the model for the Menu, which is a collection retrieved frm the database with the Entity Framework.
public class PropertyMenuModel { public Int64 PropertyID { get; set; } public string PictureUrl { get; set; } public string StreetAddress { get; set; } public string City { get; set; } public string State { get; set; } }ignatandrei
All-Star
135047 Points
21654 Posts
Moderator
MVP
Re: Creating 2 partial views with different models
May 07, 2012 03:33 PM|LINK
Why do you call here the PropertyMenuController ?!
ARTIS
Member
69 Points
94 Posts
Re: Creating 2 partial views with different models
May 07, 2012 04:19 PM|LINK
Isn't it part of the naming convention and framework that is reqired by MVC?
ignatandrei
All-Star
135047 Points
21654 Posts
Moderator
MVP
Re: Creating 2 partial views with different models
May 07, 2012 08:59 PM|LINK
No. Html.Partial expects a Model, not a controller definition.
ARTIS
Member
69 Points
94 Posts
Re: Creating 2 partial views with different models
May 09, 2012 04:13 PM|LINK
I have been experimenting with different techniques and can get it to work so long as the models represent a single empty record. But I need to send different sets of data to each of these views and that is where it breaks. From my research, my current view looks like this:
<table style="text-align: Left; vertical-align: Top" border="0" cellpadding="5"> <tr> <td>@Html.Partial("_PropertyMenu", new PMP.Models.PropertyMenuItem())</td> <td> @Html.Partial("_Property", new PMP.Models.PropertyModel()) </td> </tr> </table>Based upon everything I have read, not only is the technique above what I should be using, but I should also be able to pass an IEnumerable collection of properties to the menu. How do I get it to read from the database and then pass those records to the partial view?
ARTIS
Member
69 Points
94 Posts
Re: Creating 2 partial views with different models
May 14, 2012 12:18 AM|LINK
After beating my head for the last several days, the solution appears to be "Html.Action" because Html.Partial only works with static content. My page now looks like the code below.
<table style="text-align: Left; vertical-align: Top" border="0" cellpadding="5"> <tr> <td> @Html.Action("_PropertyMenu")</td> <td> @Html.Partial("_Property", new PMP.Models.PropertyModel()) </td> </tr> </table>