Cascading dropdown lists in ASP.NET MVC 5 - asp.net-mvc

I am wondering if there's some new helper or method introduced in ASP.NET MVC 5 to implement cascading dropdown lists. I know a way to implement cascading dropdownlist behavior in MVC 3 and MVC 4 that is by using a JSON call
http://www.dotnet-tricks.com/Tutorial/mvc/HL53191212-Custom-Validation-for-Cascading-Dropdownlist-in-MVC-Razor.html
So anyone knows a better way to implement cascading dropdownlists in MVC 5?

I know that this is an old question but somebody still may find it useful
I was searching for the same thing but din't find anything stable and useful so I ended up implementing it by myself:
Please take a look at Mvc.CascadeDropDown helper that I created.
It works with all MVC versions starting from MVC3 and doesn't require any client side libraries(It uses plain vanilla JavaScript).
The usage is very simple:
#using Mvc.CascadeDropDown
//First simple dropdown
#Html.DropDownListFor(m=>m.SelectedCountry, Model.Countries,
"Please select a Country", new {#class="form-control"})
//Dropdown list for SelectedCity property that depends on selection of SelectedCountry property
#Html.CascadingDropDownListFor(
expression: m => m.SelectedCity,
triggeredByProperty: m => m.SelectedCountry, //Parent property that trigers dropdown data loading
url: Url.Action("GetCities", "Home"), //Url of action that returns dropdown data
actionParam: "country", //Parameter name for the selected parent value that url action receives
optionLabel: "Please select a City", // Option label
disabledWhenParrentNotSelected: true, //If true, disables dropdown until parrent dropdown selected
htmlAttributes: new { #class = "form-control" }) //Html attributes
Hopefully it will be helpful for some of you

No, there are no new helpers or methods in MVC 5 to help.
The Ajax HTML helpers have been largely ignored in the update. There are some things that may help with stuff related to this:
There is a new #Html.EnumDropDownListFor() HTML helper to populate a dropdown from an Enum.
The Attribute routing functionality of the has been improved and now works with the Web API so it is much easier to map URLs to API calls.
You can now pass in html attibutes in the EditorFor Html helper #Html.EditorFor(m => m.FieldName, new { htmlAttributes = new { #class = "form-control" } })
I implemented cascading dropdowns last week and used the tried and true JSON call you mentioned. I like to use this jQuery plugin in conjunction with the Web API v2 with the new attribute routing.

Related

Kendo dropdownlist doesn't update when I change bound angular model

Below I have 2 dropdown lists, both bound to an Angular model. The first is a Kendo dropdown, the second is just a standard MVC dropdown. They both correctly display their initial selections. The second one correctly updates its selected item when I make a change to the underlying Angular model. Why doesn't the first one do the same?
#(Html.Kendo().DropDownListFor(x => x.HomeProvince)
.DataTextField("Text")
.DataValueField("Value")
.BindTo(Model.ProvinceList)
.OptionLabel("Select")
.HtmlAttributes(new { required = "required", ng_model = "model.HomeProvince", ng_pattern = "/^[1-9][0-9]{0,1}$/" })
)
#Html.DropDownListFor(x => x.HomeProvince, Model.ProvinceList, new { ng_model = "model.HomeProvince" })
Do not know Kendo, but I do know that if you are working with 3rd party libraries updating data bound to angularjs models. You need to use $scope.$apply. This will force the digest cycle and angular will take care of the binding.

How to add a custom attribute for a Drop Down List Item?

I have the below code in MVC, Razor:
- #Html.DropDownListFor(model => model.SelectedEvent,
new SelectList(Model.Events, "Id", "Name"))
I'd like to add a new attribute for each option in the select called "description" which should be bound to the Event.Description.
How to achieve this?
I'd like to avoid creating a separate Html Helper. Would that be possible?
Unfortunately, this is not supported by current DropDownListFor implementation. There is an overload that takes a IDictionary<string, Object> of html attributes, but they are bound to the select element, not to its items, as you can verify in the MSDN documentation. I don't think you will be able to do what you need here without implementing a custom helper.
Check out a couple of custom helpers that implement a similar behaviour in the answers to this question.

Change in MVC ListBoxFor behaviour between projects

Im wondering if anyone can explain the following disparity in functionality between two ASP.Net MVC 3 projects.
In both projects I have a view model which contains the following:
public List<int> Questions;
And in both projects I have the following ListBoxFor code:
#Html.ListBoxFor(x => x.Questions, new MultiSelectList(ViewBag.Questions as List<MyStandardLib.Mvc.Attribute>, "Id", "Name", #Model.Questions), new { #class = "ui-field-multiselect", style = "width: 250px;" })
The difference in functionality is that when run, one project binds the existing selected Questions correctly, the other does not and shows all Questions as unselected. Stepping through the code, the List is populated in the view and is correctly passed to the MultiSelectList constructor, but it isn't setting the values as selected.
This is really frustrating.
Change your ViewBag.Questions variable name to ViewBag.AvailableQuestions. Sometimes the renderer gets confused about what you are referring to.

How to insert drop down list box in a Telerik grid

I have a Telerik Grid, with two columns I need to keep second column as drop-down list box with in the grid, I am using ASP.NET MVC control
Can any body tell me how to do this?
I need to do that for my project.
Here is how I did it:
columns.Bound(o => o.Role).ClientTemplate(
Html.Telerik().DropDownList()
.Name("RoleList<#= UserID #>")
.BindTo(new SelectList(UserController.GetRoles()))
.ToHtmlString()
);
The static method GetRoles returns a simple IEnumerable of String. You still can return a custom object by using a different SelectList constructor to specify Value and Text property of your custom object.
new SelectList(UserController.GetCustomRoles(), "RoleID", "ShortName")
You can set the template of the column to embed arbitrary HTML. If using Ajax binding - try the client template. The following online examples will be helpful:
Server templates
Client templates

autopostback for dropdownlist in mvc.net

how to set autopostback for dropdownlist in mvc.net?
You don't - there's no concept of 'autopostback', in the same way that there is no postback concept in the MVC framework.
If you want to submit the form, you can do that via javascript, if you want to update something else via a call to the server, you can set up an AJAX call, probably using jQuery to do so.
There's a bit of an example here.
no jquery required. wrap each hidden id and dropdown in a form with the action to updateproduct. then its just:
#Html.DropDownList("id", (SelectList)ViewBag.Values, new { onchange = "this.form.submit();" })
autopostback supports only in asp.net not in mvc.net, so you just need to write #Html.DropDownList("id", (SelectList)ViewBag.Values, new { onchange = "this.form.submit();" }) or jquery function in the cshtml file script section.
One thumb rule for MVC,
Any method of controller, on
be called by JS or Jquery thru Ajax calling routing.

Resources