asp.net-mvc-3 – DropDownListFor中的ViewBag属性值,而不是Model属性值
发布时间:2020-05-27 07:43:25 所属栏目:asp.Net 来源:互联网
导读:我们在DropDownListFor(ASP.NET MVC3版本)中发现了奇怪的行为.它在下拉列表中选择ViewBag属性值而不是Model属性值. 模型: public class Country { public string Name { get; set; }}public class User { public Country Country { get; set;
|
我们在DropDownListFor(ASP.NET MVC3版本)中发现了奇怪的行为.它在下拉列表中选择ViewBag属性值而不是Model属性值. 模型: public class Country {
public string Name { get; set; }
}
public class User {
public Country Country { get; set; }
}
控制器索引动作: ViewBag.CountryList = new List<Country> { /* Dropdown collection */
new Country() { Name = "Danmark" },new Country() { Name = "Russia" } };
var user = new User();
user.Country = new Country(){Name = "Russia"}; /* User value */
ViewBag.Country = new Country() { Name = "Danmark" }; /* It affects user */
return View(user);
视图: @Html.EditorFor(user => user.Country.Name) @Html.DropDownListFor(user => user.Country.Name,new SelectList(ViewBag.CountryList,"Name",Model.Country),"...") 它将显示带有“俄罗斯”值的文本框和带有“Danmark”值的下拉列表,而不是“俄罗斯”. 我没有找到任何关于此行为的文档.这种行为是否正常?为什么这是正常的?因为很难控制ViewBag和Model属性名称. This sample MVC3 project sources 解决方法我不太确定为什么做出这个决定,但是之所以发生这种情况是因为MVC框架在使用参数提供的值之前尝试使用ViewData提供的值.这就是ViewBag.Country覆盖参数提供的值Model.Country的原因.这就是私有方法SelectInternal在MVC框架中的written. object defaultValue = (allowMultiple) ? htmlHelper.GetModelStateValue(fullName,typeof(string[])) : htmlHelper.GetModelStateValue(fullName,typeof(string));
// If we haven't already used ViewData to get the entire list of items then we need to
// use the ViewData-supplied value before using the parameter-supplied value.
if (!usedViewData) {
if (defaultValue == null) {
defaultValue = htmlHelper.ViewData.Eval(fullName);
}
}
if (defaultValue != null) {
IEnumerable defaultValues = (allowMultiple) ? defaultValue as IEnumerable : new[] { defaultValue };
IEnumerable<string> values = from object value in defaultValues select Convert.ToString(value,CultureInfo.CurrentCulture);
HashSet<string> selectedValues = new HashSet<string>(values,StringComparer.OrdinalIgnoreCase);
List<SelectListItem> newSelectList = new List<SelectListItem>();
foreach (SelectListItem item in selectList) {
item.Selected = (item.Value != null) ? selectedValues.Contains(item.Value) : selectedValues.Contains(item.Text);
newSelectList.Add(item);
}
selectList = newSelectList;
}
此代码defaultValue = htmlHelper.ViewData.Eval(fullName);试图从ViewData获取值,如果它可以获取值,它将覆盖提供的参数selectList with new list. 希望它可以提供帮助.谢谢. side-node:ViewBag只是ViewData的动态包装类. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net+js实现批量编码与解码的方法
- asp.net-mvc – ASP.NET MVC和IIS 5
- asp.net-mvc – 如何应用css类到mvccontrib网格
- 在ASP.NET C#中调用Web服务时,Session为null
- asp.net-mvc – 如何从ASP.NET MVC VIEWS文件夹访问HTML文件
- asp.net – 无法安装Microsoft.AspNet.Razor 3.0.0
- asp.net – GridView中的工具提示
- asp.net-mvc – 从MVC中的控制器确定部分视图的模型
- asp.net-mvc-3 – 如何使用下拉列表的数据注释?
- 如何在ASP.NET MVC控制器(ActionResult)中更改返回的Conten
推荐文章
站长推荐
- asp.net-mvc – ASP.NET MVC验证的唯一性
- ASP.NET – 从静态方法/静态类访问会话?
- asp.net-mvc – 如何在MVC 3中基于XML文件动态创
- asp.net-mvc – redirectToAction()和View()之间
- asp.net – 无法转换类型为’System.Web.UI.Lite
- asp.net – 很好的复杂linq到sql示例?
- asp.net-mvc – 使用mvc的主要目的
- asp.net-mvc-3 – 如何在Asp.Net Mvc 3中显示自定
- asp.net-mvc – ASP.NET MVC快速启动 – 一站式教
- nuget-package – MvcScaffolding是否通过命令行
热点阅读
