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);
}
This question is related to
asp.net-mvc
asp.net-mvc-4
razor
partial-views
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,
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" />
}
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>
Source: Stackoverflow.com