autopostback for dropdownlist in mvc.net - asp.net-mvc

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.

Related

Asp.net MVC - call database on dropdown value change

I am new to MVC. i want call database on dropdown value change. Does anybody know how to do that.
Regards,
vangli
MVC does not have a post-back mechanism as such as you had in WebForm (well, this was being carried out as javascript posting the form.
What you can do is create some javascript using jquery if you want to trigger some action when the item in that dropdown changes. it would be, (assuming the id of the dropdown is idDropDown
$('#idDropDown').change(function () {
$.ajax(#Url.Action("AjaxAction", "MyController")', { selection : selectedValue }, function (data)){
//handle ajax response here
};
});
Your action controller will be like this:
public ActionResult AjaxAction(string selection)
{
// do your server-side processing and get your data
return Json(data);
}
With out jquery you can POST the data to controller action using pure java script.
#Html.DropDownListFor(m => m.SelectedValue,Model.SelectListItems,new{ onchange = "this.form.submit();" })
Note: Here simply you can submit form by adding java script code to on change. This is similar that you click on submit button.

Cascading dropdown lists in ASP.NET MVC 5

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.

MVC call controller from radio button onclick

How can I call an action in my controller and pass a parameter from a radio button onclick event? I'm new to MVC....
I have this code which will call a javascript function, but I wanted to see if I can call an action and pass a parameter instead of calling a javascript function
#Html.RadioButtonFor(m => m.SiteAddressModel.SiteId, item.SiteId, new { #onclick = "radiobuttontest();"})
This is on of the differences between asp.net MVC and webforms in both cases you are using javascript to make a post or ajax call the difference is in webforms it's hidden by the framework. MVC give you greater control but it requires you do the work, if you have the javascript working your done.

MVC partial render

if renderpartial in MVC is not like Update panel in ASP.net. than how does it works, and what about the efficiency. I heard that update panel was so inefficient in use. But how does MVC handles postbacks, I need to undestand this before I can dive into MVC
Any suggestions
thank you
ASP.MVC Partial views are just reusable HTML fragments that can be populated by View Models. They don't have any special built in functionality like update panels do.
In general terms, with ASP.MVC you control post backs. In fact, you have to code it all yourself in HTML and JavaScript.
I suggest you start here.
Assuming that you want to update part of your page, the method I use is as follows:
Link a JavaScript function to the event you want to use to update the 'panel'
Make a jQuery AJAX call to an action in your controller
from the controller return a call to the partial view
this will cause the resulting HTML from the partial view to be returned as HTML to your AJAX call
use jQuery to add the HTML to an existing empty div
The AJAX call looks something like
$.ajax({
url: yourControllerAction URL,
data: { CodeTypeID: codeTypeID }, // optional data
type: "POST",
success: function (returnedHtml) {
$("#myDiv").html(htmreturnedHtmll);
}
});
The rest is standard MVC
Hope that helps

how to implment click-once submit button in asp.net mvc 2?

I need to implement a click-once button for my asp.net mvc 2 application. I have just a very simple submit form, and when the user clicks on the submit button, I need to change its image to a "please wait..." type of graphics and disables the click event for further submits until the server comes back.
Is there an example or code snippet for this? I used to be able to do it in the asp.net webforms, but that technique does not apply here.
thanks!
To disable the submit button when you submit the form you can use the onSubmit function for the form and call a javascript function that will disable the button.
For example:
<% using (Html.BeginForm("Create", "Organisation", FormMethod.Post, new { autocomplete = "off", onsubmit = "disableSubmitButton()" }))
{ %>
//Your code
<input id="buttonName" type="submit" value="Create"/>
<% } %>
and then in your javascript section:
function disableSubmitButton() {
document.getElementById("buttonName").disabled = true;
};
Hope this helps.
Here is example of how create ajax login using asp.net mvc and jQuery:
How to implement a Client-side Ajax Login on Asp.Net MVC (A link to the solution for Asp.Net Webforms is in here)

Resources