selectlist

Strongly typed view with a SelectList for DropDownList via ViewData: type mismatch on submit

我是研究僧i 提交于 2020-01-22 15:14:50
问题 I am trying to create a form in ASP.NET MVC2 RC 2 that is based on a calendar event object. The object has eventTypeId which is a System.Int32 that I need to populate with via a select list. The controller to create the initial view is: [WAuthorize] public ActionResult AddCalendarEvent() { CalendarEventTypesManager calendarEventTypesManager = new CalendarEventTypesManager(); ViewData["eventTypeId"] = new SelectList( calendarEventTypesManager.SelectAll(), "Id", "Type"); return View(); } The

Strongly typed view with a SelectList for DropDownList via ViewData: type mismatch on submit

霸气de小男生 提交于 2020-01-22 15:13:52
问题 I am trying to create a form in ASP.NET MVC2 RC 2 that is based on a calendar event object. The object has eventTypeId which is a System.Int32 that I need to populate with via a select list. The controller to create the initial view is: [WAuthorize] public ActionResult AddCalendarEvent() { CalendarEventTypesManager calendarEventTypesManager = new CalendarEventTypesManager(); ViewData["eventTypeId"] = new SelectList( calendarEventTypesManager.SelectAll(), "Id", "Type"); return View(); } The

Why does SelectList SelectedValue work on HttpGet but not on HttpPost?

随声附和 提交于 2020-01-15 06:10:17
问题 Using MVC3 I have found that setting a SelectList's selected value correctly renders the view on an HttpGet, but fails to render correctly on HttpPost. I have inspected the Model before they are forwarded to the View on HttpPost and they are correctly being updated, it just seems the View is not rendering the selected tag correctly. On HttpPost, the <select> is rendered exactly as it existed after any edits but before submission of the form. The m.SelectedWidgetId = 2; in the HttpPost method

Generic Enum to SelectList extension method

雨燕双飞 提交于 2020-01-13 10:08:41
问题 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

SelectListItem not setting via dropdown after extending SelectList class

二次信任 提交于 2020-01-03 06:09:14
问题 I am trying to extend the SelectListItem class to add another property called CardColor. However when I try to access the property in my controller I get NullReferenceException: Object reference not set to an instance of an object.... return View("StringView", c.IssueSelected.CardColor); Controller: [HttpPost] public async Task<ActionResult> CardCreate(UpdateCardFormOptions c) { return View("StringView", c.IssueSelected.CardColor); } View @using (@Html.BeginForm()) { <p><label>Issue Category*

SelectListItem not setting via dropdown after extending SelectList class

旧城冷巷雨未停 提交于 2020-01-03 06:08:49
问题 I am trying to extend the SelectListItem class to add another property called CardColor. However when I try to access the property in my controller I get NullReferenceException: Object reference not set to an instance of an object.... return View("StringView", c.IssueSelected.CardColor); Controller: [HttpPost] public async Task<ActionResult> CardCreate(UpdateCardFormOptions c) { return View("StringView", c.IssueSelected.CardColor); } View @using (@Html.BeginForm()) { <p><label>Issue Category*

Sort SelectList in specific order

谁说我不能喝 提交于 2020-01-02 15:45:10
问题 So I have a SelectList being generated in this method: public static SelectList HolidayDays() { SelectList retval = GenerateKeyValueList<HolidayCity>(HolidayCityHelper.GetFriendlyName, HolidayCity.NotSet); //sort list (NY, London....then the rest in order) return retval; } The GenerateKeyValueList<> method is defined here: public static SelectList GenerateKeyValueList<T>(Func<T, string> nameGetter, T itemToNoInclude) where T : struct, IComparable, IConvertible { List<SelectListItem> list =

SelectListItem with data-attributes

落爺英雄遲暮 提交于 2019-12-28 01:51:28
问题 Is there anyway to have a SelectList prepopulated on ViewModel with data-attributes ? I want to do @Html.DropdownListFor(m=> m.CityId, Model.Cities); so it generates code like : <select id="City" class="location_city_input" name="City"> <option data-geo-lat="-32.522779" data-geo-lng="-55.765835" data-geo-zoom="6" /> <option data-geo-lat="-34.883611" data-geo-lng="-56.181944" data-geo-zoom="13" data-geo-name="Montevideo" data-child=".state1" value="1">Montevideo</option> <option data-geo-lat="

What is wrong with my ASP.NET MVC SelectList?

假装没事ソ 提交于 2019-12-24 01:43:14
问题 I'm trying to use a SelectList one of my views, and its just not populating correctly. It gets the proper number of entries (4), but they all read System.Web.Mvc.SelectListItem . I fired up the debugger on the code, and saw some strangeness going on. I must be doing something wrong, but I don't quite see what. Code from the ViewModel: public SelectList DeviceTypes {get; private set;} .... var device_types = DataTableHelpers.DeviceTypes(); IEnumerable<SelectListItem> sl = device_types.Select(

Where should selectlist logic sit in ASP.NET MVC, view, model or controller?

喜夏-厌秋 提交于 2019-12-22 08:22:31
问题 I feel my question is close to this one, but I want a more general discussion on where code like this should sit. Asp.Net MVC SelectList Refactoring Question? I currently create my selectlists directly on my entity model, like so. public SelectList taskDeadlineTime { get { return new SelectList(TimeDictionary, "Value", "Key", this.getDeadlineTime()); } } This feels a bit wrong, as if I am performing view work, inside my model. However it does mean that I can just get the property, and my