nee*_*eel 145 asp.net-mvc partial-views razor asp.net-mvc-4
我有这样的链接:
<a href='Member/MemberHome/Profile/Id'><span>Profile</span></a>
Run Code Online (Sandbox Code Playgroud)
当我点击它时,它会调用这个部分页面:
@{
switch ((string)ViewBag.Details)
{
case "Profile":
{
@Html.Partial("_Profile"); break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
部分页面_Profile包含:
Html.Action("Action", "Controller", model.Paramter)
Run Code Online (Sandbox Code Playgroud)
例:
@Html.Action("MemberProfile", "Member", new { id=1 }) // id is always changing
Run Code Online (Sandbox Code Playgroud)
我怀疑的是我如何将这个"Id"传递给model.parameter部分?
我的控制器是:
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);
}
Run Code Online (Sandbox Code Playgroud)
Chr*_*att 337
你的问题很难理解,但是如果我得到了要点,你只需要在主视图中有一些值,你想要在该视图中部分渲染.
如果只使用部分名称渲染部分:
@Html.Partial("_SomePartial")
Run Code Online (Sandbox Code Playgroud)
它实际上将您的模型作为隐式参数传递,就像您调用时一样:
@Html.Partial("_SomePartial", Model)
Run Code Online (Sandbox Code Playgroud)
现在,为了让你的部分实际上能够使用它,它也需要有一个已定义的模型,例如:
@model Namespace.To.Your.Model
@Html.Action("MemberProfile", "Member", new { id = Model.Id })
Run Code Online (Sandbox Code Playgroud)
或者,如果你正在处理一个不在你视图模型上的值(它在ViewBag中或者以某种方式在视图中生成的值),那么你可以传递一个 ViewDataDictionary
@Html.Partial("_SomePartial", new ViewDataDictionary { { "id", someInteger } });
Run Code Online (Sandbox Code Playgroud)
然后:
@Html.Action("MemberProfile", "Member", new { id = ViewData["id"] })
Run Code Online (Sandbox Code Playgroud)
与模型一样,Razor ViewData默认会隐式传递您的部分视图,所以如果您ViewBag.Id在视图中,那么您可以在部分中引用相同的内容.
Osa*_*ikh 41
我在搜索自己的时候找到单值的最短方法之一,就是在这个视图中传递单个字符串并将字符串设置为模型.
在你的部分呼叫方面
@Html.Partial("ParitalAction", "String data to pass to partial")
Run Code Online (Sandbox Code Playgroud)
然后像这样将模型与Partial View绑定
@model string
Run Code Online (Sandbox Code Playgroud)
并在Partial View中使用它的值
@Model
Run Code Online (Sandbox Code Playgroud)
您还可以使用其他数据类型,如array,int或更复杂的数据类型,如IDictionary或其他.
希望能帮助到你,
Chr*_*nes 14
这是一个将对象转换为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;
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在视图中使用它,如下所示:
@Html.Partial("_MyPartial", new
{
Property1 = "Value1",
Property2 = "Value2"
}.ToViewDataDictionary())
Run Code Online (Sandbox Code Playgroud)
哪个比new ViewDataDictionary { { "Property1", "Value1" } , { "Property2", "Value2" }}语法好得多.
然后在局部视图中,您可以使用ViewBag从动态对象而不是索引属性访问属性,例如
<p>@ViewBag.Property1</p>
<p>@ViewBag.Property2</p>
Run Code Online (Sandbox Code Playgroud)
对于 Asp.Net 核心,您最好使用
<partial name="_MyPartialView" model="MyModel" />
Run Code Online (Sandbox Code Playgroud)
所以例如
@foreach (var item in Model)
{
<partial name="_MyItemView" model="item" />
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
237452 次 |
| 最近记录: |