字典< string,string>的隐藏输入在ASP.NET MVC 3 [英] Hidden input for Dictionary<string, string> in ASP.NET MVC 3
问题描述
我需要在Post操作中获取我的模型属性,因此需要为它们隐藏元素,但是我有一个问题,类型为 Dictionary< string,string>
。这是我的模型:
public class ViewModel {
...
public ViewPart ViewPart {get;组; }
}
public class ViewPart {
...
public Dictionary< string,string>旗{get;组;
}
And My Controller:
字典< string,string> flags = new Dictionary< string,string>();
flags.Add(kind,Edit);
flags.Add(Command,Save);
ViewModel model = new ViewModel(){Flags = flags};
return View(model);
在视图中:
@foreach(Model.ViewPart.Flags中的var item){
< input type =hiddenid =ViewPart_Flags _ @(item.Key)value =@ item.Value name =ViewPart.Flags [@(item.Key)]/>
}
另请试试这一个:
@foreach(Model.ViewPart.Flags中的var项){
@ Html.HiddenFor(modelItem => item)
}
更新
发布操作:[HttpPost]
public ActionResult MyPostAction(ViewModel model){
//model.ViewPart.Flags为null
}
但在Post操作中,
标志
始终为空,为什么?我的错在哪里?有没有更好的方法来从View To Action传递变量?解决方案您需要两个隐藏的字段,一个用于
键
,一个用于Value
,如果你想模拟绑定到一个字典:var index = 0;
foreach(Model.ViewPart.Flags中的var项)
{
< input type =hiddenvalue =@ item.Key
name = ViewPart.Flags [@(指数)]键。/>
< input type =hiddenvalue =@ item.Value
name =ViewPart.Flags [@(index)]。Value/>
index ++;
}
< input type =submitvalue =保存/>
请注意,您还将需要一个运行的索引来使模型绑定器快乐。
或者如果您不想运行,可以使用附加隐藏的
索引
字段来解决:foreach(Model.ViewPart.Flags中的var项)
{
< input type =hidden value =@ item.Key
name =ViewPart.Flags.Index/>
< input type =hiddenvalue =@ item.Key
name =ViewPart.Flags [@(item.Key)]。键/>
< input type =hiddenvalue =@ item.Value
name =ViewPart.Flags [@(item.Key)]。值/>
}
< input type =submitvalue =保存/>
}
在这两篇文章中,您可以找到很多关于modelbinding与集合的信息: ASP用于模型绑定到数组,列表,集合,字典的.NET线格式
I need to get my model properties in Post action, so need to hidden element for them, but I have a problem with type of Dictionary <string, string>
. this is My model:
public class ViewModel{
...
public ViewPart ViewPart { get; set; }
}
public class ViewPart {
...
public Dictionary<string, string> Flags { get; set; }
}
And My Controller:
Dictionary<string, string> flags = new Dictionary<string, string>();
flags.Add("kind", "Edit");
flags.Add("Command", "Save");
ViewModel model = new ViewModel(){ Flags = flags };
return View(model);
In View:
@foreach(var item in Model.ViewPart.Flags) {
<input type="hidden" id="ViewPart_Flags_@(item.Key)" value="@item.Value" name="ViewPart.Flags[@(item.Key)]" />
}
Also I try This one :
@foreach(var item in Model.ViewPart.Flags) {
@Html.HiddenFor(modelItem => item)
}
Update Post Action:
[HttpPost]
public ActionResult MyPostAction(ViewModel model){
//model.ViewPart.Flags is null
}
But in Post action the Flags
is Always null, why? where is my fault? Is there any better way to pass variables from View To Action?
You need two hidden fields one for the Key
and one for the Value
if you want to modelbind to a dictionary:
var index = 0;
foreach (var item in Model.ViewPart.Flags)
{
<input type="hidden" value="@item.Key"
name="ViewPart.Flags[@(index)].Key"/>
<input type="hidden" value="@item.Value"
name="ViewPart.Flags[@(index)].Value"/>
index++;
}
<input type="submit" value="Save"/>
Note, you will be also need a running index to make the model binder happy.
Or if you don't want to have a running you can solve with an addtional hidden Index
field:
foreach (var item in Model.ViewPart.Flags)
{
<input type="hidden" value="@item.Key"
name="ViewPart.Flags.Index"/>
<input type="hidden" value="@item.Key"
name="ViewPart.Flags[@(item.Key)].Key" />
<input type="hidden" value="@item.Value"
name="ViewPart.Flags[@(item.Key)].Value" />
}
<input type="submit" value="Save"/>
}
You can find a lots of info about modelbinding with collections in this two article:
- ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries
- Model Binding To A List
这篇关于字典< string,string>的隐藏输入在ASP.NET MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!