asp.net-mvc 如何将参数传递给 mvc 4 中的局部视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20799658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I pass parameters to a partial view in mvc 4
提问by neel
I have a link like this:
我有一个这样的链接:
<a href='Member/MemberHome/Profile/Id'><span>Profile</span></a>
and when I click on this it will call this partial page:
当我点击它时,它会调用这个部分页面:
@{
switch ((string)ViewBag.Details)
{
case "Profile":
{
@Html.Partial("_Profile"); break;
}
}
}
Partial page _Profile contains:
部分页面 _Profile 包含:
Html.Action("Action", "Controller", model.Paramter)
Example:
例子:
@Html.Action("MemberProfile", "Member", new { id=1 }) // id is always changing
My doubt is that how can I pass this "Id" to model.parameter part?
我的疑问是如何将这个“Id”传递给 model.parameter 部分?
My controllers are:
我的控制器是:
public ActionResult MemberHome(string id)
{
ViewBag.Details = id;
return View();
}
public ActionResult MemberProfile(int id = 0)
{
MemberData md = new Member().GetMemberProfile(id);
return PartialView("_ProfilePage",md);
}
回答by Chris Pratt
Your question is hard to understand, but if I'm getting the gist, you simply have some value in your main view that you want to access in a partial being rendered in that view.
你的问题很难理解,但如果我明白了要点,你只是在你的主视图中有一些价值,你想在该视图中呈现的部分中访问它。
If you just render a partial with just the partial name:
如果您仅使用部分名称渲染部分:
@Html.Partial("_SomePartial")
It will actually pass your model as an implicit parameter, the same as if you were to call:
它实际上会将您的模型作为隐式参数传递,就像您要调用一样:
@Html.Partial("_SomePartial", Model)
Now, in order for your partial to actually be able to use this, though, it too needs to have a defined model, for example:
现在,为了让你的部分能够真正使用它,它也需要有一个定义的模型,例如:
@model Namespace.To.Your.Model
@Html.Action("MemberProfile", "Member", new { id = Model.Id })
Alternatively, if you're dealing with a value that's not on your view's model (it's in the ViewBag or a value generated in the view itself somehow, then you can pass a ViewDataDictionary
或者,如果您正在处理不在视图模型中的值(它在 ViewBag 中或以某种方式在视图本身中生成的值,那么您可以传递一个 ViewDataDictionary
@Html.Partial("_SomePartial", new ViewDataDictionary { { "id", someInteger } });
And then:
进而:
@Html.Action("MemberProfile", "Member", new { id = ViewData["id"] })
As with the model, Razor will implicitly pass your partial the view's ViewDataby default, so if you had ViewBag.Idin your view, then you can reference the same thing in your partial.
与模型一样,ViewData默认情况下,Razor 将隐式传递您的局部视图,因此如果您ViewBag.Id的视图中有,那么您可以在局部中引用相同的内容。
回答by Osama Sheikh
One of The Shortest method i found for single value while i was searching for myself, is just passing single string and setting string as model in view like this.
我在搜索自己时为单个值找到的最短方法之一,就是在这样的视图中传递单个字符串并将字符串设置为模型。
In your Partial calling side
在您的部分呼叫方
@Html.Partial("ParitalAction", "String data to pass to partial")
And then binding the model with Partial View like this
然后像这样将模型与 Partial View 绑定
@model string
and the using its value in Partial View like this
并像这样在部分视图中使用它的值
@Model
You can also play with other datatypes like array, int or more complex data types like IDictionary or something else.
您还可以使用其他数据类型,如数组、整数或更复杂的数据类型,如 IDictionary 或其他数据类型。
Hope it helps,
希望能帮助到你,
回答by Chris Haines
Here is an extension method that will convert an object to a ViewDataDictionary.
这是一个将对象转换为 ViewDataDictionary 的扩展方法。
public static ViewDataDictionary ToViewDataDictionary(this object values)
{
var dictionary = new ViewDataDictionary();
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(values))
{
dictionary.Add(property.Name, property.GetValue(values));
}
return dictionary;
}
You can then use it in your view like so:
然后您可以在您的视图中使用它,如下所示:
@Html.Partial("_MyPartial", new
{
Property1 = "Value1",
Property2 = "Value2"
}.ToViewDataDictionary())
Which is much nicer than the new ViewDataDictionary { { "Property1", "Value1" } , { "Property2", "Value2" }}syntax.
这比new ViewDataDictionary { { "Property1", "Value1" } , { "Property2", "Value2" }}语法好得多。
Then in your partial view, you can use ViewBagto access the properties from a dynamic object rather than indexed properties, e.g.
然后在您的局部视图中,您可以使用ViewBag从动态对象而不是索引属性访问属性,例如
<p>@ViewBag.Property1</p>
<p>@ViewBag.Property2</p>
回答by Gerard
For Asp.Net core you better use
对于 Asp.Net 核心,您最好使用
<partial name="_MyPartialView" model="MyModel" />
So for example
所以例如
@foreach (var item in Model)
{
<partial name="_MyItemView" model="item" />
}

