Html.DropDownListFor Selected=true reset automatically in index view - asp.net-mvc

I’m binding #html.DropdownListFor control via modal in mvc page.
My code as below in cshtml page,
#Html.DropDownListFor(a => a.stateId, Model.statelist, new { #id = "StateList", #class = "form-control" })
in above Model.Statelist is of type List<SelectListItem>
I checked the Model.statelist have 2nd item Selected=true by debugging code.
But after render page on browser, selecteditem goes to first item.
Can any one Suggest me.

I think you need to make the following change. I did notice, for first parameter you have used 'a', while for second parameter list you have have "Model".
#Html.DropDownListFor(model => model.stateId, model.statelist, new { #id = "StateList", #class = "form-control" })

Related

ViewBag update does not update the view

I have the following ViewBag:
ViewBag.AuthObject
I'm refering this ViewBag in on of the drop-down s follows:
#Html.DropDownListFor(m => m.AuthId, ViewBag.ViewBag != null ?
(SelectList)ViewBag.AuthObject: Enumerable.Empty<SelectListItem>(),
"-- Please select --", new { #class = "form-control" })
Also, this is ViewBag is changing during the selection of another drop-down above the above one.
It seems that the dropdown for AuthId does not populate even after the ViewBag changes.
It looks like your referencing a different ViewBag than ViewBag.AuthObject, unless you're setting them equal at some point.
#Html.DropDownList(m => m.AuthId, (SelectList)ViewBag.AuthObject, "-- Please select --", new {#class = "form-control})

EditorFor displays perfectly but doesn't save accordingly

I am trying to use EditorFor to enter values into a list within a view model. I don't want every element in the list to allow edits though. The method of determining which element in the list can be edited is by using an additional list in my view model. The two lists line up perfectly so at the i'th position of one list exactly corresponds to the i'th position of the other. The code is as follows:
<div class="form-group">
#Html.Label("Inputs", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#for (int i = 0; i < Model.InputList.Count(); i++)
{
#Html.Label(i + 1 + ") " + Model.ProtocolSteps[i].Description)
if (Model.ProtocolSteps[i].InputNeeded.Value)
{
#Html.EditorFor(model => model.InputList[i], new { htmlAttributes = new { #class = "form-control" } })
}
<br />
}
</div>
</div>
The ProtocolSteps list determines if an editorfor template should be displayed. If it should, the editorfor input is stored into the InputList. This code works perfectly without the if statement (i.e. all values are stored to the correct location in InputList). With the if statement, the view is displayed perfectly, but values are not stored into InputList (usually one value is stored into the list, but not all). Any help is appreciated with why the if statement is messing things up.
Edit
Took this offline and we found that ASP.NET needed it to always write out the value, so adding a hidden field seems to work. But if anyone happens by this post, it'd be nice if you could explain why this is required.
#Html.Label(i + 1 + ") " + Model.ProtocolSteps[i].Description)
if (Model.ProtocolSteps[i].InputNeeded.Value)
{
#Html.EditorFor(model => model.InputList[i], new { htmlAttributes = new { #class = "form-control" } })
}
else
{
#Html.HiddenFor(model => model.InputList[i]);
}
Was just watching a microsoft video about this, they said that when using the #Html helpers within an iteration, you need to refer directly to the list, and not the value passed into the lambda expression. So, try changing to:
#Html.EditorFor(i => Model.InputList[i], new { htmlAttributes = new { #class = "form-control" } })
EDIT
Looks like a similar problem over here.
How to use EditorFor inside a foreach
I updated my previous entry to match, but I haven't tested this. Seems like the other guy did cause it got a lot of upvotes.

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.

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.

Resources