html.dropdownlistfor

Bind Html.DropDownList with static items

非 Y 不嫁゛ 提交于 2019-11-27 04:25:43
问题 I have to bind an Html.DropDownList with just two items statically. Text="Yes" Value="1" Text="No" Value="0" The important thing is that, I have to set the text and value fields. How can I do this? 回答1: It is a best practice not to create the SelectList in the view. You should create it in the controller and pass it using the ViewData. Example: var list = new SelectList(new [] { new { ID = "1", Name = "name1" }, new { ID = "2", Name = "name2" }, new { ID = "3", Name = "name3" }, }, "ID",

Add attribute to select list option

五迷三道 提交于 2019-11-27 02:39:41
问题 I have a list of items in a drop down list within a Razor view. In the database each item has 3 values associated with it - the database id, short name (for display), and long name (for passing to a service). The drop down must show the short name, so I'm populating the drop down with the database id as the value and the short name as the text. However when a user selects an item I need pass the long name as a query parameter to a search service using jQuery, e.g when Cortina is seleted "Ford

Load PartialView with using jquery Ajax?

江枫思渺然 提交于 2019-11-27 01:42:33
问题 PartialView @model OsosYeni23072012.Models.TblMeters <h3> Model.customer_name </h3> <h3> Model.meter_name </h3> Controller [HttpGet] public ActionResult MeterInfoPartial(string meter_id) { int _meter_id = Int32.Parse(meter_id); var _meter = entity.TblMeters.Where(x => x.sno == _meter_id).FirstOrDefault(); return PartialView("MeterInfoPartial", _meter); } Razor @Html.DropDownList("sno", new SelectList(Model, "sno", "meter_name"), "-- Select Meter --", new { id = "meters"}) @Html.Partial(

MVC DropDownList SelectedValue not displaying correctly

依然范特西╮ 提交于 2019-11-26 17:47:00
问题 I tried searching and didn't find anything that fixed my problem. I have a DropDownList on a Razor view that will not show the the item that I have marked as Selected in the SelectList. Here is the controller code that populates the list: var statuses = new SelectList(db.OrderStatuses, "ID", "Name", order.Status.ID.ToString()); ViewBag.Statuses = statuses; return View(vm); Here is the View code: <div class="display-label"> Order Status</div> <div class="editor-field"> @Html.DropDownListFor

onchange event for html.dropdownlist

三世轮回 提交于 2019-11-26 15:52:38
问题 I am trying to trigger an action method for onchange event for dropdownlist, how can I do this without using jquery onchange. @Html.DropDownList("Sortby", new SelectListItem[] { new SelectListItem() { Text = "Newest to Oldest", Value = "0" }, new SelectListItem() { Text = "Oldest to Newest", Value = "1" }}) Thanks 回答1: You can do this @Html.DropDownList("Sortby", new SelectListItem[] { new SelectListItem() { Text = "Newest to Oldest", Value = "0" }, new SelectListItem() { Text = "Oldest to

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

jQuery Chosen Dropdown validation client site doesn&#39;t work

扶醉桌前 提交于 2019-11-26 11:40:32
问题 My model class contains of required lookup value which is a lookup-based record: [Required] [DisplayName(\"Business Unit\")] public string value { get; set; } [Required] //not working on client side? [DisplayName(\"Business Group\")] public int id_businessgroup { get; set; } View: <div class=\"editor-label\"> @Html.LabelFor(model => model.value) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.value) @Html.ValidationMessageFor(model => model.value) </div> <div class=\"editor

Populating a razor dropdownlist from a List<object> in MVC

北慕城南 提交于 2019-11-26 11:35:57
I have a model: public class DbUserRole { public int UserRoleId { get; set; } public string UserRole { get; set; } } public class DbUserRoles { public List<DbUserRole> GetRoles() { BugnetReports RoleDropDown = new BugnetReports(); List<DbUserRole> Roles = new List<DbUserRole>(); DataSet table = RoleDropDown.userRoleDropDown(); foreach (DataRow item in table.Tables[0].Rows) { DbUserRole ur = new DbUserRole(); ur.UserRole = Convert.ToString(item["UserRoleName"]); ur.UserRoleId = Convert.ToInt32(item["UserRoleID"]); Roles.Add(ur); } return Roles; } } And here is the Controller that loads the view

Fill drop down list on selection of another drop down list [closed]

坚强是说给别人听的谎言 提交于 2019-11-26 07:58:09
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I am using MVC and I am still new with MVC. Can anyone tell me if you can fill a drop down list with data on selection of another drop

Populating a razor dropdownlist from a List<object> in MVC

僤鯓⒐⒋嵵緔 提交于 2019-11-26 03:26:28
问题 I have a model: public class DbUserRole { public int UserRoleId { get; set; } public string UserRole { get; set; } } public class DbUserRoles { public List<DbUserRole> GetRoles() { BugnetReports RoleDropDown = new BugnetReports(); List<DbUserRole> Roles = new List<DbUserRole>(); DataSet table = RoleDropDown.userRoleDropDown(); foreach (DataRow item in table.Tables[0].Rows) { DbUserRole ur = new DbUserRole(); ur.UserRole = Convert.ToString(item[\"UserRoleName\"]); ur.UserRoleId = Convert