ViewBag update does not update the view - asp.net-mvc

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})

Related

Html.DropDownListFor Selected=true reset automatically in index view

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" })

Include 'ALL' option in dropdownlist bind using ViewBag

I have bind the dropdownlist in view by Viewbag from controller as following :
ViewBag.test = from p in _userRegisterViewModel.GetEmpPrimary().ToList().Where(a => a.UserType_id != Convert.ToInt32(Session["loginUserType"].ToString()))
select new
{
Id = p.EmpId,
Name = p.First_Name.Trim() + " " + p.Last_Name.Trim()
};
In view I have bind as following :
#Html.DropDownListFor(model => model.EmpId, new SelectList(#ViewBag.test, "Id", "Name"),
new { #class = "form-control", id="ddlEmp" })
Now i want to Insert "ALL" and "--Select--" in this dropdownlist.. How can i do this..
Can anyone help me to do this..
Thanks in advance..
You can add a null option to the dropdownlist by using one of the overloads of DropDownlistFor() that accepts a optionLabel, for example
#Html.DropDownListFor(m => m.EmpId, new SelectList(#ViewBag.test, "Id", "Name"), "--select--", new { #class = "form-control", id="ddlEmp" })
which will generate the first option as <option value="">--select--</option>
However, if you want to include options with both "--select--" and "ALL" you will need to generate you own IEnumerable<SelectListItem> in the controller and pass it to the view. I would recommend using view model with a IEnumerable<SelectListItem> property for the options, but using ViewBag, the code in the controller would be
List<SelectListItem> options = _userRegisterViewModel.GetEmpPrimary()
.Where(a => a.UserType_id != Convert.ToInt32(Session["loginUserType"].ToString()))
.Select(a => new SelectListItem
{
Value = a.EmpId.ToString(),
Text = a.First_Name.Trim() + " " + a.Last_Name.Trim()
}).ToList();
// add the 'ALL' option
options.Add(new SelectListItem(){ Value = "-1", Text = "ALL" });
ViewBag.test = options;
Note that I have given the ALL option a value of -1 assuming that none of your EmpId values will be -1
Then in the view, your code to generate the dropdownlist will be
#Html.DropDownListFor(m => m.EmpId, (Ienumerable<SelectListItem>)ViewBag.test, "--select--", new { #class = "form-control" })
Not sure why your wanting to change the id attribute from id="EmpId" to id="ddlEmp"?
Then in the POST method, first check if ModelState is invalid (if the user selected the "--select--" option, a value of null will be posted and the model will be invalid), so return the view (don't forget to reassign the ViewBag.test property).
If ModelState is valid, then check the value of model.EmpId. If its -1, then the user selected "ALL", otherwise they selected a specific option.

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 to Create an Empty Html.DropDownlistFor & how to disable Html.TextBox

I have the following code inside my asp.net mvc view:-
#Html.DropDownListFor(model => model.FirewallCustomer.CustomerVLANSID,null, null, null)
#Html.ValidationMessageFor(model => model.FirewallCustomer.CustomerName)
<div><span class="f">VLAN IP</span> #Html.TextBox("VLANIP", new { disabled = "disabled"})</div>
but the above code will raise an error on the DropDownListFor:-
There is no ViewData item of type 'IEnumerable' that
has the key 'FirewallCustomer.CustomerVLANSID'.
and for the TexBox it will show the disabled = "disabled" inside the textbox body, instead of disabling it. can anyone adivce on how to solve my dropdownlist & my textbox problems???
Thanks`
For textbox
#Html.TextBox("VLANIP", new { disabled = "disabled"}) change to
#Html.TextBox("VLANIP","Your Value", new { disabled = "disabled"}) . I would like to advise you change disabled = "disabled" to #readonly = "readonly".
For dropdown list, if you wanna create a empty dropdownList for, you have to convert you FirewallCustomer.CustomerVLANSID to SelectListItem and add empty value for it. Please post your action for this view, i can help you. Should try
Html.DropDownListFor(model => model.FirewallCustomer.CustomerVLANSID,Enumerable.Empty< SelectListItem >(),null)

Resources