在辅助方法,奇怪的行为DropDownListFor [英] Strange behavior in helper method DropDownListFor
问题描述
我看到 DropDownListFor
一个奇怪的事情。这是我的视图的一部分:
1:< DT>
2:@ Html.LabelFor(型号=> model.MainModel.DefaultAreaId)
3:或其可/ dt的>
4:LT; DD>
5:@ Html.DropDownListFor(型号= GT; model.MainModel.DefaultAreaId,Model.FKs [DefaultAreaId]项目,新{@class =Areadd})
6:@ Html.ValidationMessageFor(型号=> model.MainModel.DefaultAreaId,空,新的{@class =无效-侧面说明})
7:LT; / DD>
我检查调试我的控制器的最后一行:返回查看(模型);
和 Model.FKs [DefaultAreaId的价值] .Items
是:
0:{标题=T1,值= 13,选择= FALSE},
1:{标题=T2,值= 15,选择= FALSE},
2:{标题=T3,值= 17,选择= FALSE},
3:{标题=T4,值= 16,选择= TRUE}
此外,我检查一下上面鉴于2线和价值观是相同的,但经过 DropDownListFor
5号线后究竟值更改为:
0:{标题=T1,值= 13,选择= FALSE},
1:{标题=T2,值= 15,选择= FALSE},
2:{标题=T3,值= 17,选择= FALSE},
3:{标题=T4,值= 16,选择= FALSE}
我无法理解与 DropDownListFor
发生什么,我的模型值?我也用同样的方式在同样的观点:
11:其中,DT>
12:@ Html.LabelFor(型号=> model.MainModel.DefaultControllerId)
13:< / DT>
14:< DD>
15:@ Html.DropDownListFor(型号=> model.MainModel.DefaultControllerId,Model.FKs [DefaultControllerId]项目,新{@class =Controllerdd})。
16:@ Html.ValidationMessageFor(型号=> model.MainModel.DefaultControllerId,空,新的{@class =无效-侧面说明})
17:< / DD>
和一切都很好,我也改变 DefaultAreaId
的名称,但没有任何变化。
因此,没有任何一个有任何解释?这里发生什么?
如果需要的话我可以复制我所有的视图,控制器和模型。
更新
在控制器:
列表< SelectListItem>地区=新的List< SelectListItem>();
Areas.Add(新SelectListItem的{text =T1,值=13,选择= FALSE});
Areas.Add(新SelectListItem的{text =T2,值=15,选择= FALSE});
Areas.Add(新SelectListItem的{text =T3,值=17,选择= FALSE});
Areas.Add(新SelectListItem的{text =T4,值=16,选择=真});Model.FKs.Add(DefaultAreaId,新ForeignKey的{标题=区,项目区=});
FKS
:
公众解释<字符串,ForeignKey的> FKS {搞定;组; }
和 ForeignKey的
是:
公共类ForeignKey的{
公共字符串名称{搞定;组; }
公开名单< SelectListItem>项目{搞定;组; }
}
您应该指定默认值以 model.MainModel.DefaultAreaId
在控制器发送模型视图前。
更新:的
但是这里有一个更好的办法,因为我认为:)的
我有一个辅助方法来设置所选项目在列表中是这样的:
公共静态的IEnumerable< SelectListItem>的setSelected(这IEnumerable的< SelectListItem>选择列表,对象了selectedValue)
{
选择列表=选择列表?新的List< SelectListItem>();
如果(==了selectedValue空)
返回选择列表;
变种vlaue = selectedValue.ToString();
返回selectList.BuildList(M = GT; m.Text,M => m.Value,空,M => String.Equals(m.Value,vlaue,StringComparison.CurrentCultureIgnoreCase));
}
在这里,如果你想BuildList helber,只要你想的SelectItem列表,您可以使用它的
公共静态的IEnumerable< SelectListItem> BuildList< T>(IEnumerable的< T>的项目,Func键< T,串>文字,Func键< T,串>值= NULL,字符串optionalText = NULL,Func键< T,BOOL>选择= NULL)
{
VAR列表= items.Select(X =>新建SelectListItem
{
文字= text.Invoke(X)
值=价值== NULL
? text.Invoke(X)
:value.Invoke(X)
选择=选择= NULL&放大器;!&安培; selected.Invoke(X)
})了ToList()。
如果(optionalText!= NULL)
list.Insert(0,新SelectListItem(){值=,文本= optionalText});
返回列表;
}
在控制器没有做任何事情,但设置要到模型中的值:
公众的ActionResult编辑(){
VAR模型=新YourModel();
model.CategoryId = 4;
// ..设置在ViewData的或你的情况你的模型DropDownList的项目
返回查看(模型);
}
和视图就叫DropDownListFor像往常一样,只是呼吁的setSelected
helper方法:
@ Html.DropDownListFor(型号=> model.CategoryId,((IEnumerable的< SelectListItem方式>)计算机[类别])的setSelected(model.CategoryId))
这将是在您的示例:
@ Html.DropDownListFor(型号=方式> model.MainModel.DefaultAreaId,Model.FKs [DefaultAreaId] Items.SetSelected(model.MainModel.DefaultAreaId))
只是不要忘记在你的控制器设置默认值。
I see a weird thing in DropDownListFor
. This is a part of My View:
1:<dt>
2: @Html.LabelFor(model => model.MainModel.DefaultAreaId)
3:</dt>
4:<dd>
5: @Html.DropDownListFor(model => model.MainModel.DefaultAreaId, Model.FKs["DefaultAreaId"].Items, new { @class = "Areadd" })
6: @Html.ValidationMessageFor(model => model.MainModel.DefaultAreaId, null, new { @class = "invalid-side-note" })
7:</dd>
I Check in Debug the Last Line of My Controller: return View(model);
And the values of Model.FKs["DefaultAreaId"].Items
is:
0: {Title="t1", value=13, Selected = false},
1: {Title="t2", value=15, Selected = false},
2: {Title="t3", value=17, Selected = false},
3: {Title="t4", value=16, Selected = true}
Also I Check it in line 2 of above view and the values is same, But Exactly after line 5 after DropDownListFor
values Change to:
0: {Title="t1", value=13, Selected = false},
1: {Title="t2", value=15, Selected = false},
2: {Title="t3", value=17, Selected = false},
3: {Title="t4", value=16, Selected = false}
I can't understand what happen with DropDownListFor
and my model values? I also use the same way in same view :
11:<dt>
12: @Html.LabelFor(model => model.MainModel.DefaultControllerId)
13:</dt>
14:<dd>
15: @Html.DropDownListFor(model => model.MainModel.DefaultControllerId, Model.FKs["DefaultControllerId"].Items, new { @class = "Controllerdd" })
16: @Html.ValidationMessageFor(model => model.MainModel.DefaultControllerId, null, new { @class = "invalid-side-note" })
17:</dd>
And everything is fine, I also change the name of DefaultAreaId
but nothing change.
So does any one have any explanation for this? what happen here?
if needed I can copy all of my View, Controller and Model.
Update
In Controller:
List<SelectListItem> Areas = new List<SelectListItem>();
Areas.Add(new SelectListItem { Text = "t1", Value = "13", Selected = false });
Areas.Add(new SelectListItem { Text = "t2", Value = "15", Selected = false });
Areas.Add(new SelectListItem { Text = "t3", Value = "17", Selected = false });
Areas.Add(new SelectListItem { Text = "t4", Value = "16", Selected = true });
Model.FKs.Add("DefaultAreaId", new ForeignKey { Title = "area", Items = Areas });
Fks
:
public Dictionary<string, ForeignKey> FKs { get; set; }
And ForeignKey
is:
public class ForeignKey {
public string Title { get; set; }
public List<SelectListItem> Items { get; set; }
}
You should assign default value to model.MainModel.DefaultAreaId
in the controller before sending the model to the view.
Update :
However here is a better way "as i assume :)" I've a helper method to set Selected Item in the list like this:
public static IEnumerable<SelectListItem> SetSelected(this IEnumerable<SelectListItem> selectList, object selectedValue)
{
selectList = selectList ?? new List<SelectListItem>();
if (selectedValue == null)
return selectList;
var vlaue = selectedValue.ToString();
return selectList.BuildList(m => m.Text, m => m.Value, null, m => String.Equals(m.Value, vlaue, StringComparison.CurrentCultureIgnoreCase));
}
Here if you want the BuildList helber and you can use it whenever you want SelectItem list
public static IEnumerable<SelectListItem> BuildList<T>(IEnumerable<T> items, Func<T, string> text, Func<T, string> value = null, string optionalText = null, Func<T, bool> selected = null)
{
var list = items.Select(x => new SelectListItem
{
Text = text.Invoke(x),
Value = value == null
? text.Invoke(x)
: value.Invoke(x),
Selected = selected != null && selected.Invoke(x)
}).ToList();
if (optionalText != null)
list.Insert(0, new SelectListItem() {Value = "", Text = optionalText});
return list;
}
in the controller don't do anything but set the value you want to the model:
public ActionResult Edit(){
var model = new YourModel();
model.CategoryId = 4;
//.. set the dropdownlist items in the ViewData or in your model in your case
return View(model);
}
and in the view just call DropDownListFor as usual and just call for SetSelected
helper method:
@Html.DropDownListFor(model => model.CategoryId, ((IEnumerable<SelectListItem>)ViewData["Categories"]).SetSelected(model.CategoryId))
And that would be in your example :
@Html.DropDownListFor(model => model.MainModel.DefaultAreaId, Model.FKs["DefaultAreaId"].Items.SetSelected(model.MainModel.DefaultAreaId))
Just don't forget to set the default value in your Controller.
这篇关于在辅助方法,奇怪的行为DropDownListFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!