我有一个这样的链接:
<a href='Member/MemberHome/Profile/Id'><span>Profile</span></a>
当我点击它时,它会调用这个部分页面:
@{
switch ((string)ViewBag.Details)
{
case "Profile":
{
@Html.Partial("_Profile"); break;
}
}
}
部分页面_Profile包含:
Html.Action("Action", "Controller", model.Paramter)
示例:
@Html.Action("MemberProfile", "Member", new { id=1 }) // id is always changing
我的疑问是如何将此“Id”传递给 model.parameter 部分?
我的 Controller 是:
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);
}
最佳答案
你的问题很难理解,但如果我明白了要点,你只是在你的主视图中拥有一些你想要在该 View 中渲染的部分中访问的值。
如果您仅使用部分名称渲染部分:
@Html.Partial("_SomePartial")
它实际上会将您的模型作为隐式参数传递,就像您要调用一样:
@Html.Partial("_SomePartial", Model)
现在,为了让您的部分真正能够使用它,它也需要有一个定义的模型,例如:
@model Namespace.To.Your.Model
@Html.Action("MemberProfile", "Member", new { id = Model.Id })
或者,如果您处理的值不在 View 模型中(它在 ViewBag 中或以某种方式在 View 本身中生成的值),那么您可以传递 ViewDataDictionary
@Html.Partial("_SomePartial", new ViewDataDictionary { { "id", someInteger } });
然后:
@Html.Action("MemberProfile", "Member", new { id = ViewData["id"] })
与模型一样,默认情况下,Razor 会隐式传递 View 的 ViewData
部分,因此如果您的 View 中有 ViewBag.Id
,那么您可以引用你的部分也有同样的事情。
关于asp.net-mvc - 如何将参数传递给 mvc 4 中的局部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20799658/