html.dropdownlistfor

MVC Multiple DropDownLists from 1 List<SelectListItem>

独自空忆成欢 提交于 2019-12-30 08:28:54
问题 Background: I have 4 dropdown lists on my page that all use one List of SelectListItem to pull data from. All 4 of these dropdowns will always have the same exact elements in them. Each dropdown has an empty element at the top. Problem: If I do not select an item from the list that renders 2nd, when the page loads the 2nd list automatically selects the item that is selected in the 1st list. This is because (I think) the list has a SelectListItem where Selected = true , and it just uses that

DropDownListFor - does not select “Selected” value

眉间皱痕 提交于 2019-12-30 07:59:26
问题 Another one of these questions regarding DropDownListFor not selecting the "Selected" value. Here is code: Model: public class CreateEditAccountModel { [Required] [Display(Name = "Permission")] public int PermissionId { get; set; } public IEnumerable<SelectListItem> Permissions { get; set; } } Controller: [HttpGet] [Authorize] public ActionResult EditAccount(int id) { CreateEditAccountModel model = new CreateEditAccountModel(); model.Permissions = PermissionsAll(); return View(

DropDownListFor - does not select “Selected” value

谁说胖子不能爱 提交于 2019-12-30 07:59:11
问题 Another one of these questions regarding DropDownListFor not selecting the "Selected" value. Here is code: Model: public class CreateEditAccountModel { [Required] [Display(Name = "Permission")] public int PermissionId { get; set; } public IEnumerable<SelectListItem> Permissions { get; set; } } Controller: [HttpGet] [Authorize] public ActionResult EditAccount(int id) { CreateEditAccountModel model = new CreateEditAccountModel(); model.Permissions = PermissionsAll(); return View(

How to create ASP.Net MVC DropDownList with required validation

随声附和 提交于 2019-12-29 07:17:12
问题 I working with mvc 5. I am loading data from Database Using ORM and fill a drop down list from the controller, like this. ViewBag.Country_id = new SelectList(_db.Countries, "Country_id", "Description"); As i wanted an empty field first I am doing this in my HTML. <div class="form-group"> @Html.LabelFor(model => model.Countries, "Country", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("Country_id", null, htmlAttributes: new { @class =

Binding to a DropDownList in MVC3

不羁岁月 提交于 2019-12-29 01:56:05
问题 Previously, I was using this in my model public List<SelectListItem> StatusList { get; set; } public string Status { get; set; } and this in my view to bind to the Dropdownlist @Html.DropDownListFor(model => model.Status, Model.StatusList, "") but if I want to use public List<ICode> StatusList { get; set; } where ICode is as below public interface ICode { int Id { get; set; } ICodeGroup CodeGroup { get; set; } string ShortDescription { get; set; } string LongDescription { get; set; } } What

Include 'ALL' option in dropdownlist bind using ViewBag

半腔热情 提交于 2019-12-25 11:53:04
问题 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

sql query and dropdownlist

有些话、适合烂在心里 提交于 2019-12-25 05:35:25
问题 I have a drop down list which has the values of column's table. and I have the following statement in c#: string raf = string.Format("select Id from Customer WHERE email="dropdownlist1"); how can assign the value of the drop down list to email ? 回答1: You need to use .SelectedValue property to fetch the value of dropdown:- string raf = string.Format("select Id from Customer WHERE email={0}", dropdownlist1.SelectedValue); For fetching dropdown text:- string raf = string.Format("select Id from

Assign selectlist to dynamically created dropdown in MVC

廉价感情. 提交于 2019-12-25 05:32:14
问题 I have static select list as below var listItems = new SelectListItem[] { new SelectListItem(){Text="-- Please Select --",Value=""}, new SelectListItem(){Text="Death",Value="Death"}, new SelectListItem(){Text="Resignation",Value="Resignation"}, new SelectListItem(){Text="Retirement",Value="Retirement"}, new SelectListItem(){Text="Termination",Value="Termination"}, new SelectListItem(){Text="Transfer",Value="Transfer"}, }; ViewBag.DeletionReason = listItems; and assigning the same to ViewBag.

How to set default selected value for Html.DropDownListFor

你说的曾经没有我的故事 提交于 2019-12-25 05:31:35
问题 @if (HttpContext.Current.Request.Cookies[MyCookies.SelectedRegion] != null) { var defaultRegionValue = HttpContext.Current.Request.Cookies[MyCookies.SelectedRegion].Value; @Html.DropDownListFor(m => m.SelectedRionName, Model.RegionList, defaultRegionValue) } else { @Html.DropDownListFor(m => m.SelectedRionName, Model.RegionList, "Select") } I have the above sample in my view which is supposed to populate a Dropdownlist with a list of regions. Once a region is selected, i set that region as

Code First conventions confusion

久未见 提交于 2019-12-25 04:05:44
问题 Models: public class Status { public int Id { get; set; } } public class Podcast { public int Id { get; set; } public virtual Status Status { get; set; } } The Podcast table has the StatusId column, and this column is a foreign key. In this case I've got the following error message: Invalid column name 'Status_Id'. Why? - Many times I faced that articles with such examples. This is the first question. Ok, no problem - i've added an underscore character to these columns: Sttaus_Id and so on.