MVC partial render - asp.net-mvc

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

Related

Pass javascript parameter to partial view in asp.net mvc

lets say I want to a partial view like the following sample
#Html.RenderAction("ListDocuments", "MultiFileAttachments", new { id = jsparameter });
in the jsparameter I want to pass as a parameter that is the result of a javascript function. Maybe this is not logical because javascript runs on the client, but how can I achieve a similar functionality ? thank you in advance
If you need to call a part of a page based on some actions or values determined by actions on the client side, then you should think to use JavaScript (preferably jQuery) and Ajax.
Through jQuery-Ajax, you could easily call a Partial view through the Contoler and since it's a regular HTTP request, you would be able to pass all the parameters you need.
Using code as :
#Html.RenderAction("ListDocuments", "MultiFileAttachments", new { id = jsparameter });
is not possible since it is rendered by the server before it's sent to the client browser.

Callinf action in controller via jquery

I am new to MVC and I am continuing to learn more each day. I am converting and webform application to MVC for practice and wanted to know is there a way to call GET on a action and return json, array or whatever I want on document.ready function? I can do this using webapi, but I would rather do it using an action in the controller. I like the helpers that they Microsoft provides but they are for forms and action links, etc. Thanks for any help.
$(document).ready(function(){
$.ajax(Somehow call controller action here with data), success(function that returns json data)
});
I think this article has a great explanation with examples
http://www.codeproject.com/Articles/41828/JQuery-AJAX-with-ASP-NET-MVC

Display AJAX controller action result using HtmlHelpers

I have a fairly complex object which has some C# code written to render it as HTML in various views.
There is also a view which can call an AJAX method of a controller, which returns the complex object serialized to JSON which should then be displayed.
This seems to leave me requriring complicated duplicate code to render the resulting JSON as HTML using Javascript/jQuery.
The obvious solution is to render the HTML in the controller action and return this from the AJAX call. However this seems in violation of the MVC pattern so not really a good option.
Is there a different way I can render the object returned from the AJAX method making use of the existing C# code?
Thanks.
Create a PartialView to which you render the object, and return that.
As mentioned, either create a PartialView user control and return that, which you can inject the HTML in nicely (returns the HTML as a string) or you can use a templating option in JQuery or something else to do the UI generation for you.
HTH.

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.

ASP.Net MVC Best approach to render a results grid

I'm creating a search page, the page has a form that is being submitted using Ajax, after the search is performed I want to display a grid with the results.
My question is, should I create the grid when the page loads and then fill it with the data after the search is performed, or create the grid on the server when the search is performed and then just append the grid to the page.
I was thinking on creating a helper method to render the grid and invoking it from the controller after it gets the results, then returning the result of the helper method and appending it to the page, but this might be against MVC architecture (I'm defining UI on the controller).
What approach should I take?
Thanks
for the grid creation, you can have a look at MVCContrib grid helper
You could use jqGrid (http://www.trirand.com/blog/) or Flexigrid (http://www.flexigrid.info/) and load data with ajax and json. You submit search form with ajax, controller returns JsonResult, and then you load it into grid in callback. It is easy to implement and gives you additional functionalities (sorting and much, much more). Here you have some demos:
http://trirand.com/jqgrid/jqgrid.html

Resources