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.
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
Post a Comment