MVC 4 Ajax的请求 - 引用JavaScript文件 [英] MVC 4 Ajax Requests - referencing a javascript file

查看:77
本文介绍了MVC 4 Ajax的请求 - 引用JavaScript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林做一些Ajax调用返回了当脚本写在视图中工作细一些局部视图。

Im making some ajax calls to return some partial views which are working fine when the scripts are written in the view.

脚本code是

<script type="text/javascript">
$.ajax({
    url: "@(Url.Action("ProjectPartial", "Project"))",
    contentType: 'application/html; charset=utf-8',
    type: 'POST',
    dataType: 'html',
    data: {
        documentType: $('#DocumentType').val(),
        sectionName: $('#SectionName').val()
    }
})
.success(function (result) {
// Display the section contents.
$('#Projects').html(result);
})
.error(function (xhr, status) {
alert(xhr.responseText);
});
</script>

我想要做的就是将这些存储在名为renderpartial.js一个JavaScript文件,这样我可以添加ajax调用到一个文件中,而不是把它们写入到每个视图。

What i want to do is to store these in a javascript file called renderpartial.js so that i can add ajax calls to to one file and not write them into every view.

有谁知道这是可能的吗?

Does anyone know if this is possible?

香港专业教育学院试图把

Ive tried putting

<script src="~/Scripts/RenderPartial.js"></script>

在我的页面的顶部,但我得到的是错误的功能。

at the top of my page but all i get is the error function.

推荐答案

只要你使用像 @(Url.Action内嵌剃刀语法(你不能移动它js文件

As long as you use inline razor syntax like @(Url.Action( you can't move it to js file

您可以做到在任何指定的URL像

You can do it in either specifying url like

url: '/Project/ProjectPartial',

或View.cshtml

or in View.cshtml

<script type="text/javascript">
var projectUrl="@(Url.Action("ProjectPartial", "Project"))"
</script>

在RenderParial.js

in RenderParial.js

url: projectUrl,

这篇关于MVC 4 Ajax的请求 - 引用JavaScript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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