Question

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.AsEnumerable();
}
Was it helpful?

Solution

it all depends on how you are setting up the drop down. I would recommend not using viewbag/data for sending the information. Put it in your viewmodel build the list how you are building it but save it to the view model

viewModel.TrackList = TrackList;

then on your view

@Html.DropDownListFor(x => x.Track, Model.TrackList)

the for helper will override the selected property. In this case to set the selected value you would need to set viewModel.Track to the value you want selected and the for helper would then be set accordingly. Let me know if you have any questions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top