asp.net mvc - How to prevent Razor from adding prefixes to inputs when using nested display templates? -


when use nested display templates , add input elements through html helper razor engine adds prefix fields names.

i understand done guarantee input name uniqueness @ page level (and rebuild whole model on post back).

however have many small forms perform ad-hoc actions, , don't need neither name uniqueness nor ability rebuild whole model.

i need single property value, , having razor alter input items names breaks model binder when submit 1 of forms, since names different.

this example contains simplified nested model

public class student {     public guid id { get; set; }     public string firstname { get; set; }     public string lastname { get; set; }     public list<course> courses { get; set; } }  public class course {     public guid id { get; set; }     public string name { get; set; }     public list<grade> grades { get; set; } }  public class grade {     public guid id { get; set; }     public datetime date { get; set; }     public decimal value { get; set; } } 

and has index view 3 nested display templates

indexview     studentdisplaytemplate         coursedisplaytemplate             gradedisplaytemplate 

in grade display template add button remove grade

@model playground.sandbox.models.home.index.grade  <li>     @this.model.date: @this.model.value      @using (html.beginform("remove", "home", formmethod.post))     {         <input name="gradeid" type="hidden" value="@this.model.id" />          <input type="submit" value="remove" />     } </li> 

and on other side of request controller action receives grade id

public actionresult remove(guid id) {     // various things.     return this.redirecttoaction("index"); } 

if try using model helper

@html.hiddenfor(x => x.id) 

i html element

<input data-val="true"        data-val-required="the id field required."        id="courses_0__grades_1__id"        name="courses[0].grades[1].id"        type="hidden"        value="76f7e7ed-a479-42cb-add5-e58c0090770c" /> 

where field name gets prefix based on whole parent's view model tree.

using "manual" helper

@html.hidden("gradeid", this.model.id) 

gives html element

<input id="courses_0__grades_0__gradeid"        name="courses[0].grades[0].gradeid"        type="hidden"        value="bbb3c11d-d2d0-464a-b33b-ff7ac9815601" /> 

where prefix still present, albeit name @ end.

adding manually hidden input

<input name="gradeid" type="hidden" value="@this.model.id" /> 

gives html element

<input name="gradeid"        type="hidden"        value="a1a35e81-29cd-41b5-b619-bab79b767613" /> 

which want.

is possible achieve want, or getting display templates thing wrong?

you want set viewdata.templateinfo.htmlfieldprefix in grade template:

@model playground.sandbox.models.home.index.grade  @{     viewdata.templateinfo.htmlfieldprefix = ""; } 

this gives desired output of:

<input id="gradeid" name="gradeid" type="hidden" /> 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -