Pavan Kumar Pavan Kumar - 1 year ago 146
Javascript Question

Dropdownlistfor is not fetching the selected value in MVC Control

I am appending the option value to the select element by using JavaScript and I have only one option value while posting the form.

Once my JavaScript added the option to select, my html code will be like below.

Html Script Image

My view Model is not getting the selected value in drop down list. Please help me.


@Html.DropDownListFor(m => m.PostAdCategoryID, new List<SelectListItem>
{new SelectListItem { Text = "", Value = "" } },
"Select Category", new Dictionary<string, object>
{ "class", "form-control col-lg-5" },
{ "id", "selectCategory" },
{ "data-toggle", "modal" },
{ "data-target", "#categoryModal" }


public class PostAdFormViewModel
// I have other objects and those are able to fetch the values (text boxes)
public int PostAdCategoryID { get; set; }


public ActionResult PostNewAd(PostAdFormViewModel viewModel)


Answer Source

To add just one option to the select input is not enough, you have to add the select option but it must have the selected attribute:

<option selected="selected" value="9">TVS</option>

For what you have posted so far the selected attribute is missing, so, though it have just one option this unique options is "visual selected" (because there is just one option) but it is not really selected (missing the attribute).

Looking at the information you have provided, the other things look like are ok. If still this does not work I suggest the see what/how is being sent to the server (F12 in browser).

Have a nice weekend. Regards.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download