MVC call controller from radio button onclick - asp.net-mvc

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.

Related

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

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.

Showing both MVC Ajax calls and jQuery Ajax calls with one indicator

I have a web application that utilises both the MVC Ajax calls:
Html.ActionLink() rendering as Sys.Mvc.AsyncForm.handleClick( in the page source
and: jQuery Ajax calls, eg:
$.post()
Now I can easily set a generic Ajax indicator to show and hide when the jQuery is making an Async call using:
$('#indicatorId').ajaxStart(function() {
$(this).show();
}).ajaxStop(function() {
$(this).hide();
});
And I can set some functions to show and hide the same indicator by passing values into the constructor of the Ajax.ActionLink constructor and setting OnBegin and OnComplete, but this means that I have to do this EVERYTIME I add a new Ajax.ActionLink to the site.
Is there a better way to do this? Perhaps I could scan the DOM after it's rendered and add the generic events then?
Any thoughts, better examples?
Unless you've a compelling reason (I've never found one) you shouldn't mix jQuery and MS Ajax into the same site and unless you've a compelling reason you should prefer jQuery. So, no need to use Ajax.* helper methods which tend to pollute the markup with javascript.
Do it the jQuery way, unobtrusively.

How to perform PostBack operation in ASP.NET MVC?

In mvc the page is not get post back as in asp.net then how can we perform postback operations in asp.net mvc2. for ex how to perform particular action when someone selects a chech box?
Thanks in Advance
The mechanism behind the postback model in WebForms is called HTTP POST. This is how user input is communicated back to the server.
You can do it manually. Attach a JavaScript handler manually to the checkbox "onclick" event and perform a POST request to some url. There, this request will hit some controller action where you do what you want. For example, update the model (check/uncheck the checkbox) and return the same view from which the POST originated. The view will now show the different state for the checkbox.
The WebForms mechanisms do pretty much the same, though these things are abstracted away from you. With ASP.NET MVC you'll need to learn how to do it on your own (which is always a good thing).
Your MVC Action method on your Controller somewhat is your 'PostBack' handler.
Start with a simpler example; a simple HTML form post:
<form action="/MyController/MyAction" method="post">
<input type="text" name="myName" />
<input type="submit />
</form>
Now in your controllers action you can get the posted values and perform your tasks. When done give the browser back what it needs:
public class MyController: Controller
{
public ActionResult MyAction(string myName)
{
// Do something with myName
return new ContentResult { Content = "Hello " + myName };
}
}
As for a checkbox, it is different. You need to learn Javascript (jQuery is the most used library to use with that) and post the action using that. For example you can wire up to the check box 'onclick()' event and perform an XHR - a browser specific Javascript operation, post (you can use jQuery for that too) to your controller.
So you need to start thinking differently than webforms abstractions and get involved with HTML, HTTP and Javascript.
you can Put this inside an MVC Razor page:
if (Request.HttpMethod=="POST") {
}

ASP.NET MVC AjaxActionLink OnComplete and OnSuccess property and Jquery

I'm trying to create a dialog box in my ASP.NET application with an AJAX ActionLink.
The Ajax ActionLink inserts a partial view into a div in the current view.
I've called a function OnComplete (and I've tried OnSuccess, too) that calls the dialog function for JQuery UI, like so:
function() loaddialog {
$("ContainerwithInsertedPartialView").dialog();
}
Nevertheless, it display the dialog. Any ideas as to what I'm doing wrong?
You have your function definition wrong, for one. You need to use the id selector (prepend a hash) for another.
function loaddialog() {
$('#ContainerwithInsertedPartialView').dialog();
}

Resources