DropDownListFor is not selecting existing value - asp.net-mvc

I have a dropdownlist for
#Html.DropDownListFor(model => f.MovieID, (SelectList)ViewBag.MoviesList, "--Select One--", htmlAttributes: new { #class = "form-control" })
In the controller I set the ViewBag.MovesList to a SelectList. It is a valid Select List. I'm trying to reuse the same select list for multiple items in a foreach loop. However, when it is rendered the existing MovieID value is not being selected in the drop down list. I have seen many similar questions but none of their root causes I am seeing in mine. Also, based on other questions simimar, this is slightly different then others because it is inside of a loop.

Try something like below:
#Html.DropDownListFor(model => model.MovieID, new SelectList((IEnumerable)ViewBag.MoviesList, "Value", "Text", Model.MovieID), new { #class = "form-control" })
Where Value is what you want to pass to the action invoked and Text is what you want to show in the dropdown.

Related

ASP>NET MVC concatenate two details onto dropdown list

If I'm using this viewbag
ViewBag.employee = (from e in db.Employees
select e.FullName).Distinct();
to use in this dropdown list
<div class="form-group">
#Html.LabelFor(model => model.EmployeeID, "EmployeeID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("EmployeeID", new SelectList(ViewBag.employee), "--Select--")
#Html.ValidationMessageFor(model => model.EmployeeID, "", new { #class = "text-danger" })
</div>
</div>
Is there a way to concatenate on another detail?
For example EmployeesID so that when you use the dropdown list, the EmployeeID will appear beside their name? I need this feature as some employees might have the same name.
One way to get what you've asked for is to use
select e.FullName + " (" + e.ID + ")"
to concatenate the values together at source in the query.
And remove the Distinct() as it's unnecessary.
P.S. As a broader point, your dropdown list really ought to be using the ID to identify the selected item anyway. So your SelectList should be passed a list of SelectListItem (these can hold both an ID and a display value) instead of a simple list of strings. That way when the form is submitted back to the server you can be certain exactly which employee was selected. You want Razor to render options like <option id="2">Joe Bloggs (2)</option> so that "2" will be the value submitted back to the server. The display value is just for the user and isn't very useful when sending the data back. If you do some searching you'll soon find examples of creating and using SelectListItems.

Asp.Net Core HTML Helper does not select the value it is provided

I am trying to add a Select List to my view. In the razor template I have:
#foreach (var item in Model.ProjectRecordGroupParsed)
{
<div>
<!-- Code: #item.Code -->
#Html.DropDownList("ProjectRecordGroup", new SelectList(AllProjectRecordGroups, "Code", "Name", item.Code), "Select Project Record Group", htmlAttributes: new { #class = "form-control" })
</div>
}
The result of this is a drop down with the values from AllProjectRecordGroup which does contain this option:
<option value="MARKPROJECT">MARK Project</option>
and the comment line contains this:
<!-- Code: MARKPROJECT -->
So, I am wondering what is going on here? Why is the value not selected?
To make this work, I had to change this line to make the ID unique:
#Html.DropDownList("ProjectRecordGroup", new SelectList(
It now works with this change:
#Html.DropDownList($"ProjectRecordGroup{item.Code}", new SelectList(
where AllProjectRecordGroups come from, if i understood this class populating this selectbox right?? maybe you need return this from controller with data, give more details please, and i trying help you

Razor mutliselect dropdown- Set values as selected in the multiselect dropdown in ASP.Net MVC

I need a way to set already selected values in a multi select dropdown in a edit view in a web application (ASP.NET, Mvc)
<div class="form-group">
<label>Choose Categories</label>
#Html.DropDownListFor(model => model.SelectedValues, new MultiSelectList(Models.GetCategoryList().OrderBy(c => c.Value), "Key", "Value"), "Select Category", new { #class = "chzn-select form-control", multiple = "multiple" })
</div>
this is view code, here I passed selected ids in to selected values array.(when I return values from DB , assign those in to a array then assign to here.
I need a edit view like this
please anyone suggest way to do this
Thank you.
To generate this view you can use select2 jquery plugin. Check out the following link.
https://select2.github.io/examples.html#multiple

How to display empty dropdown in view page

I have my get and post methods . I populate my data during post methods based on some value. When i try to run the program it gives me a here is no ViewData item of type 'IEnumerable' that has the key because there is no data in the dropdown. How can i show empty dropdown and bind the same during post method .
The DropDownListFor template must be provided with an IEnumerable to work properly. If you want an empty list, the best way to provide an empty IEnumerable is to use the Enumerable.Empty<> static method.
Your code would then look like this:
#Html.DropDownListFor(model => model.Name, Enumerable.Empty<SelectListItem>(), "-- Select Name --", new { #class = "form-control" })

How can I show default message if no data and selected value if data available in dropdownlist in MVC4

I am binding the dropdownlist with some data and display the selected data in dropdownlist also. That works fine with the following code.
#Html.DropDownListFor(modelItem => item.UniformCommentSelected,
new SelectList(item.UniformComment),
item.UniformCommentSelected)
Now I want to add default message "Select" for dropdownlist.
I also want to show the selected value in dropdownlist if any and "Select" if not.
If I put "Select" as third parameter in DropDownListFor then it display "Select" for all users. If I put "Select" as 4th parameter in dropdownlist then nothing happens.
Can any one help me?
Try to use the following code.
#Html.DropDownListFor(modelItem => item.UniformCommentSelected, new SelectList(item.UniformComment),item.UniformCommentSelected == null ? "---Select---" : item.UniformCommentSelected)
in my project i fill my dropdown list like this it's below
#Html.DropDownListFor(model => model.CountryId, new SelectList(Model.Countries, "ID", "Name"), "select", new { #ID = "ddlCountry", #class = "text", Style = "width: 150px;", onchange = "javascript:cascadingdropdown();" })
i think this will help you
I usually bind to a collection of SelectListItem's, and manually add the blank of "Please select one..." option to that collection. In the controller (or in the view right before rendering the droplist), I set .Selected = true for the selected option.

Resources