Tuesday, 20 August 2013

How can I pass the selected value of SelectList to my Action?

How can I pass the selected value of SelectList to my Action?

In my ASP.NET MVC 4 project I have a View that has this piece:
<td>
@Html.DropDownList("SelectedValue", ViewBag.SoftwaresList as
SelectList, "Add New Software")
</td>
<td>
No Description
</td>
<td>
@Html.ActionLink("Add", "AddSoftware", new { pDeviceId = Model.Id,
pSoftwareId = Model.SelectedValue})
</td>
My Model has an object property "SelectedValue". What I am trying to do
is, when a user clicks the Link Add, I pass the Device Id (works fine,
because this is straight from the Model being passed into the the View)
AND the Selected Id from the DropDownList. The DropDownList is a
collection of a different Model, but it contains an Id as well.
I've tried the following with no luck:
@Html.ActionLink("Add", "AddSoftware", new { pDeviceId = Model.Id,
pSoftwareId =
((ViewBag.SoftwaresList as SelectList).SelectedValue as
SoftwareModel).Id})
It's worth noting: The DropDownList is populated with my SoftwareModel's
correctly, however, I am having no luck passing that additional value from
the selected value to to the Action in the Controller.
In fact, what I see in Chrome hover over is:
someURL\AController\AddSoftware?pDeviceId=2
Where I would expect it to be something like:
someURL\AController\AddSoftware?pDeviceId=2&pSoftwareId=5

No comments:

Post a Comment