MVC3 - 传递数据超出了模型局部视图 [英] MVC3 - Passing data beyond the model to Partial view
问题描述
有没有办法通过一块额外的数据与模型的局部视图沿?
Is there a way to pass a piece of extra data along with a model to a Partial view?
例如
@ Html.Partial(_ SomeTable(列表< CustomTable>)ViewBag.Table);
是我现在有。我可以添加别的东西不改变我的模型?
Is what I have now. Can I add something else without changing my Model?
@ Html.Partial(_ SomeTable(列表< CustomTable>)ViewBag.TableTemporaryTable);
我看<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.viewdatadictionary.aspx\">ViewDataDictionary作为一个参数。我不知道这个对象做什么,或者这符合我的需要。
I see ViewDataDictionary as a param. I am not sure what this object does or if this meets my need.
推荐答案
的ViewDataDictionary可以用来取代ViewData字典中的局部视图...如果你没有传递的ViewDataDictionary参数则parial的可视数据是一样的家长。
ViewDataDictionary can be used to replace the ViewData dictionary in the partial view... If you don't pass a ViewDataDictionary parameter then the parial's viewdata is the same as the parents.
如何在父使用它的一个例子是:
An example of how to use it in the parent is:
@Html.Partial("_SomeTable", (List<CustomTable>)ViewBag.Table, new ViewDataDictionary {{ "Key", obj }});
然后内的部分你可以访问此OBJ如下:
Then within the partial you can access this obj as follows:
@{ var obj = ViewData["key"]; }
一个完全不同的方法woud是使用元组类组中的两个原始模型和额外数据一起如下:
A completely different approach woud be to use the Tuple class to group both the original model and extra data together as follows:
@Html.Partial("_SomeTable", Tuple.Create<List<CustomTable>, string>((List<CustomTable>)ViewBag.Table, "Extra data"));
为偏模型类型随后将是:
The model type for the partial would then be:
@model Tuple<List<CustomTable>, string>
Model.Item1给List对象和Model.Item2给出字符串
Model.Item1 gives the List object and Model.Item2 gives the string
这篇关于MVC3 - 传递数据超出了模型局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!