Asp.net mvc 如何在MVC4中向局部视图传递参数

Asp.net mvc 如何在MVC4中向局部视图传递参数,asp.net-mvc,asp.net-mvc-4,razor,partial-views,Asp.net Mvc,Asp.net Mvc 4,Razor,Partial Views,我有这样一个链接: <a href='Member/MemberHome/Profile/Id'><span>Profile</span></a> 部分页面配置文件包含: Html.Action("Action", "Controller", model.Paramter) 例如: @Html.Action("MemberProfile", "Member", new { id=1 }) // id is always changing

我有这样一个链接:

 <a href='Member/MemberHome/Profile/Id'><span>Profile</span></a>
部分页面配置文件包含:

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)
现在,为了让您的partial能够实际使用它,它也需要有一个已定义的模型,例如:

@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
,那么您可以在分部中引用相同的内容。

这里有一个扩展方法,可以将对象转换为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;
}
然后,您可以在视图中使用它,如下所示:

@Html.Partial("_MyPartial", new
{
    Property1 = "Value1",
    Property2 = "Value2"
}.ToViewDataDictionary())
这比新的ViewDataDictionary{{“Property1”、“Value1”}、{“Property2”、“Value2”}语法要好得多

然后,在局部视图中,可以使用
ViewBag
从动态对象而不是索引属性访问属性,例如

<p>@ViewBag.Property1</p>
<p>@ViewBag.Property2</p>
@ViewBag.Property1

@ViewBag.Property2


在我搜索自己的时候,我找到了一个最短的单值方法,就是传递单个字符串,并将字符串设置为视图中的模型,如下所示

在你的部分呼叫端

@Html.Partial("ParitalAction", "String data to pass to partial")
然后像这样用局部视图绑定模型

@model string
在这样的局部视图中使用它的值

@Model
您还可以使用其他数据类型(如array、int)或更复杂的数据类型(如IDictionary或其他)


希望对您有所帮助,

对于Asp.Net内核您可以更好的使用

<partial name="_MyPartialView" model="MyModel" />

比如说

@foreach (var item in Model)
{
   <partial name="_MyItemView" model="item" />
}
@foreach(模型中的变量项)
{
}

我不明白。也许如果您添加控制器和操作可能会有所帮助,但正如现在所写的,我不理解您的问题。请参阅MSDS上的关于扩展如果您让主模型实现一个接口,您可以让局部视图将该接口用作模型。然后您可以从中获得大量重用。@ChrisPratt Very explan答案一如既往:)投票+我必须使用
@Html.Partial(“_SomePartial”,新的Microsoft.AspNet.Mvc.ViewFeatures.ViewDataDictionary(this.ViewData){{“id”,someInteger})让这对我有用。如果其他人也有同样的问题,我将使用VS2015 DNX 4.5.1。这很好!正是我想要的。谢谢。我的partial不需要我的模型,但我需要保留所有ViewData ModelState的内容。。这就成功了。在我的局部视图中,我仍然可以访问ViewContext.ViewData.ModelState.ContainsKey(@Model.ToString())这会持续引发
NullReferenceException
,对象引用未设置为对象的实例,这意味着
Model
为null。有什么想法吗?好吧,我投票的时候有点早了。当我尝试这样做时,我得到的消息是,我正试图将视图模型传递给局部视图,但它需要一个字符串。我很确定我做的是对的。我最终创建了一个简单的局部视图模型,其中包含一个文本属性来解决这个问题(并将其传递给局部视图),因为这浪费了太多的时间。我应该把这个
放到ViewDataDictionary
静态方法的哪里?无论我想把它放在哪里,都会出现某种编译器错误。@CsabaToth这是一种扩展方法。只需将其放置在静态类中,然后在视图中引用该类。
@foreach (var item in Model)
{
   <partial name="_MyItemView" model="item" />
}