0

I have this razor's statement in a view that create a dropdownlist. How can I mantain the selected value after a post-back to the server ? I set the selected value in a ViewBag property once on the server but I was not able to set the value again.

@Html.DropDownListFor(x => x.Languages, Model.Languages.Select(x => new SelectListItem { Text = x.Descr, Value = x.Code }), "Choose an option", new { htmlAttributes = new { @class = "form-control" } })

Thank you so much for the help.

  • The answer to this question was what actually worked in my case (hard coded id and name of `` to save database call) – joym8 May 21 '20 at 17:25

1 Answers1

2

When posting back to view in mvc just pass return view(model). model is the parameter you are catching on controller from view.

Update:
You can also do that view jquery if you have viewbag.Dropdown value like this: @if (@Model !=null ) { $("#DropdownId").val("@viewBag.DropdownListNameFromController") }