I am trying to access the Route value of id from inside the PartialView. I have a Post, Comments scenario. And I want my Comments partialview to have access to the PostId.
The Url looks like the following:
Posts/Detail/2
Now, how can I access the value 2 inside the Partial View?
UPDATE:
I changed my CommentsController to the following:
public ActionResult Add(string id,string subject,string name,string body)
{
}
But now when I debug it does not even step into the Add method.
UPDATE 2:
My partial view looks like the following:
<div class="title">Add Comment</div>
@using (Html.BeginForm("Add","Comments",FormMethod.Post))
{
@Html.Label("Subject")
@Html.TextBox("subject")
<br />
@Html.Label("Name")
@Html.TextBox("name")
<br />
@Html.Label("Body")
@Html.TextBox("body")
<input type="submit" value="submit" />
}
And here is the Controller:
public ActionResult Add(string id, string subject, string name, string body)
If I remove id from the above controller action then it works.
You could fetch it from
RouteData:In WebForms:
In Razor:
After showing your code it seems that in your form you are not passing the id. So modify it like this:
or use a hidden field if you prefer:
Now you should get the id in your
Addcontroller action.