开发者

how does mvc work when index is returned via ajax?

开发者 https://www.devze.com 2022-12-17 01:53 出处:网络
does it reload the entire page or does it have the intelligence to only send the nec开发者_开发知识库essary js to update the needed parts of the page that have changed?

does it reload the entire page or does it have the intelligence to only send the nec开发者_开发知识库essary js to update the needed parts of the page that have changed?

if it does the latter that would be a god send, however im probably being dreamful. it probably returns the entire view without any regard, right?

edit: answer seems to be no, everything is returned.

edit added: do you think it would be difficult to write a framework where mvc compares last html it output to the current html we want to output, and instead of sending the entire html, figure out what has changed and generate js code that will do the updating as compared to previous html? (presuming nothing was manually changed on the client using js)... maybe an idea for a codeplex project? or maybe something like this exists?


Well, it entirely depends on how you do it.

You can return anything you want, when using ajax its common to return your data in JSON. Example:

public ActionResult GetCustomers()
{
    if(Request.IsAjaxRequest)
       return Json(db.GetCustomers());

   return View(db.GetCustomers());
}

This will return all customers Json encoded if the request was made using Ajax.

You can stil return anything you want, if you want to return a view, it's just to

return View(model);

If you dont want to return the layout (master page), just return the MasterPageFile directive from your view.

And no, it does not reload the entire page, that's why it's called Ajax.


Frankly, what happens on the client side is of no concern to MVC. :-) It does not know whether the call was made by the browser engine or the ECMA script.

And since you are asking about Ajax call, the responsibility with dealing with the result falls onto your script (or whatever JS framework you're using).

0

精彩评论

暂无评论...
验证码 换一张
取 消