Add a prefix to manage the identifier and bind it to MVC Razor

I have a case where I have a page with the order and tabs showing the details of the order. The details of the order are quite complex, and since they are the same layout, I want to use a partial view template or editor that will generate the form.

The problem is that a multiple double form input identifier is generated (one for each order detail. For example, I have:

foreach (var orderDetail in Model.OrderDetils) { @Html.EditorFor(model => orderDetail, "WorkOrder", orderDetail) } 

I read a lot about this and see solutions that recommend using editortemplate, but this solution only works when you have the same form for rendering, but passing it different model properties, so the control identifier prefix will be different ... ie. how is that decision .

In my case, this will not work, since the property of the model that I pass is always the same.

So, how else can I create a unique identifier in a partial or editorial template that will also be bound.

I know instead:

 @Html.EditorFor(model => model.WOHdr.Attribute1) 

I could do:

 @Html.TextBoxFor(model => model.WOHdr.Attribute1, new { id = Model.Id + "_Attribute1" }) 

But then he will not communicate when he goes to the controller.

Thoughts?

+4
source share
2 answers

try it

 @Html.TextBoxFor(model => model.WOHdr.Attribute1, new { @id = @Model.Id + "_Attribute1" }) 

Use the dynamic value "@" +. Now you will get a unique identifier

In the editor, you can use

 @Html.EditorFor(model => model.WOHdr.Attribute1, null, "id=" + @Model.Id + "" ) 

id will generate like this

 id="id_1", id="id_2" and so on.. 
+1
source

<input id=" Checkbox1_@ (test.TestId)" type="checkbox" />

Hope the top code helps you

0
source

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


All Articles