166

I have a link like this:

 <a href='Member/MemberHome/Profile/Id'><span>Profile</span></a>

and when I click on this it will call this partial page:

 @{
    switch ((string)ViewBag.Details)
    {

        case "Profile":
        {
           @Html.Partial("_Profile"); break;
        }

    }
}

Partial page _Profile contains:

Html.Action("Action", "Controller", model.Paramter) 

Example:

@Html.Action("MemberProfile", "Member", new { id=1 })   // id is always changing

My doubt is that how can I pass this "Id" to model.parameter part?

My controllers are:

 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);
    }
neel
  • 4,583
  • 12
  • 37
  • 63
  • 2
    I don't get it. Perhaps if you add your controller and action it might help, but as it's written now, I don't understand your question. – Liam Dec 27 '13 at 11:14
  • see about [Partial](http://msdn.microsoft.com/ru-ru/library/ee402926(v=vs.108).aspx) extension on msdn – Grundy Dec 27 '13 at 11:21

4 Answers4

370

Your question is hard to understand, but if I'm getting the gist, you simply have some value in your main view that you want to access in a partial being rendered in that view.

If you just render a partial with just the partial name:

@Html.Partial("_SomePartial")

It will actually pass your model as an implicit parameter, the same as if you were to call:

@Html.Partial("_SomePartial", Model)

Now, in order for your partial to actually be able to use this, though, it too needs to have a defined model, for example:

@model Namespace.To.Your.Model

@Html.Action("MemberProfile", "Member", new { id = Model.Id })

Alternatively, if you're dealing with a value that's not on your view's model (it's in the ViewBag or a value generated in the view itself somehow, then you can pass a ViewDataDictionary

@Html.Partial("_SomePartial", new ViewDataDictionary { { "id", someInteger } });

And then:

@Html.Action("MemberProfile", "Member", new { id = ViewData["id"] })

As with the model, Razor will implicitly pass your partial the view's ViewData by default, so if you had ViewBag.Id in your view, then you can reference the same thing in your partial.

Chris Pratt
  • 207,690
  • 31
  • 326
  • 382
  • 4
    If you make your main model implement an interface, you can have the partial view use that interface as a model. Then you can get a lot of reuse out of it. – Jess Sep 16 '14 at 12:58
  • @ChrisPratt Very explanatory answer as always :) Voted+ – Murat Yıldız May 27 '15 at 09:54
  • 7
    I had to use `@Html.Partial("_SomePartial", new Microsoft.AspNet.Mvc.ViewFeatures.ViewDataDictionary(this.ViewData) { { "id", someInteger } });` for this to work for me. I'm using VS2015 DNX 4.5.1 if anyone else has this same issue. – MikeTeeVee Jun 22 '16 at 09:17
51

One of The Shortest method i found for single value while i was searching for myself, is just passing single string and setting string as model in view like this.

In your Partial calling side

@Html.Partial("ParitalAction", "String data to pass to partial")

And then binding the model with Partial View like this

@model string

and the using its value in Partial View like this

@Model

You can also play with other datatypes like array, int or more complex data types like IDictionary or something else.

Hope it helps,

Osama Sheikh
  • 844
  • 1
  • 10
  • 14
  • 5
    This is neat! Exactly what I wanted. Thanks. – Gautam Jain Nov 22 '17 at 09:30
  • My partial did not need my model, but I needed to preserve all the ViewData ModelState stuff.. this did the trick. In my partial view, I can still access ViewContext.ViewData.ModelState.ContainsKey(@Model.ToString()) – da_jokker Aug 03 '18 at 18:35
  • This keeps throwing a `NullReferenceException`, object reference not set to an instance of an object, it's saying that `Model` is null. Any ideas? – mekb Aug 22 '19 at 11:19
  • OK, I was a bit premature upvoting this. When I try this, I get the message that I'm trying to pass a view model to the partial view, but it's expecting a string. I'm pretty certain I'm doing the right thing. I've ended up creating a simple partial view model containing a single text property to get round this (and passing this in to partial view), as it was wasting too much time. – Andy Brown Mar 19 '21 at 11:01
18

Here is an extension method that will convert an object to a 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;
}

You can then use it in your view like so:

@Html.Partial("_MyPartial", new
{
    Property1 = "Value1",
    Property2 = "Value2"
}.ToViewDataDictionary())

Which is much nicer than the new ViewDataDictionary { { "Property1", "Value1" } , { "Property2", "Value2" }} syntax.

Then in your partial view, you can use ViewBag to access the properties from a dynamic object rather than indexed properties, e.g.

<p>@ViewBag.Property1</p>
<p>@ViewBag.Property2</p>
Chris Haines
  • 6,207
  • 5
  • 47
  • 61
  • Where should I put this `ToViewDataDictionary` static method? Everywhere I try to put it I get some kind of a compiler error. – Csaba Toth Oct 14 '18 at 06:36
  • 2
    @CsabaToth this is an extension method. Just place it in a static class and then reference that class in your view. – Chris Haines Oct 15 '18 at 07:53
6

For Asp.Net core you better use

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

So for example

@foreach (var item in Model)
{
   <partial name="_MyItemView" model="item" />
}
Gerard
  • 2,311
  • 1
  • 23
  • 35