selectlist

ASP.NET MVC DropDownListFor does not honour SelectListItem.Selected

ⅰ亾dé卋堺 提交于 2019-12-08 19:42:28
问题 I am using DropDownListFor to render a dropdown list in a view. Somehow the rendered list does not select the SelectListItem with Selected set to true . In the controller action: var selectList = sortedEntries.Select(entry => new SelectListItem { Selected = entry.Value.Equals(selectedValue), Text = entry.Value, Value = entry.Id }); return View(new DropDownListModel { ListId = id, SelectList = selectList, OptionLabel = "Click to Select" }); In the view: <%= Html.DropDownListFor(m => m.ListId,

Change a select list display value upon selecting description

旧时模样 提交于 2019-12-08 05:36:09
问题 I would like a select list that does the following: When the select list is open, display the list of descriptions. When the user selects an item, the select list will close an only the value will be displayed. The reason for this is to conserve space as the values will be much shorter. I'm hoping that there's an answer in jQuery. Thanks. 回答1: Alright, I've worked it out: $("#selectList").focus(function() { var selectedOption = $(this).find("option:selected"); selectedOption.text

How to create a DropDownList from a LINQ query in MVC3?

雨燕双飞 提交于 2019-12-07 21:20:22
问题 I need to know how I could create a drop down list to represent all the categories in my "Categories" table. I have already extracted the names and the values of each category I need, using this LINQ query : var dbcontext = new LNQ2SQLDataContext(); var Q = from P in dbcontext.Categories where P.SUB_CAT == null select P; I can pass this "Q" to my view like this : In Controller : return View(Q); And in the View : @model IEnumerable<MyAppName.Models.Category> But I have no idea how to use @html

PHP Select List - Insert multiple values in database

谁说我不能喝 提交于 2019-12-07 13:26:38
问题 I'm trying to insert multiple values in the database using select list. What I got so far: HTML <form enctype="multipart/form-data" action="" method="post"> <select name="cars[]" multiple="multiple" style="width:300px"> <?php $getcars = mysql_query("SELECT cars_id, cars_name FROM car"); while ($row = mysql_fetch_assoc($getcars)) { $car_id = $row['cars_id']; $car_name = $row['cars_name']; ?> <option value="<?php echo $car_id ?>"><?php echo $car_name ?></option> <?php } ?> </select><br />

How to create a DropDownList from a LINQ query in MVC3?

亡梦爱人 提交于 2019-12-06 10:46:37
I need to know how I could create a drop down list to represent all the categories in my "Categories" table. I have already extracted the names and the values of each category I need, using this LINQ query : var dbcontext = new LNQ2SQLDataContext(); var Q = from P in dbcontext.Categories where P.SUB_CAT == null select P; I can pass this "Q" to my view like this : In Controller : return View(Q); And in the View : @model IEnumerable<MyAppName.Models.Category> But I have no idea how to use @html.DropDownListFor() to make a darn good drop down list out of the model. :| PLUS: I could make a

ASP.NET mvc: Moving items between two listboxes

末鹿安然 提交于 2019-12-06 08:17:46
Currently I have 2 listboxes binding data from a database on my view... I've used a custom Viewmodel and it is not strongly-typed. Here's the code: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ProjectenII.Models.Domain.StudentModel>"%> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> IndexStudents </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>IndexStudents</h2> <div class="editor-field"> <%: Html.ListBox("IndexStudentsNormal", Model

Result of LINQ Query in MVC SelectList as Value and Text - not working

醉酒当歌 提交于 2019-12-05 06:42:47
I am trying to use the result of a LINQ Query to populate a SelectList in a MVC 5 application. The LINQ query returns customer IDs. Model public partial class Pricelist { public int CustomerID { get; set; } public int SelectedCustomer { get; set; } public Pricelist(int customerID, int selectedCustomer) { } } View @Html.DropDownList("custList") Controller (1) var query = ((from s in db.Pricelists select s.CustId).Distinct()).ToList(); int i = 1; List<Pricelist> CustomerList = new List<Pricelist>(); foreach (var c in query) { int cust = c; Pricelist p = new Pricelist(i, cust); CustomerList.Add(p

Generic Enum to SelectList extension method

大憨熊 提交于 2019-12-05 06:17:13
I need to create a SelectList from any Enum in my project. I have the code below which I create a select list from a specific enum, but I'd like to make an extension method for ANY enum. This example retrieves the value of the DescriptionAttribute on each Enum value var list = new SelectList( Enum.GetValues(typeof(eChargeType)) .Cast<eChargeType>() .Select(n => new { id = (int)n, label = n.ToString() }), "id", "label", charge.type_id); Referencing this post , how do I proceed? public static void ToSelectList(this Enum e) { // code here } What I think you are struggling with, is the retrieval

.net MVC, SelectLists, and LINQ

▼魔方 西西 提交于 2019-12-05 03:33:06
I am new to using the Html.DropDownList in the MVC framework and am having a hard time understading how to select the data out my database to bind to the DropDownList. Is there an easy way to return a bindable list (such as a SelectList) from a standard LINQ query? The SelectList constructor takes an IEnumerable so all you need to do is pass the LINQ query to the constructor like so var query = from c in customers select c; var customerList = new SelectList(query, "CustomerId", "CustomerName"); You should do this in the Controller and have the SelectList in your ViewModel. You want to use the

Combine 2 fields in SelectList

不羁岁月 提交于 2019-12-05 03:22:09
问题 Currently I have SelectList that writes ID, and shows FirstName on the form. ViewBag.Person = new SelectList(db.Person, "ID", "FirstName"); How to concatenate FirstName and LastName into SelectList? Something like: ViewBag.Person = new SelectList(db.Person, "ID", "FirstName & LastName"); 回答1: Try something like this: ViewBag.Person = new SelectList((from s in db.Person select new { ID=s.ID, FullName = s.FirstName+ " " + s.LastName}), "ID", "FullName", null); Or Add a new property to your