Modelbinding with SelectList

筅森魡賤 提交于 2019-12-10 11:37:28

问题


I create a DropDown with the Html.DropDownList(string NameSelectListInViewData) method. This generates a valid Select input with the correct values. And all is well.

Upon submit however, the value in the source SelectList is not bound.

Case: ViewData.SearchBag.FamilyCodes:

public SelectList FamilyCodes { get; set; }

Html that generates the dropdown:

<%=Html.DropDownList("SearchBag.FamilyCodes")%>

Generated html:

<select id="SearchBag.FamilyCodes" name="SearchBag.FamilyCodes">
    <option value=" ">Any</option>
    <option value="A">BLUE</option>
    <option value="B">BLACK</option>
    <option value="C">BEIGE</option>
    <option value="G">GREEN</option>
    <option value="O">ORANGE</option>
    <option value="P">PURPLE</option>
    <option value="R">RED</option>
    <option value="S">GRAY</option>
    <option value="U">BROWN</option>
    <option value="W">WHITE</option>
    <option value="Y">YELLOW</option>
</select>

In my controller I have an action with a parameter searchBag.

public ActionResult AdvancedSearch(SearchBag searchBag) { 
    //Do Stuff with parameters in searchBag
    return View("AdvancedSearch", searchViewData);
}

All other fields bind just fine, only the selection boxes don't. Any ideas?

UPDATE
For future readers it might be worth it to read this blog post: http://haacked.com/archive/0001/01/01/model-binding-to-a-list.aspx


回答1:


While "SearchBag.FamilyCodes" is a valid HTML id, the framework will attempt to map each form item to a corresponding parameter of the same name and you cannot have a period in a parameter name. If you assign a separate ID to the control (different overload of the Html.DropDownList method), does it come through?




回答2:


SearchBag needs to be lower case in the html (searchBag rather than SearchBag)




回答3:


I think you need to change the type of FamilyCodes from SelectList to string.

public string FamilyCodes { get; set; }

Then on submit the value of FamilyCodes should be the value selected from the dropdown list. I have several of these in my current project and have no problems.




回答4:


Had a similar issue yesterday with select lists, the list would generate fine, just that on UpdateModel- would fail =- not bound?

And i found the answer in the parameters list...

SelectList (
Collection - items to use in the  drop down,
ValueField - ie the primarykey as a String,
NameField  - ie the name of the thing as a String,
SelectedValue -  which is the passed in current objects FK relationship)

So for me ...

Country = new SelectList(db.Countries, "pkCountry", "CountryName",address.fkCountry);

I use the ViewModel approach - and have this in the constructor of the view Model...

public AddressCountryViewModel(){
public SelectList Countrys {get; private set;}
public AddressCountryViewModel(Address address)
{
     Countrys = new SelectList(db.Countries, "pkCountry", "CountryName",address.fkCountry);
}

I then grab the values in the controllers Edit Action and assign back to the object that is updating...

address.fkCountry = Convert.ToInt32(collection["fkCountry"]);


来源:https://stackoverflow.com/questions/295313/modelbinding-with-selectlist

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