我想为下一段代码使用集合初始值设定项:
public Dictionary<int, string> GetNames()
{
Dictionary<int, string> names = new Dictionary<int, string>();
names.Add(1, "Adam");
names.Add(2, "Bart");
names.Add(3, "Charlie");
return names;
}
Run Code Online (Sandbox Code Playgroud)
通常它应该是这样的:
return new Dictionary<int, string>
{
1, "Adam",
2, "Bart"
...
Run Code Online (Sandbox Code Playgroud)
但是这个的正确语法是什么?
有没有办法将一段额外的数据和模型传递给部分视图?
例如
@Html.Partial("_SomeTable", (List<CustomTable>)ViewBag.Table);
是我现在拥有的.我可以在不改变模型的情况下添加其他内容吗
@Html.Partial("_SomeTable", (List<CustomTable>)ViewBag.Table, "TemporaryTable");
我将ViewDataDictionary视为一个参数.我不确定这个对象是做什么的,或者这是否符合我的需要.
我正在尝试ViewData使用ViewDataDictionary.将主页中的对象传递给视图用户控件.
问题是,ViewDataDictionary无论我尝试使用哪种方式,都不会在视图用户控件中返回任何值.
下面的示例代码仅使用匿名对象进行演示,尽管此方法或传递ViewData对象都不起作用.
以下是RenderPartial我正在尝试使用的辅助方法:
<% Html.RenderPartial("/Views/Project/Projects.ascx", ViewData.Eval("Projects"), new ViewDataDictionary(new { Test = "Mark" })); %>
Run Code Online (Sandbox Code Playgroud)
并在我看来用户控制我执行以下操作:
<%= Html.Encode(ViewData["Test"]) %>
Run Code Online (Sandbox Code Playgroud)
为什么这不会返回任何东西?
谢谢你的帮助.
编辑:
我能够毫无问题地传递和访问强类型模型.这是ViewDataDictionary我试图用来传递模型之外的单个值...