简体   繁体   English

在下拉列表mvc4中编辑和更新

[英]Edit and Update in Dropdownlist mvc4

I am trying to implement edit and update in dropdownlist in my application. 我正在尝试在应用程序的下拉列表中实现编辑和更新。 The list of values for the dropdownlist are displayed from the model. 从模型中显示下拉列表的值列表。 But the selected value is not displayed on dropdownlist. 但是所选的值不会显示在下拉列表中。 The selected value is also populated as the list of values in dropdownlist. 所选值也将作为下拉列表中的值列表填充。

my model : 我的模特:

public string State
public SelectList RegionList { get; set; }
public class Region
{
 public string ID { get; set; }
 public string Name { get; set; }            
}

View 视图

@foreach (var item in Model.AddressList)
{
    @Html.DropDownListFor(model => item.State, new SelectList(Model.Address.RegionList, "Value", "Text", Model.Address.RegionList.SelectedValue))                                                     
}

Note : 注意 :
item.State is populated but the value is not displayed 填充item.State,但不显示值
model.address.regionlist is populated and displayed 填充并显示model.address.regionlist

Controller 调节器

public ActionResult EditAddress(AddressTuple tmodel,int AddressID)
{
    int country;
    string customerID = 1;
    List<AddressModel> amodel = new List<AddressModel>();
    amodel = GetAddressInfo(customerID, AddressID); // returns the selected value for dropdown
    foreach (var item in amodel)
    {
        country = item.CountryId;
    }            
    List<Region> objRegion = new List<Region>();
    objRegion = GetRegionList(id); // returns the list of values for dropdown
    SelectList objlistofregiontobind = new SelectList(objRegion, "ID", "Name", 0);
    atmodel.RegionList = objlistofregiontobind;

    tmodel.Address      = atmodel;
    tmodel.AddressList  = amodel;
    return View(tmodel);
}

For edit in dropdownlist, the list of values are displayed. 要在下拉列表中进行编辑,将显示值列表。 but the selected value is not displayed. 但不会显示所选的值。 What is the mistake in my code .Any suggestions. 我的代码有什么错误。有任何建议。

EDIT : 编辑:

@Html.DropDownListFor(model => model.State, new SelectList(Model.RegionList, "ID", "Name",Model.State))

This 这个

model => item.State

is not going to work, because it hides from the html helper the fact that value for the drop down is taken from the model. 是行不通的,因为它从html帮助器中隐藏了下拉列表的值来自模型的事实。 Correct way to implement this is with replacing foreach with for : 正确的实现方法是将foreach替换for

@for (int i=0; i<Model.AddressList.Count; i++)
{
    @Html.DropDownListFor(model => model.AddressList[i].State, new SelectList(Model.Address.RegionList, "Value", "Text", Model.Address.RegionList.SelectedValue))                                     
}

Assuming that the AddressList property is an IList<Something> try like this: 假设AddressList属性是IList<Something>尝试如下操作:

@for (var i = 0; i < Model.AddressList.Count; i++)
{
    @Html.DropDownListFor(
        model => model.AddressList[i].State, 
        new SelectList(Model.Address.RegionList, "Value", "Text")
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM