ASP.Net MVC DROPDOWNLIST和选择的值 [英] ASP.Net MVC DropdownList and selected value
问题描述
我能够创建一个DropDownList我的MVC项目,但价值没有被选为在DropDownList的选定值。这只是表明全县名单从第一。值是从数据库。我试图从previous后找到它,但它是相当混乱。请给我建议的任何想法。
控制器code
公众的ActionResult编辑(int i)以
{
变种项=新的SelectList(db.MST_COUNTRies.ToList(),COUNTRY_ID,COUNTRY_NM);
计算机[MST_COUNTRies] =物品;
}
查看code
<%= Html.DropDownList(COUNTRY_ID(的SelectList)ViewData的[MST_COUNTRies])%GT;
我会使用强类型的视图(不需要的ViewData code>和魔术字符串)建议你。与往常一样开始定义您的视图模型类:
公共类CountriesViewModel
{
公众诠释? SelectedCountryId {搞定;组; }
公共IEnumerable的< SelectListItem>国家{搞定;组; }
}
然后控制器:
公共类HomeController的:控制器
{
公众的ActionResult指数(INT?ID)
{
// TODO:从您的数据库获取这些
VAR国家=新[]
{
新{ID = 1,名称=国家1},
新{ID = 2,名称=国家2},
}; VAR模型=新CountriesViewModel
{
国家=新的SelectList(国家,ID,姓名,ID)
}; 返回查看(模型);
}
}
和最后你的强类型视图:
<%@页面语言=C#的MasterPageFile =〜/查看/共享/的Site.Master继承=System.Web.Mvc.ViewPage< SomeNs.Models。 CountriesViewModel>中%GT;< ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器> &所述;%= Html.DropDownListFor(
X => x.SelectedCountryId,
Model.Countries,
- 选择国家 - )
%GT;< / ASP:内容>
当你调用
现在 /家庭/指数
你没有选择的国家,但是,当你通过 ID
来这个动作像 /家庭/指标/ 2
你的国家,这个ID选择。
注:如果您使用的是ASP.NET MVC 1.0,你将不会有强类型DropDownListFor帮手,你可以用这个来代替:
<%= Html.DropDownList(
SelectedCountryId
Model.Countries,
- 选择国家 - )
%GT;
I was able to create a dropdownlist for my MVC project, but the value is not selected as selected value on the dropdownlist. It just shows whole county list from the first.The value is from database. I tried find it from previous post, but it was quite confusing. Please, suggest me for any idea.
Controller Code
public ActionResult Edit(int i)
{
var items = new SelectList(db.MST_COUNTRies.ToList(), "COUNTRY_ID", "COUNTRY_NM");
ViewData["MST_COUNTRies"] = items;
}
View Code
<%= Html.DropDownList("COUNTRY_ID", (SelectList)ViewData["MST_COUNTRies"])%>
I would suggest you using a strongly typed views (no need of ViewData
and magic strings). As always start by defining your view model class:
public class CountriesViewModel
{
public int? SelectedCountryId { get; set; }
public IEnumerable<SelectListItem> Countries { get; set; }
}
Then your controller:
public class HomeController : Controller
{
public ActionResult Index(int? id)
{
// TODO: Fetch those from your DB
var countries = new[]
{
new { Id = 1, Name = "Country 1" },
new { Id = 2, Name = "Country 2" },
};
var model = new CountriesViewModel
{
Countries = new SelectList(countries, "Id", "Name", id)
};
return View(model);
}
}
And finally your strongly typed view:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SomeNs.Models.CountriesViewModel>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.DropDownListFor(
x => x.SelectedCountryId,
Model.Countries,
"-- Select Country --")
%>
</asp:Content>
Now when you call /home/index
you get no country selected, but when you pass the id
to this action like /home/index/2
you get the country with this id selected.
Remark: If you are using ASP.NET MVC 1.0 you won't have the strongly typed DropDownListFor helper and you could use this instead:
<%= Html.DropDownList(
"SelectedCountryId",
Model.Countries,
"-- Select Country --")
%>
这篇关于ASP.Net MVC DROPDOWNLIST和选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!