c# - The given key was not present in the dictionary. in Razorview -


i have error the given key not present in dictionary. in razor view

please check below code

<select class="form-control" name="country" id="country">     <option value="">select country</option>     @foreach (var country in viewbag.countrylist)     {         <option @((country.countrycode == "gb") ? "selected" : "") value="@country.countrycode">@country.countryname</option>     } </select> 

my country model class

public class country {     public long countryid { get; set; }     public string countrycode { get; set; }     public string countryname { get; set; } } 

for viewbar.countrylist add this

list<country> countrylist = common.getcountry(); viewbag.countrylist = jsonconvert.serializeobject(countrylist); 

please check in below image got data in variable error above.

enter image description here

list<airport> airportcodelist = common.getairport(); viewbag.airportcodelist = jsonconvert.serializeobject(airportcodelist); 

should

list<airport> airportcodelist = common.getairport(); viewbag.countrylist= jsonconvert.serializeobject(airportcodelist); 

don't use viewbag this, use typed model, use dropdownlist html helper in other answer

also, why serialize it? assign objects

viewbag.countrylist= airportcodelist; 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -