任何人都可以指导我在MVC 4中调用Ajax调用部分视图吗? [英] Can anyone guide me to make Ajax call for partial view in MVC 4 ?

查看:54
本文介绍了任何人都可以指导我在MVC 4中调用Ajax调用部分视图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用MVC中的Web应用程序。我有一个局部视图,其中包含导航菜单。问题是,在向网格提交值时,它会刷新并且所有导航都会崩溃。我是MVC的新手,而谷歌搜索Ajax Call将完成工作,但我不知道如何解决它。





先谢谢,

Alok

I have been working on a web application in MVC. i have a partial view ,which contains a navigation menu. The problem i that while on submitting values to grid it gets refreshed and all the navigation get collapsed. I am a newby to MVC while googling Ajax Call will do the work but i don't know how to work it out.


Thanks in Advance,
Alok

推荐答案

Ajax调用是对服务器的简单异步请求,用于为应用程序加载资源。在此示例中,我将考虑您的页面(将处理ajax请求)具有部分视图加载代码,并且视图仅生成菜单的HTML内容。



Ajax请求在浏览器中原生可用,您只需要配置ASP.NET Web应用程序以接受ajax请求并返回配置为的响应作为一个片段发送(不在ajax请求中发送整个HTML DOM,如果你必须下载整个HTML标记,则没有ajax请求的目的)。将资源发送到服务器也是一样的。要使用ajax提交表单(因为您发送ajax请求的目的是忽略表单回发,停止页面重新加载),您可以添加 event.preventDefault()到ajax处理程序,以便表单不提交,你将自己的数据发送到服务器。



Ajax calls are simple asynchronous requests to the servers to load the resources for your application. In this example I will consider that your page (which will handle the ajax request) has the partial view loading code present, and the view only generates the HTML content for the menu.

Ajax requests are available natively in the browsers, you only need to configure your ASP.NET web application to accept the ajax requests and return the response which is configured to be sent as a snippet (do not send entire HTML DOM in ajax requests, there is no purpose of ajax requests if you have to download entire HTML markup). Same thing for sending the resources to the server. To submit a form using ajax (because your purpose to send ajax requests is to ignore the form postback, stopping page reloading) you can add an event.preventDefault() to the ajax handler so that the form doesn't submit and you send your own data to the server.

event.preventDefault(); // Inside the function for button handler


.ajax( {
url:' ajax-controller / form-handler'
data :
.ajax({ url: 'ajax-controller/form-handler', data:


' #myform')。serialize() , // 序列化表单并作为请求数据提交
成功: function (data){
// 提交的数据
}
});
('#myform').serialize(), // Serialize the form and submit as data for request success: function (data) { // Data submitted } });





我为初学者撰写了一篇帖子,主题如 ASP.NET MVC中的Ajax 。它介绍了如何在ASP.NET MVC中执行这些操作,也许单独的控制器是最佳选择!如果是ajax控制器,你也可以返回一个字符串,这样布局就不会返回HTML DOM了。



ASP.NET MVC框架中ajax开发人员的提示 [ ^ ]



I have written a post for beginners in topics such as, Ajax in ASP.NET MVC. It covers how you should perform these actions in ASP.NET MVC, perhaps a separate controller is best choice! Also you may return a string in case of ajax controllers, so that HTML DOM is not returned by the layouts and so.

A tip for ajax developers in ASP.NET MVC framework[^]


这篇关于任何人都可以指导我在MVC 4中调用Ajax调用部分视图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆