C# MVC3 Listbox callback - asp.net-mvc-3

I am creating a ListBox in MVC:
#Html.ListBoxFor(model => model.SelectedItemIds, new SelectList(Model.Items, "Value", "Text"))
which works fine, I can see the ListBox appear in the view.
I want to add some kind of callback when a user clicks and selects an item in the list. How can I do this using MVC3?

You would probably be best served by adding an id parameter to your ListBox, and then doing whatever callback that you want by using jQuery. Your line of code would then be like this:
#Html.ListBoxFor(model => model.SelectedItemIds, new SelectList(Model.Items, "Value", "Text"), new {id = "MyListBox")
Afterwards you could easily hook up an event in jQuery as follows:
$('#MyListBox').change(function() { ...some function... } );

You can add javascript onChange handler with this overload:
#Html.ListBoxFor(model => model.SelectedItemIds, new SelectList(Model.Items, "Value", "Text"), new{onchange="onListBoxChanged(); return false;"})
where onListBoxChanged() is javascript function.

You can do something like this
#using(html.begainform( ........your action name........))
{
#Html.ListBoxFor(model => model.SelectedItemIds, new SelectList(Model.Items, "Value", "Text"))
}
& in jquery you can write something like this
$("<Id or class of your ListBox>").change(function() {
this.form.submit();
});

Related

How can I send an Html.HiddenFor value as a parameter in an onchange event?

I’m trying to send a parameter to an action using a dropdown list and onchange. I’ve tried sending a hard-coded value like this:
onchange = "this.form.action='/Profile/Edit/16
That works fine but now I want to send the value from a hidden id found here:
#Html.HiddenFor(model => model.StudentID)
…doing something like this:
#Html.DropDownList("DropDownValue", new SelectList(ViewBag.sellectedSubjects, "text"), "select one", new { onchange = "this.form.action='/Profile/Edit/#model => model.StudentID';this.form.method='get';this.form.submit();" })
When I do this I get a ‘Bad Request’ error. How can I send the value in my Html.HiddenFor as a parameter in onchange? Thanks for any help!
Try:
#Html.DropDownList("DropDownValue", new SelectList(ViewBag.sellectedSubjects, "text"), "select one", new { onchange = "this.form.action='/Profile/Edit/"+Model.StudentID+"';this.form.method='get';this.form.submit();" })

MVC3: Set Dropdown list Selected Value

I am using mvc3 and I have a drop down list in my view.
#Html.DropDownListFor(m => m.State,
new SelectList(Model.StateList, "Value", "Text"))
Is there a way of setting the selected value in the View?
Extending on what Romias said, in your controller, set Model.State to whatever value you want. If you wanted 'WI', then Model.State should equal that.
Controller:
public ActionResult Index()
{
var m = new TestViewModel();
m.State = "WI";
return View(m);
}
View:
#Html.DropDownListFor(m => m.State, new SelectList(Model.StateList, "Value", "Text", Model.State))
Just do:
#Html.DropDownListFor(m => m.State, new SelectList(Model.StateList, "Value", "Text", Model.State))

Strongly Typed ASP.NET MVC DropDownList Without SelectList

Is there a way to remove the selectlist parameter below and still use the HTML Helper? I like the strongly typed option, but use knockout.js quite a bit.
#Html.DropDownListFor(m => m.ProgramId, new SelectList(Enumerable.Empty<ProgramModel>()), new { data_bind = "enable: programs().length > 0, value: programId, options: programs, optionsText: 'Name', optionsValue: 'Id', optionsCaption: ' -- Choose Program -- ', event: { change: function(_,event) { getLicenseProductsForProgram() } }" })
Since Drop down List actually needs to have list of options, it requires the selectlist parameter. You can always pass an empty list to it though:
#Html.DropDownListFor(m => m.ProgramId, new List<SelectListItem>(), new { [the rest of your stuff] })
No, that's not possible. The standard DropDownList/DropDownListFor helpers expect an IEnumerable<SelectListItem> as second argument. Obviously there should be nothing preventing you from writing a custom helper, or custom editor template.

Populating a dropdownlist from a value type

This obviously is pretty basic question but I haven't found my way around this: I'm trying to create a dropdown list for an integer property of my Model, so I can populate the dropdown with a default list of values.
My controller looks like this:
ViewBag.MyListOfOptions = new SelectList(new int[] { 5, 15, 25, 50 });
And my view looks like this:
#Html.DropDownListFor(model => model.MyIntField, new SelectList(ViewBag.MyListOfOptions , [dataValueField], [dataTextField]))
Most examples I've found dataValueField and dataTextField are the properties that should be used in creating the dropdown, but I haven't found a way on how to use DropDownListFor with value types
Because you created the SelectList from MyListOfOptions which is already a SelectList you can use "Text" and "Value"
#Html.DropDownListFor(model => model.MyIntField,
new SelectList(ViewBag.MyListOfOptions , "Value", "Text"))
But you don't need to wrap again an SelectList, so this will also work:
#Html.DropDownListFor(model => model.MyIntField,
(IEnumerable<SelectListItem>)ViewBag.MyListOfOptions)
One easy way to get around this is to take this approach using a ViewModel:
dropdownlist set selected value in MVC3 Razor
MVC 3 Layout Page, Razor Template, and DropdownList

Telerik MVC specifying your own action router within a template column

I am using the latest version of Telerik MVC with ASP.NET MVC 3 and the Razor view engine.
I have the following column declaration:
column.Bound(x => x.Id)
.Template(x => Html.ActionLink("Edit", "Edit", new { id = x.Id }))
.Title("Action")
.Width(100);
I have created my own method that routes to this Edit action method which I would like to use but not sure how to?
public static object AdministrationCategoryEdit(this UrlHelper urlHelper, int categoryId)
{
Check.Argument.IsNotNull(urlHelper, "urlHelper");
return new { area = "Administration", controller = "Category", action = "Edit", id = categoryId };
}
How would I reference the above method in my column declaration and pass it through the category ID?
For example, if I want to use it with a button, then I would do something like:
$('#btnEdit').click(function () {
window.location = '#Url.RouteUrl(Url.AdministrationCategoryEdit(Model.Id))';
});
There is no perfect match for your requirement since all ActionLink methods have a separate parameter for the action. However, it should work with the following code even though the action is now specified twice.
column.Bound(x => x.Id)
.Template(x => Html.ActionLink("Edit", "Edit", Url.AdministrationCategoryEdit(x.Id)))
.Title("Action")
.Width(100);
An alternative would be to create an HTML helper similar to ActionLink that now only generates the route parameters but the complete code for a link.

Resources