ASP.NET MVC 3 Partial View Template

Honestly, I have no idea what to call it and how to start searching for it.

I have a display page with a standard layout.

<div> <label for="field">Field Name:</label> @Model.Field </div> 

When trying to make it more convenient to change, I want to create a template instead of entering each field with the above code.

I created a partial view with the following:

 @model System.Object <div> @Html.LabelFor(m => m) @Html.DisplayFor(m => m) </div> 

In my opinion, I added the following:

 @Html.Partial("_BillField", Model.Field) 

Then the model has a description with the following:

 public ModelName { [Display(Name="Field Description")] public decimal Field { get; set; } } 

This works when on the main view, but the label is missing when using the template. What am I missing?

Update: Per @K. Bob, I am making changes to the partial view:

 <div> @Html.LabelFor(m => m) @Html.DisplayFor(m => m) </div> 

Update 2: For clarity of what I want.

In the end, I want to be able to:

 @Html.Partial("_BillField", Model.Field1) @Html.Partial("_BillField", Model.Field2) @Html.Partial("_BillField", Model.Field3) 

And have the equivalent:

 <div> <label for="field1">Field 1 Name:</label> @Model.Field1 </div> <div> <label for="field2">Field 2 Name:</label> @Model.Field2 </div> <div> <label for="field3">Field 3 Name:</label> @Model.Field3 </div> 

Sorry for not understanding this.

+4
source share
5 answers

I think that potentially you should use Display / Editor templates for types, your model includes 3 fields of this type.

 @Html.Partial("_BillField", Model.Field1) @Html.Partial("_BillField", Model.Field2) @Html.Partial("_BillField", Model.Field3) 

Defining a template for a type rather than a partial representation is perhaps more efficient. See ASP.NET MVC 3 - Partial vs Display Template vs Editor Template for more details.

In this case, your view will be more similar:

 @Html.DisplayFor(model => model.Field1) @Html.DisplayFor(model => model.Field2) @Html.DisplayFor(model => model.Field3) 

Your model will be:

 public class model { [DisplayName("Field1")] public ComplexType Field1 {get;set;} [DisplayName("Field2")] public ComplexType Field2 {get;set;} [DisplayName("Field3")] public ComplexType Field3 {get;set;} } 

Or regardless of the data annotation for the display name.

+1
source

Partially, you do not need to know what @model is, it will use the parent @model if you select @model in partial, what will help?

If I have it as a species ....

 @model MyApp.Models.ModelName @{ ViewBag.Title = "Test"; } <h2>Test</h2> <div> <div class="editor-label"> @Html.LabelFor(m => m.Field) </div> <div class="editor-field"> @Html.TextBoxFor(m => m.Field) @Html.ValidationMessageFor(m => m.Field) </div> </div> @Html.Partial("_partial", Model) @*note I pass the whole model*@ 

And this is how partial ....

 @model MyApp.Models.ModelName <div> <div class="editor-label"> @Html.LabelFor(m => m.Field) </div> <div class="editor-field"> @Html.TextBoxFor(m => m.Field) @Html.ValidationMessageFor(m => m.Field) </div> </div> 

Then it does what I think you want to do (obv, it does it twice, but you can remove the code from the main view). I am not sure if this gives you a huge advantage. Maybe I didn’t understand something.

+2
source

You do not need ToString() your property.

 @Html.LabelFor(m => m) 

UPDATED

Based on what you want to do, reuse the views, take a look at this longer than the usual reuse of validation and partial views message I wrote for the ASP.NET MVC 3 answer - Model validation . This is extremely detailed.

+1
source

A new answer has been added since the other did not answer the question after clarification. This post LabelFor Extension should be customized according to your needs, I think, but instead of better off creating your own extension.

You will need to call it slightly different than what you suggested, because you need to use the m=>m.Field . But I'm sure this should do what you need without using a partial view.

If you use Model.Field syntax, you will only send the actual value of the Field property, for example 1.23 for your decimal place, you need to use m=>m.Field to get more than just evaluating the property so that you can change the text in the label .

0
source

Source: https://habr.com/ru/post/1382327/


All Articles