本文介绍了如何在从数据库中检索的MVC下拉列表中显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请任何人告诉新手,如何从多个值中显示下拉列表中的特定值.例如,我的下拉列表中有印度,澳大利亚,中国,英国,而我将国家更新为中国,因此当下次有人查看详细信息时,他将在下拉列表中看到中国.可见的代码如下:

Please any body tell a novice that how to show a particular value in dropdownlist from several values. For example I have India, Australia, China, England in my dropdownlist and I have updated the country as China, so when next time somebody view the detail he will see china in dropdownlist.The code in view is as follows:

@Html.DropDownList("list",ViewData["list"] as SelectList)
                                    @Html.ValidationMessageFor(model => model.Country)

.cs页面上的代码如下:

and Code on .cs page is as follows:

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在操作方法和视图之间传输数据.假设您正在尝试编辑客户记录.因此,您将拥有一个这样的视图模型

This is a solution which does not use ViewData,But uses viewmodels to transfer data between action methods and views. Assume you are trying to edit a customer record. So you will have a viewmodel like this

public class CustomerEditVM
{
  public int CustomerID { set;get;}
  public string Name { set;get;}
  public List<SelectListItem> Countries { 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"},
  }
}

您的视图应严格键入到CustomerEditVM,我们将使用Html.DropDownListFor辅助方法.

Your view should be strongly typed to CustomerEditVM and we will use Html.DropDownListFor helper method.

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

这篇关于如何在从数据库中检索的MVC下拉列表中显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:27