asp.net-mvc - 如何将参数传递给 mvc 4 中的局部 View

标签 asp.net-mvc asp.net-mvc-4 razor partial-views

我有一个这样的链接:

 <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/

相关文章:

javascript - 资源 URL 中的 ETag 或哈希值,例如。 js、css、图像

ruby-on-rails - 可以在 Rails 中定义多个同名的操作吗?

asp.net-mvc - Web API ajax 调用中参数为 null

c# - asp.net mvc 项目中 NeutralResourcesLanguageAttribute 和 uiCulture 的区别是什么

asp.net-mvc - MVC 4 路由无法正确读取 URL

c# - 配置路由到api到子文件夹

razor - 如何使用 razor 语法动态向 html 元素添加类?

c# - 如何使 PayPal Agreement start_date 在 ASP.NET 的 PayPal SDK 中有效

c# - 显示使用 ajax 更新的多个相关 ViewModel 的 MVC 页面

asp.net-mvc - Knockout Js、JQuery UI 对话框和部分 View