dropdownlistfor

DropDownListFor with an item value 0 always selected

允我心安 提交于 2019-12-01 08:28:49
I have a problem getting asp.net and the razor engine to create a drop down list with the correct element selected. No matter what I try, if one of the values available is "0", that item will always be selected. What I am trying to create is a select for a rating that span from -2 to 2, with 0 being the middle value, but that should NOT be pre-selected. The user should be forced to make a decision and therefore the defualt value should be empty. Creating the dropdownlist with an option label like the code below shows does not leave the default as empty. This would work if the values were 1-5

DropDownListFor with an item value 0 always selected

混江龙づ霸主 提交于 2019-12-01 07:45:49
问题 I have a problem getting asp.net and the razor engine to create a drop down list with the correct element selected. No matter what I try, if one of the values available is "0", that item will always be selected. What I am trying to create is a select for a rating that span from -2 to 2, with 0 being the middle value, but that should NOT be pre-selected. The user should be forced to make a decision and therefore the defualt value should be empty. Creating the dropdownlist with an option label

How to use a ViewBag to create a dropdownlist?

别来无恙 提交于 2019-11-28 18:38:33
Controller: public ActionResult Filter() { ViewBag.Accounts = BusinessLayer.AccountManager.Instance.getUserAccounts(HttpContext.User.Identity.Name); return View(); } View: <td>Account: </td> <td>@Html.DropDownListFor("accountid", new SelectList(ViewBag.Accounts, "AccountID", "AccountName"))</td> ViewBag.Accounts contains Account objects which have AccountID , AccountName and other properties. I would like a DropDownList called accountid (so that on Form Post I can pass the selected AccountID) and the DropDownList to display the AccountName while having the AccountID as value. What am I doing

Value cannot be null. Parameter name: items (DrodownList)

被刻印的时光 ゝ 提交于 2019-11-26 21:03:52
I'm working with ASP.NET MVC4 and now I want to add a dropdownlist with data from my mysql database. This is what I do : In my view (Register.cshtml):` <div class="control-group"> @Html.LabelFor(m => m.DistrictID, new { @class= "control-label"}) <div class="controls"> @Html.DropDownListFor(model => model.DistrictID, new SelectList(ViewBag.Districts, "district_id", "district_name", Model.DistrictID)) </div> </div> In my Controller (AccountController): [AllowAnonymous] public ActionResult Register() { var districts = repository.GetDistricts(); ViewBag.Districts = districts; return View(new

What is the best ways to bind @Html.DropDownListFor in ASP.NET MVC5?

核能气质少年 提交于 2019-11-26 14:48:43
问题 I want to bind @Html.DropDownListFor from Model data without using Viewbag and look at many different examples on the web. But most of them use Viewbag or an extension method and I want a better approach to solve this problem. I tried the the following methods but it does not seem to work: @Html.DropDownListFor(m => m.LabId, new SelectList(Model.Lab, "Id", "Name"), "---- Select----", new { @class = "selectpicker" } ) Is it possible to bind @Html.DropDownListFor directly from Model without

Value cannot be null. Parameter name: items (DrodownList)

筅森魡賤 提交于 2019-11-26 07:50:08
问题 I\'m working with ASP.NET MVC4 and now I want to add a dropdownlist with data from my mysql database. This is what I do : In my view (Register.cshtml):` <div class=\"control-group\"> @Html.LabelFor(m => m.DistrictID, new { @class= \"control-label\"}) <div class=\"controls\"> @Html.DropDownListFor(model => model.DistrictID, new SelectList(ViewBag.Districts, \"district_id\", \"district_name\", Model.DistrictID)) </div> </div> In my Controller (AccountController): [AllowAnonymous] public

better way to load 2 dropdown in mvc

你离开我真会死。 提交于 2019-11-25 22:59:06
问题 This is how i am loading on page load state and city dropdown: My Controller method : This is the first method which is calling when page is loaded. public ActionResult Index() { var states = GetStates(); var cities = Enumerable.Empty<SelectListItem>(); ViewBag.States = states; ViewBag.Cities = cities; } private IEnumerable<SelectListItem> GetStates() { using (var db = new DataEntities()) { return db.States.Select(d => new SelectListItem { Text = d.StateName, Value =d.Id.ToString() }); } }