Required data not displayed in html.dropdownlist MVC

五迷三道 提交于 2019-12-13 21:31:00

问题


I am trying to populate some data from model to view in a dropdown list. For that I have created my model as

Model:

public class NoOfSupportingLanguages
{
    public int LanguageId { get; set; }
    public string LanguageName { get; set; }
}
public class OrganisationModel
{
      public SelectList NoofSupportingLanguages { get; set; }
}

And on the controller side , something like this:

Controller:

public ActionResult Index()
        {
            List<NoOfSupportingLanguages> LanguageOption = new         List<NoOfSupportingLanguages>{
                new NoOfSupportingLanguages{ LanguageId = 1, LanguageName="English" },
                new NoOfSupportingLanguages{ LanguageId = 2, LanguageName="Arabic" },
                new NoOfSupportingLanguages{ LanguageId = 3, LanguageName="French" }
            };
            SelectList languageList = new SelectList(LanguageOption, "LanguageId");
            model.NoofSupportingLanguages = languageList;
            return View(model);
        }

View:

 @Html.LabelFor(m => m.NoofSupportingLanguages, "Number of languages to support :")
 @Html.DropDownList("ddlNoOfSupportingLanguages", Model.NoofSupportingLanguages)

but in my dropdown I am getting values as :

Web.Models.Organisation.NoOfSupportingLanguages
Web.Models.Organisation.NoOfSupportingLanguages
Web.Models.Organisation.NoOfSupportingLanguages

Please suggest some help .

Thanks in advance !!


回答1:


If you want to do it in a clean MVC way you should change your model:

public class OrganisationModel
{
      public int SelectedLanguageId { get; set; }
      public List<SelectListItem> NoofSupportingLanguages { get; set; }
}

Then in your controller:

    public ActionResult Index()
    {
        List<SelectListItem> LanguageOption = new List<SelectListItem>{
            new SelectListItem { Value = 1.ToString(), Text = "English" },
            new SelectListItem { Value = 2.ToString(), Text = "Arabic" },
            new SelectListItem { Value = 3.ToString(), Text = "French" }
        };
        //here you can set selected value
        model.SelectedLanguageId = 1;
        model.NoofSupportingLanguages = LanguageOption;
        return View(model);
    }

And then you will be able to do like this:

 @Html.LabelFor(m => m.NoofSupportingLanguages, "Number of languages to support :")
 @Html.DropDownListFor(m => m.SelectedLanguageId, Model.NoofSupportingLanguages, "Select Language")

Now when you POST your model it will automatically bind Selected LanguageId to SelectedLanguageId model property.




回答2:


You have multiple errors with your code. First, to generate a SelectList you need to provide 2 property names (for the value attribute and the display text), so it needs to be

SelectList languageList = new SelectList(LanguageOption, "LanguageId", "LanguageName");

Next, you need a property to bind to, so you model needs to contain another property, say

public int SelectedLanguage { get; set; }

and then in the view its

@Html.DropDownListFor(m => m.SelectedLanguage, Model.NoofSupportingLanguages)

so that when you submit the form, the value of SelectedLanguage will be the value of the selected option.



来源:https://stackoverflow.com/questions/36077637/required-data-not-displayed-in-html-dropdownlist-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!