Razor Viewer - How to Add Partial Views

I was wondering if, if possible, the best way to make partial use of the new razor viewing mechanism. I understand that this is something that was not completely completed by the time

I am currently using RenderPage to render a custom control:

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

The page calling the RenderPage uses a layout page (master) with three defined sections: TitleContent, HeadContent, and Maincontent. When I try to display my control on this page, it seems that these sections are also necessary - they should be necessary only on the calling page and be present. I get the following message, regardless of whether I include sections in my partial view (obviously, I don't want to include these sections, but that seemed like an interesting debugging point ...).

The following sections were but were not 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> 
+79
c # asp.net-mvc razor
Aug 1 '10 at 6:00
source share
2 answers

You partially look the same as the editor template, so you can enable it as such (assuming that your partial part is located in a subfolder of ~/views/controllername/EditorTemplates ):

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

Or if it is not:

 @Html.Partial("nameOfPartial", Model) 
+115
Aug 01 '10 at 7:05
source share

If you don’t want to duplicate the code, and like me, you just want to show statistics, in your view model you can just pass the models that you want to get data for, for example:

 public class GameViewModel { public virtual Ship Ship { get; set; } public virtual GamePlayer GamePlayer { get; set; } } 

Then in your controller, just run your queries on the appropriate models, pass them to the view model and return it, for example:

 GameViewModel PlayerStats = new GameViewModel(); GamePlayer currentPlayer = (from c in db.GamePlayer [more queries]).FirstOrDefault(); 

[code to verify the results]

 //pass current player into custom view model PlayerStats.GamePlayer = currentPlayer; 

As I said, you really need to do this only if you want to display statistics from the corresponding tables, and there is no other part of the CRUD process, for security reasons that other people talked about above.

0
Jun 16 '14 at 21:20
source share



All Articles