Asp.net mvc MVC2绑定不是';t适用于Html.DropDownListFor<&燃气轮机;
我正在尝试使用Html.dropdownlistforHTMLHelper,但在绑定post时遇到了一些问题。HTML正确呈现,但我在提交时从未获得“选定”值Asp.net mvc MVC2绑定不是';t适用于Html.DropDownListFor<&燃气轮机;,asp.net-mvc,viewmodel,Asp.net Mvc,Viewmodel,我正在尝试使用Html.dropdownlistforHTMLHelper,但在绑定post时遇到了一些问题。HTML正确呈现,但我在提交时从未获得“选定”值 <%= Html.DropDownListFor( m => m.TimeZones, Model.TimeZones, new { @class = "SecureDropDown",
<%= Html.DropDownListFor( m => m.TimeZones,
Model.TimeZones,
new { @class = "SecureDropDown",
name = "SelectedTimeZone" } ) %>
我尝试了一些不同的事情,我确信我正在做一些愚蠢的事情。。。只是不确定它是什么:)下面是我为您编写的一个示例,演示了DropDownList for helper方法的用法: 型号:
public class SettingsViewModel
{
public string TimeZone { get; set; }
public IEnumerable<SelectListItem> TimeZones
{
get
{
return TimeZoneInfo
.GetSystemTimeZones()
.Select(t => new SelectListItem
{
Text = t.DisplayName, Value = t.Id
});
}
}
}
视图:
x、 时区,
模型。时区,
新建{@class=“SecureDropDown”}
) %>
由于您只展示了代码的一部分,我无法说出它有什么问题。我知道我做错了什么。我不是为(x=>x.TimeZone)做DropDownListFor,而是为x.TimeZone做。谢谢你的帮助,达林。
public class SettingsViewModel
{
public string TimeZone { get; set; }
public IEnumerable<SelectListItem> TimeZones
{
get
{
return TimeZoneInfo
.GetSystemTimeZones()
.Select(t => new SelectListItem
{
Text = t.DisplayName, Value = t.Id
});
}
}
}
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new SettingsViewModel());
}
[HttpPost]
public ActionResult Index(SettingsViewModel model)
{
return View(model);
}
}
<% using (Html.BeginForm()) { %>
<%= Html.DropDownListFor(
x => x.TimeZone,
Model.TimeZones,
new { @class = "SecureDropDown" }
) %>
<input type="submit" value="Select timezone" />
<% } %>
<div><%= Html.Encode(Model.TimeZone) %></div>