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" })
Related
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.
I've done this a hundred times but not sure what is going on here. I have a DropDownListFor that I populate in the controller like so
var mktsegments = from p in db.ChannelMarketSegment
where (p.ChannelCode != "0")
select p;
ViewBag.Pendist = new SelectList(mktsegments, "Pendist", "Pendist");
And in the view, I am attempting to set the default value of this drop down list with the Pendist value.
EDIT: Pendist is a field that exists in each item pulled into mktsegments via the Linq query.
<div class="M-editor-label">
#Html.LabelFor(model => model.Pendist)<span class="req">*</span>
</div>
<div class="M-editor-field">
#Html.DropDownListFor(model => model.Pendist,
(SelectList)ViewBag.Pendist, new { onchange = "ChangeChannel()" })
</div>
However, all this does is set the first value in the list as the default value. If I try to add model => model.Pendist or Model.Pendist as the third paramter in the DropDownListFor like this
#Html.DropDownListFor(model => model.Pendist,
(SelectList)ViewBag.Pendist, model => model.Pendist, new { onchange = "ChangeChannel()" })
I either get the following errors
(for model => model.Pendist)
Cannot convert lambda expression to type 'string' because it is not a delegate type
(for Model.Pendist)
'Model' confilcts with the declaration 'System.Web.Mcv.WebViewPage<TModel>.Model'
You are conflicting with the MVC ModelState. When creating Drow down lists, make sure that your property that holds the selected value is not named the same thing as your list of objects. Also, do not use a lambda for the default value, but rather just use the model item directly.
#Html.DropDownListFor(model => model.Pendist, (SelectList)ViewBag.PendistList,
Model.Pendist, new { onchange = "ChangeChannel()" })
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.
I have a dropdownlist where a user can select a care provider. In the database the value for this is null however in the application, a value is selected. What do I need to do to have the null value appear as a blank? I thought this was the default behavior. I changed to using strongly typed lists in my view model instead of the viewbag and this may have broken at that time.
Here is the view markup:
<div class="editor-label">
#Html.LabelFor(model => model.PsychologistId, "Psychologist")
</div>
<div class="editor-field">
#Html.DropDownListFor(x => x.PsychologistId, Model.ListPsychologists)
#Html.ValidationMessageFor(model => model.PsychologistId)
</div>
Here is the property from the view model:
[DisplayName("Psychologist")]
public Nullable<int> PsychologistId { get; set; }
Here is the relevant part of my controller:
model.ListPsychologists = new SelectList(XXX, "Id", "DisplayName");
return this.View(model);
where XXX is just the LINQ expression with filtering and sorting criteria. It's been omitted for clarity and space.
The model passed from the controller to the view has PsychologistId being null. And the SelectedValue property on model.ListPsychologists is null.
if PsychologistId is an int, it will assign 0 value to it since int is not a nullable type.
Show your model and controller if my assumption above is not true.
Does your SelectList contain entry with Id==null? I doubt that. And that means that you will have no select list entry matching your value, so browser will select first one available.
Solution: explicitly add entry with Id = null to your SelectList.
I've had these issues before, and what I did to fix it was send the base collection of what I want to fill the dropdown with in the viewmodel, instead of a SelectList. So:
model.ListPsychologists = XXX;
Then in the view:
#Html.DropDownListFor(x => x.PsychologistId, new SelectList(Model.ListPsychologists, "Id", "DisplayName", Model.PsychologistId))
Please verify the SelectList constructor overload I used in MSDN, I'm typing this from memory. Basically you give it the collection of items to use in the SelectList, then the name of the text and value properties (just like you did in your action method code), and the fourth parameter is the value to set the drop down to.
Edit: Also, there is an overload for the DropDownListFor to add a default item in the menu. Like, "Select One".
I have a table(Collection) of Kinds And in Edit View (in MVC) Need this Kinds in DropDown, Also I Need The DropDown Select Old Kind as Default, But there is some Problems.
This is my Select List:
var Kinds = db.Kinds
.Where(n => n.Member.Id == owner.Id)
.Select(k =>k.Title);
var selectList = new SelectList(Kinds);
Also The Default Kind(Value) is :
var Selection= db.Kinds
.Single(k => k.Numbers
.Any(no => no.Id == Model.Id)).Title;
And this is My DropDown:
#Html.DropDownListFor(model => model.NumberKind.Title, selectList)
When I Define DropDown Like following the new member added at the first of dropdown items:
#Html.DropDownListFor(model => model.NumberKind.Title, selectList, Selection)
For ex if the items be [Mobile, Home, Work]
And Selection is [Home]
With above Code the items will be like this: [Home, Mobile, Home, Work]
but I don't need new item, I just want Select Home From The Main Items
How Can I do this?
Thank you
You're currently providing a "default" value for your drop down list. The intended purpose of the dropdownlistfor constructor you are using would be to add an item like "Select Phone" to your drop down list.
Use simply:
#Html.DropDownListFor(model => model.NumberKind.Title, selectList)
Similar question here for reference: DropDownList setting selected item in asp.net MVC