Janice
Janice

Reputation: 25

How do i get the value from my drop down list

I am trying to return the selected item and when i debug my code, the view data track list is always null.

How can I assign the value to the view data?

public ActionResult EditParcel(int id)
    {
        Parcel parc = _abcSearchService.GetAbcParcel(id);
        List<SelectListItem> TrackList = new List<SelectListItem>
        {
            new SelectListItem { Text = "Processing", Value = "Processing", Selected = true},
            new SelectListItem { Text = "Out for delivery", Value = "Out for delivery"},
            new SelectListItem { Text = "Delivered", Value = "Delivered"},
        };
        ViewData["TrackList"] = TrackList;

[HttpPost]
    public ActionResult EditParcel(Parcel parcel)
    {
        try
        {
            var TrackingStatus = ViewData["TrackList"];
            parcel.Tracking_Status = (string)TrackingStatus;
            _abcSearchService.UpdateParcelDetails(parcel);
            return RedirectToAction("Parcel", new { id = parcel.ParcelID, Controller = "ABCParcel" });
        }
        catch
        {
            return View();
        }
    }

The drop down list appears on my edit action result, when i select an item from the drop down list to update the item the http post action result happens, the view data TrackList is null.

This is my view

   <div class="editor-label">
      <%: Html.LabelFor(model => model.Tracking_Status) %>
   </div>
   <div class="editor-field">
        <%: Html.DropDownList("TrackList", (List<SelectListItem>)ViewData["TrackList"])%>
        <%--<%: Html.EditorFor(model => model.Tracking_Status) %>--%>
        <%: Html.ValidationMessageFor(model => model.Tracking_Status) %>

Upvotes: 1

Views: 59

Answers (1)

Gautam Jain
Gautam Jain

Reputation: 6849

Use following line instead to get the selected value from drop down in your POST action.

var TrackingStatus = Request["TrackList"];

Upvotes: 1

Related Questions