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

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -