Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor view engine - How can I add Partial Views

I was wondering what, if it is possible, is the best way to render a partial using the new razor view engine. I understand this is something that wasn't finished completely by the time

Right now I am using RenderPage to render the user control:

@RenderPage("~/Views/Shared/LocaleUserControl.cshtml",ViewData.Model) 

The page calling RenderPage uses a layout (master) page with three sections defined: TitleContent, HeadContent and Maincontent. When I attempt to render my locale control from this page it appears that these sections are also required - they should only be required in the calling page and are present. I receive the following message, regardless of whether or not I include the sections in my partial view (obviously I dont want to include these sections but it seemed like an interesting debugging point...).

The following sections have been defined but have not been rendered on the layout page '~/Views/Shared/LocaleUserControl.cshtml': TitleContent; HeadContent; MainContent

My partial view is as follows (adapted from the following link):

@inherits System.Web.Mvc.WebViewPage<LocaleBaseModel> @using System.Web.UI;  <p>      @Html.LabelFor(model => Model.CountryName)     <br />     @Html.DropDownListFor(model => Model.CountryName,null, string.Empty, new { @class = "text", accesskey="u"}) </p> <p>      @Html.LabelFor(model => Model.StateProvince)     <br />      @Html.DropDownListFor(model => Model.StateProvince, null, string.Empty, new { @class = "text", accesskey="t" }) </p>   <script type="text/javascript">     $(function () {         var countries = $("#CountryName");         var statesprovinces = $("#StateProvince");         countries.change(function () {             statesprovinces.find('option').remove();             var url = '@Url.Action("GetStatesProvinces", "Base")';             $.getJSON(url, { countryId: countries.val() }, function (data) {                 $(data).each(function () {                     $("<option value=" + this.ID + ">" + this.Name + "</option>").appendTo(statesprovinces);                 });             });         });     }); </script> 
like image 745
JP. Avatar asked Aug 01 '10 06:08

JP.


People also ask

How do you add a partial view in Razor page?

Add partial view in razor page You can create a partial view under Pages folder or in any other sub folder. Here we have created a partial view called "_menu" under pages=>shared folder. In Asp.Net Core 2.1 there is new partial tag helper.

How do I add a partial view model?

To create a partial view, right click on Shared folder -> select Add -> click on View.. Note: If the partial view will be shared with multiple views, then create it in the Shared folder; otherwise you can create the partial view in the same folder where it is going to be used.

How do I render a partial view in Razor?

For this go to Solution Explorer then select Views -> Shared Folder -> Right-click -> Add View. Now for the View -> Home -> Index. cshtml. Here I am rendering a Partial View using 4 types, so the index.

How can I load partial view inside the view?

In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.


1 Answers

You partial looks much like an editor template so you could include it as such (assuming of course that your partial is placed in the ~/views/controllername/EditorTemplates subfolder):

@Html.EditorFor(model => model.SomePropertyOfTypeLocaleBaseModel) 

Or if this is not the case simply:

@Html.Partial("nameOfPartial", Model) 
like image 169
Darin Dimitrov Avatar answered Sep 22 '22 23:09

Darin Dimitrov