asp.net mvc 5 - c# mvc 5 display model text from list based on id -


i have model:

public class model {    public string id { get; set }    public list<selectlistitem> items { get; set; } } 

now in view have:

@foreach (var item in model) {    @html.displayfor(modelitem => item.items.where(t=>t.value ==  item.id).select(t=>t.text)) } 

basically items property have this:

value = 1 text = "somethnig"  value = 2 text = "somethnig else"  value = 3 text = "somethnig else in here" 

the id property hold value of item, can 1, 2, 3 , forth.

how can display in view text of item rather value?

if want acces items lists, try this.

@foreach (selectlistitem item in model.items) {    @html.displayfor(item.text) } 

now if var item object of model datattype try this.

@foreach (var item in model) {    @html.displayfor(modelitem => item.items.where(t=>t.value ==  item.id)[0].text) } 

note: if there not object in list complies expression t.value == item.id there not going object in index 0, , reason give indexoutofrangeexception.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -