DropDownList Error in MVC3 - asp.net-mvc

After adding the class to the html.dropdownlist am facing the below error.
Error:'System.Web.Mvc.HtmlHelper' does not contain a definition for 'DropDownList' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, string)' has some invalid arguments
<li>
#Html.LabelFor(m => m.BuildType)
#Html.DropDownList("BuildType", new { #class = "BuildType" })
#Html.ValidationMessageFor(m => m.BuildType)
</li>
<li>#Html.LabelFor(m => m.BuildMode)
#Html.DropDownList("BuildMode", new { #class = "BuildMode" })
#Html.ValidationMessageFor(m => m.BuildMode) </li>
<li>

Where are your list options? You need to provide a list of options via an IEnumerable<SelectListItem> object (see this overload).
So your model would have something like this:
public IEnumerable<SelectListItem> BuildModeOptions { get; set; }
And your view would pass the list into the helper:
#Html.DropDownList("BuildMode", Model.BuildModeOptions, new { #class = "BuildType" })
Or, since you're using the type-safe For versions on your other helpers, use DropDownListFor on this one as well:
#Html.DropDownListFor(m => m.BuildMode, Model.BuildModeOptions, new { #class = "BuildType" })
But keep in mind, Model.BuildMode is your selected value -- Model.BuildModeOptions is for your dropdown options.

You can use:
#Html.DropDownListFor(m => m.BuildType, (SelectList)Viewbag.YourSelectList, "Select Build type", new { #class = "BuildType" })
or
#Html.DropDownListFor(m => m.BuildType, Model.YourSelectList, "Select Build type", new { #class = "BuildType" })
When you use #Html.DropDownList, you specify a name for the dropdownlist... but you are missing the SelectList itself. I think that out of the box, the helper will try to use the name of the DropDownList (in your case "BuildType") to search in the ViewData collection for the SelectList.
When you use a #Html.DropDownListFor you don't use a name, but a lamda expression m => m.BuildType that will help you in same cases to not have harcoded names.
Your SelectList (the second parameter) can be grabbed from Viewbag or from a property in your Model.

The second parameter should be the list of items you want to show in the dropdown.
so It will look like :
#Html.DropDownListFor("BuildType", m.yourListofItems, new { #class = "BuildType" })

Related

Mvc 5 Multiselectlist selected not working

select element created with items in it, but "selected" properties not showing in html result. What i do wrong?
this code is working:
controller:
ViewBag.ProductTwistDirectionID = new SelectList(db.tblProductTwistDirection,
"ProductTwistDirectionID",
"ProductTwistDirectionName",
tblProduct.ProductTwistDirectionID);
view:
#Html.DropDownList("ProductTwistDirectionID",
null,
htmlAttributes: new { #class = "form-control" })
this code is not working: (selected items not selecting at render)
controller:
ViewBag.ProductUsePurposes = new MultiSelectList(db.tblProductUsePurpose,
"ProductUsePurposeID",
"ProductUsePurposeName",
tblProduct.ProductUsePurposes
.Select(x
=> x.ProductUsePurposeID)
.ToArray());
view:
#Html.DropDownListFor(model => model.ProductUsePurposes,
null,
new
{
#class = "chosen-select",
multiple = "multiple",
data_placeholder = "Choose use purposes",
style = "width: 400px;"
})
Your ViewBag member holding the select list cannot be named the same as your model property. ViewBag is used, in addition to ViewData and Request, to compose the ModelState object and whatever is in ModelState overrides the properties on your model.
Simply, you need to rename ViewBag.ProductUsePurposes to something like ViewBag.ProductUsePurposesChoices. Then, you should be fine.

Html.Dropdownlist selected value not working

In my project, Html.DropdownList could not display its selected value.It displays the initial value of the list.My controller and view codes are given below:
Controller:
public ActionResult Edit(int id = 0)
{
PERMISSION permission = permissionManager.Find(id);
ViewBag.MODULE_ID = new SelectList(moduleManager.GetAll(), "ID", "TITLE",permission.MODULE_ID);
return View(permission);
}
View :
#Html.DropDownList("MODULE_ID", (IEnumerable<SelectListItem>)#ViewBag.MODULE_ID, new { #class = "form-control" })
But if I write :
#Html.DropDownList("MODULE_ID", String.Empty)
It works fine.but I have to add the class="form-control".What should be solution?
UPDATE:
I have changed the ViewBag name from ViewBag.MODULE_ID to ViewBag.ModuleList. It may conflicts with the name of the dropdownlist. and now It works fine.
I have changed the ViewBag name from ViewBag.MODULE_ID to ViewBag.ModuleList. It may conflicts with the name of the dropdownlist. and now It works fine.
You are adding the form-control class in the wrong parameter. currently it is being added as the option label.
Use this:
#Html.DropDownList("MODULE_ID", (IEnumerable<SelectListItem>)#ViewBag.MODULE_ID, "-- Select --", new { #class = "form-control" })
On a side note, you should consider using a ViewModel class and doing away with both the magic string "MODULE_ID" and the ViewBag magic property. A ViewModel class allows you to strongly name things without casting, like this:
#Html.DropDownListFor(model => model.MODULE_ID, Model.Modules, "-- Select --", new { #class = "form-control" })
It's a subtle looking change but everything is compile time checked.
what works for me according to previous solution:
in controller:
ViewBag.VCenter = new SelectList(db.VirtualMachinesData.Where(x => x.IsVCenter == true).ToList(), "Id", "IPAddress","80"); ;
in View:
#Html.DropDownList("v", (IEnumerable<SelectListItem>)#ViewBag.VCenter, new { #class = "form-control" })
Important note: the name of dropdownlist -"v"- must be different from the name of viewbag -"VCenter"-, if same names it didn't work.

How do I add a ID to my textbox or dropdown list

In my view I have textbox and dropdown menu , I am trying to add an Class so that I can apply bootstrap themes to my fields. This is my code:
<div class="form-group">
#Html.LabelFor(model => model.ID, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("ID", String.Empty)
#Html.ValidationMessageFor(model => model.ID)
</div>
</div>
How do I declare class to this class="from-controller" I tried this #Html.DropDownList("ID", new {class="test"}) but I get an error. Can anyone tell me why.
The second argument of the DropDownList Method is IEnumerable<SelectListItem>, so your dropdown has to look like this:
#Html.DropDownList("ID", new SelectList(Model.CollectionOfIds, "DataValueField", "DataTextField"),
new { #class= "from-controller" })
First parameter is name not an ID. If you want to specify an id you have to do it same way as specifying a class.
#Html.DropDownList("ddlName", new SelectList(Model.Items, "Key", "Value"), "Please select", new { #class = "className", #id="yourId" })
new SelectList(Model.Items, "Key", "Value") will work for
Dictionary<int, string>
type
First :
class is a reserved keyword of c# language. When you want use a reserved keyword like variable name in C# you should put the # sign before variable name to the compiler ignore the fact that it's also a keyword.
in your case your must add # before class keyword
new {#class="test"})
Second :
There are not definition for Html.DropDownList(string, string) or Html.DropDownList(string, string, object)
the second parameter must be an IEnumerable
If you want set an empty list (I don't undersant why you set a String.Empy value) use Enumerable.Empty()
#Html.DropDownList("ID", Enumerable.Empty<SelectListItem>())

MVC4 SelectList not selected default object

My select list isn't selecting the default object being brought in through code.
I first create my SelectList like so:
public SelectList CreateSelectList(object objSelected = null)
{
return new SelectList(GetAll().OrderBy(s => s.NumericalValue), "PeriodID", "Name", objSelected);
}
My objSelected gets filled with a Guid that's associated with the PeriodID.
Inside my controller I define my viewbag variable to the new select list.
public ActionResult Edit(Guid id)
{
Classroom classroom = classroomRepository.GetByID(id);
ViewBag.PeriodID = periodRepository.CreateSelectList(classroom.PeriodID);
return View(classroom);
}
Then in my View here's how I'm displaying my SelectList:
<div class="control-group">
#Html.LabelFor(model => model.PeriodID, "Period", new { #class = "control-label" })
<div class="controls">
#Html.DropDownListFor(model => model.PeriodID, ViewBag.PeriodID as SelectList, String.Empty, new { #class = "span3" })
#Html.ValidationMessageFor(model => model.PeriodID)
</div>
</div>
You have two problems here. First, this:
#Html.DropDownListFor(model => model.PeriodID, ViewBag.PeriodID as SelectList,
String.Empty, new { #class = "span3" })
Change ViewBag.PeriodID to ViewBag.Periods or ViewBag.PeriodList. This is confusing, and there are a number of situations in which MVC will get confused if you use the same named object. It's just best to make sure everything is named differently.
Second, The SelectList class ignores the selecteditem member of the SelectListItem. It's not used at all. DropDownListFor will take the value of model.PeriodID and make it the selected value. However, I see in your code that those should be the same so I'm guessing the naming may be a factor here.

DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'CategoryTypeID'

I am using MVC. I want to pass the category data I entered from my view and passed to my Post/ Createcontroller, but it's not's letting me pass my categoryTypeID that I have selected from my dropdownlist.
Here is the error:
DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'CategoryTypeID'.
Here is my code:
My CreateController:
//
// POST: /Category/Create
[HttpPost]
public ActionResult Create(Category category)
{
if (ModelState.IsValid)
{
db.Categories.Add(category);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryTypes = new SelectList(db.CategoryTypes, "CategoryTypeID", "Name", category.CategoryTypeID);
return View(category);
}
My Create View
#model Haykal.Models.Category
<div class="editor-label">
#Html.LabelFor(model => model.CategoryTypeID, "CategoryType")
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.CategoryTypeID,
new SelectList(ViewBag.CategoryTypes as System.Collections.IEnumerable, "CategoryTypeID", "Name"),
"--select Category Type --", new { id = "categoryType" })
#Html.ValidationMessageFor(model => model.CategoryTypeID)
</div>
I faced this error. I was binding an object of the View Model:
editPanelViewModel.Panel = new SelectList(panels, "PanelId", "PanelName");
In the View, I created the ListBox like this:
#Html.ListBoxFor(m => m.Panel, new SelectList(Model.Panel, "PanelId", "PanelName"))
It should be like this in fact:
#Html.ListBoxFor(m => m.Panel, new SelectList(Model.Panel, "Value", "Text"))
You are defining your SelectList twice, in your controller as well as in your view.
Keep the view clean. Just the following would be enough in your case:
#Html.DropDownListFor(model => model.CategoryTypeID, (SelectList)ViewBag.CategoryTypes)
I have to admit that DropDownListFor is quite confusing in the beginning :)

Resources