MVC3 - HiddenFor字典中的值 [英] MVC3 - HiddenFor Dictionary Values

查看:160
本文介绍了MVC3 - HiddenFor字典中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本字典属性叫周:

I have a dictionary property called Week:

public IDictionary<DayOfWeek, Day> Week { get; private set; }

和我想通过自己的价值观关闭以 HiddenFor (天)

And I'm trying to pass its values off to HiddenFor (Days)

@Html.HiddenFor(x => x.Week.Values)

它必须是模型的属性,所以我不能做 x.Week.Values​​.ToList();

我怎么会去传递字典值到 Html.HiddenFor

How would I go about passing the Dictionary Values to the Html.HiddenFor?

推荐答案

嗯,你使用HiddenFor因为,我要假设你需要重新绑定的形式后字典的值。要绑定一个字典视图,你将需要做这样的事情:

Well since your using HiddenFor, I'm going to assume that you need to rebind the values of the dictionary on form post. To bind a Dictionary to the view, your going to need to do something like this:

@foreach(var key in Model.Week.Keys){
     Html.DisplayFor(model=>model.Week[key])
}

在字典中的每个值将被赋予它自己隐藏的输入字段,名称属性:
NAME =周{这里的关键}

Each value in the dictionary will be given its own Hidden Input field, with the name attribute: name="Week.{key here}

如果,在另一方面,所有你需要做的就是在模型中的数据发送到客户端,这样就可以在JavaScript用它做什么,你可能想看看它写入页面JSON。

If, on the other hand, all you need to do is send the data in your model to the client so that you can do something with it in javascript, you might want to look at writing it to the page as JSON.

<script type="text/javascript">
@Html.Raw(Json.Encode(Model.Week))
</script>

这篇关于MVC3 - HiddenFor字典中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆