Asp.net-Mvc
如何將參數傳遞給 mvc 4 中的局部視圖
我有一個這樣的連結:
<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 部分?
我的控制器是:
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); }
你的問題很難理解,但如果我得到了要點,你只是在你的主視圖中有一些價值,你想在該視圖中呈現的部分訪問。
如果您只使用部分名稱渲染部分:
@Html.Partial("_SomePartial")它實際上會將您的模型作為隱式參數傳遞,就像您要呼叫一樣:
@Html.Partial("_SomePartial", Model)現在,為了讓您的部分能夠真正使用它,它也需要有一個定義的模型,例如:
@model Namespace.To.Your.Model @Html.Action("MemberProfile", "Member", new { id = Model.Id })或者,如果您正在處理不在視圖模型上的值(它在 ViewBag 中或以某種方式在視圖本身中生成的值,那麼您可以傳遞
ViewDataDictionary@Html.Partial("_SomePartial", new ViewDataDictionary { { "id", someInteger } });然後:
@Html.Action("MemberProfile", "Member", new { id = ViewData["id"] })與模型一樣,Razor
ViewData預設會隱式傳遞您的局部視圖,因此如果您ViewBag.Id的視圖中有,那麼您可以在局部視圖中引用相同的內容。