Mohamed Fawas
Mohamed Fawas

Reputation: 37

mvc3 how to pass selected item of a dropdown list from one view to another viaction link

I have a drop down list in Razor View

@Html.DropDownList("ddlAccount", items)

this drop down list is binded with items.

I want to pass this selected list item to 'Create' Method of "TaskManagement" Controller on click of action link.

@Html.ActionLink("Create New Task", "Create", "KTTaskManagement")

Upvotes: 2

Views: 3625

Answers (2)

Anders
Anders

Reputation: 17554

And while your at it please use the typed equivalence

@Html.DropDownListFor(m => m.SelectedItemId, new
 SelectList(Model.Items, "ItemId", "ItemName", Model.SelectedItemId))

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038940

The semantically correct way to handle this case is to use a form instead of an anchor:

@using (Html.BeginForm("Create", "KTTaskManagement"))
{
    @Html.DropDownList("ddlAccount", items)
    <button type="submit">Create New Task</button>
}

This way the selected value of the dropdown will be automatically sent to the controller action:

public ActionResult Create(string ddlAccount)
{
    // the action argument will contain the selected value
    ...
}

Upvotes: 1

Related Questions