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);
}
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
And then binding the model with Partial View like this
and the using its value in Partial View like this
You can also play with other datatypes like array, int or more complex data types like IDictionary or something else.
Hope it helps,
Here is an extension method that will convert an object to a ViewDataDictionary.
You can then use it in your view like so:
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.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:
It will actually pass your model as an implicit parameter, the same as if you were to call:
Now, in order for your partial to actually be able to use this, though, it too needs to have a defined model, for example:
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
And then:
As with the model, Razor will implicitly pass your partial the view's
ViewData
by default, so if you hadViewBag.Id
in your view, then you can reference the same thing in your partial.For Asp.Net core you better use
So for example