html.dropdownlistfor

Setting default value to Html.DropDownListFor

╄→尐↘猪︶ㄣ 提交于 2019-12-11 03:49:07
问题 I am populating a dropdownlist in mvc using viewbag. i want to set a default value to this dropdownlist, if the page is not saved with any other value. Otherwise the selected item should be the one they select and save the page. this is how i populate my viewbag ViewBag.ListOfCountries = new SelectList(Db.Countries, "CountryCode", "CountryName"); this is the view side: <%: Html.DropDownListFor(m => m.OfficeAddressCountry, (IEnumerable<SelectListItem>)ViewBag.ListOfCountries, new{@class=

DropDownList not behaving as expected

有些话、适合烂在心里 提交于 2019-12-11 02:19:44
问题 I'm having trouble with my DropDownListFors that I'm hoping you can help me with. I'm guessing it's one of those things that you either know or you don't. The problem is I have a Countries table in my database which has a list of countries in it. The behaviour I would like from my drop down is to create a foreign key reference in my Address table pointing to the county selected in the drop down. The behaviour I'm getting is that the foreign key in my Address table is pointing to a new entry

Bind DropDownListFor with List on ViewModel

青春壹個敷衍的年華 提交于 2019-12-10 22:55:14
问题 I'm trying to do this: This is my ViewModel and Model: public class OpeningYearViewModel { public int OpeningYearId { get; set; } public string Description { get; set; } public List<Grade> GradesList { get; set; } } public class Grade { public int GradeId { get; set; } public string Name { get; set; } public int CurrencyId { get; set; } public int Cost { get; set; } } This is my Controller. I build a SelecList here and pass it to the view through the ViewBag OpeningYearViewModel viewmodel =

Use dropdownlistfor with foreach in Asp.Net MVC View?

送分小仙女□ 提交于 2019-12-10 17:13:40
问题 I have a View with a foreach loop for a list property of the model. Now, I want to be able to let the user set the value of each of the items in the list using a dropdownlist. But I'm not sure how to do that. I've used something like this when it is not in a foreach loop: @Html.DropDownListFor(model => model.Level, new SelectList(new[] { 1, 2, 3, 4, 5 }, Model.Level)) But how do I do this when I need to reference item.Level in the loop instead? Here's my View code: <div id="formDiv"> @using

MVC DropDownListFor and StringLength Attribute Not Playing Well together

不羁的心 提交于 2019-12-10 14:56:55
问题 My stringlength validation always fails on dropdownlists with a string value. Here is my model: [Required(ErrorMessage = "Required")] [StringLength(2, MinimumLength = 2)] [Display(Name = "Home Address State")] public string HomeAddressState { get; set; } Here is my view: @Html.DropDownListFor(model => model.HomeAddressState, new SelectList(ViewBag.States, "Value", "Text"), string.Empty) @Html.ValidationMessageFor(model => model.HomeAddressState) Here is the html output: <select data-val="true

KendoUI cascading dropdownlists, need value from 2 dropdownlists.

我的梦境 提交于 2019-12-10 11:09:19
问题 I have 3 cascading dropdownlists as follows: <p> <label for="categories">Catergories:</label> @(Html.Kendo().DropDownList() .Name("categories") .OptionLabel("Select category...") .DataTextField("CategoryName") .DataValueField("CategoryId") .DataSource(source => { source.Read(read => { read.Action("GetCascadeCategories", "ComboBox"); }); }) ) </p> <p> <label for="products">Products:</label> @(Html.Kendo().DropDownList() .Name("products") .OptionLabel("Select product...") .DataTextField(

How to properly send SelectedValue of DropDownList from View to controller? ViewModel

无人久伴 提交于 2019-12-10 10:52:13
问题 I've tried many solutuons, for example this, this and this. However, it does not work cause other examples use ViewBag , but I am using ViewModel . I have ScheduleViewModel : public class ScheduleViewModel { public int[] SelectedValues { get; set; } public IEnumerable<SelectListItem> Values { get; set; } public Schedule OneSchedule { get; set; } } Controller action List : public ActionResult List(ScheduleViewModel scheduleVM)//scheduleVM is always NULL { var model = new ScheduleViewModel();

MVC DropDownList selected value not working

牧云@^-^@ 提交于 2019-12-10 03:24:13
问题 I am using MVC 5. I have my ViewBag for list as ViewBag.TitleList = new SelectList((new string[] { "Mr", "Miss", "Ms", "Mrs" }), contact.Title); //contact.Title has selected value. then I tried converting the array to SelectListItem ( to no avail) On the View it looks like @Html.DropDownListFor(model => model.Title, ViewBag.TitleList as SelectList, "Select") also I tried @Html.DropDownList("Title", ViewBag.TitleList as SelectList, "Select") The list loads successfully but the Selected Value

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,

Populate single dropdown from two tables

。_饼干妹妹 提交于 2019-12-08 15:07:13
问题 The scenario is: Data is stored in database in project and neighbourhood tables both. Now, i want to populate dropdown with project id and project name and neighbourhoodId and neighbourhood name. i am right now sending through viewBag like this: ViewBag.NeighbourhoodId = new SelectList(allNeighbourhood(), "Id", "NeighbourhoodName"); on view page, getting dropdown populated like this: @Html.DropDownList("Locations", ViewBag.NeighbourhoodId as SelectList, "Select a location") Now,how to send