簡體   English   中英

在下拉列表mvc4中編輯和更新

[英]Edit and Update in Dropdownlist mvc4

我正在嘗試在應用程序的下拉列表中實現編輯和更新。 從模型中顯示下拉列表的值列表。 但是所選的值不會顯示在下拉列表中。 所選值也將作為下拉列表中的值列表填充。

我的模特:

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

視圖

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

注意 :
填充item.State,但不顯示值
填充並顯示model.address.regionlist

調節器

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);
}

要在下拉列表中進行編輯,將顯示值列表。 但不會顯示所選的值。 我的代碼有什么錯誤。有任何建議。

編輯:

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

這個

model => item.State

是行不通的,因為它從html幫助器中隱藏了下拉列表的值來自模型的事實。 正確的實現方法是將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))                                     
}

假設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