Asp.net mvc 如何在从数据库检索的MVC dropdownlist中显示值

Asp.net mvc 如何在从数据库检索的MVC dropdownlist中显示值,asp.net-mvc,Asp.net Mvc,请告诉新手如何从几个值中显示dropdownlist中的特定值。例如,我的dropdownlist中有印度、澳大利亚、中国、英国,我已将国家更新为中国,因此下次有人查看详细信息时,他将在dropdownlist中看到中国。查看的代码如下: @Html.DropDownList("list",ViewData["list"] as SelectList) @Html.ValidationMessageFor(model =&g

请告诉新手如何从几个值中显示dropdownlist中的特定值。例如,我的dropdownlist中有印度、澳大利亚、中国、英国,我已将国家更新为中国,因此下次有人查看详细信息时,他将在dropdownlist中看到中国。查看的代码如下:

@Html.DropDownList("list",ViewData["list"] as SelectList)
                                    @Html.ValidationMessageFor(model => model.Country)
.cs页面上的代码如下:

var list = new SelectList(new[]
                                          {
                                              new {ID="0",Name="Select"},
                                              new{ID="1",Name="Australia"},
                                              new{ID="3",Name="United States"},
                                              new {ID="4",Name="United Kingdom"},
                                              new{ID="5",Name="Europe"},
                                              new{ID="6",Name="Canada"},
                                              new {ID="7",Name="India"},
                                              new {ID="8",Name="China"},
                                              new{ID="9",Name="Japan"},
                                              new{ID="10",Name="New Zealand"},
                                          },
                            "ID", "Name", 1);
            ViewData["list"] = list;

此解决方案不使用ViewData,而是使用viewmodels在动作方法和视图之间传输数据。假设您正在尝试编辑客户记录。因此,您将有一个这样的viewmodel

public class CustomerEditVM
{
  public int CustomerID { set;get;}
  public string Name { set;get;}
  public List<SelectListItem> Countries { set;get;}
  public int SelectedCountry { set;get;}
}
公共类CustomerEditVM
{
public int CustomerID{set;get;}
公共字符串名称{set;get;}
公共列表国家{set;get;}
public int SelectedCountry{set;get;}
}
在你的行动方法中

public ActionResult View(int id)
{
  var customer=repositary.GetCustomer(id);
  var customerEditVM=new CustomerEditVM { CustomerID=id,Name=customer.Name};
  customerEditVM.Countries=GetCountries();

  //Setting the selected item value here
  customerEditVM.SelectedCountry=customer.CountryID 

  return View(customerEditVM);
}
public List<SelectListItem> GetCountries()
{
  return new List<SelectListItem> {
     new SelectListItem{ Value="1",Text="India"},
     new SelectListItem{ Value="2",Text="China"},
     new SelectListItem{ Value="3",Text="US"},
  }
}
public ActionResult视图(int-id)
{
var customer=repositary.GetCustomer(id);
var customerEditVM=new customerEditVM{CustomerID=id,Name=customer.Name};
customerEditVM.Countries=GetCountries();
//在此处设置所选项目值
customerEditVM.SelectedCountry=customer.CountryID
返回视图(customerEditVM);
}
国家/地区公共列表()
{
返回新列表{
新建SelectListItem{Value=“1”,Text=“India”},
新建SelectListItem{Value=“2”,Text=“China”},
新建SelectListItem{Value=“3”,Text=“US”},
}
}
您的视图应该是强类型的CustomerEditVM,我们将使用Html.DropDownListFor帮助器方法

@model YourNamespaceHere.CustomerEditVM
@using(Html.BeginForm())
{
 Country : 
 @Html.DropDownListFor(s=>s.SelectedCountry,Model.Countries,"Select")
 <input type="submit" value="Save" />
 @Html.HiddenFor(s=>s.CustomerID)
}
@model YourNamespaceHere.CustomerEditVM
@使用(Html.BeginForm())
{
国家:
@Html.DropDownListFor(s=>s.SelectedCountry,Model.Countries,“选择”)
@Html.HiddenFor(s=>s.CustomerID)
}

你为什么给我负号,explain@fealin大喊大叫..现在请看编辑的代码我真的不明白你的态度。我不是那个否决这个问题的人。我不确定,你这样的反应会得到你想要的。我想你应该试试搜索引擎。试试这些术语。如何在中选择一个值DropDownList@fealin请不要误会我,伙计,如果它伤害了你,我很抱歉,我真的不是那个意思。顺便说一句,谢谢你的帮助和指导。圣诞快乐。