简体   繁体   English

如何使用Jquery .load()重新加载部分视图?

[英]How do I reload a partial view using the Jquery .load()?

I have a partial view on my page that I would like to reload. 我的页面上有一个局部视图,我想重新加载。 It is located in the "Attachments" div. 它位于“附件” div中。

What I'm doing at the moment which is not working is by using the following command: 目前无法执行的操作是使用以下命令:

$('#attachments').load('@Url.Action("Attachments" "Transactions",new {id=1297})')

The above will give me an error in the console of: 上面的内容会在以下控制台中给我一个错误:

GET http://localhost:49793/Transactions/@Url.Action(%22Attachments%22 404 (Not Found)

What am I missing? 我想念什么? Why is the URL.Action() not rendered properly? 为什么URL.Action()无法正确呈现?

The action Attachments in the Transaction controller is [ChildActionOnly]. 事务控制器中的操作附件为[ChildActionOnly]。

Your action url comes from your backend. 您的操作网址来自您的后端。 In the way that you are doing it, you're sending just a string with your backend action, not the url. 用这种方式,您只发送带有后端操作的字符串,而不发送url。

One possible way it's to use a data-attribute in your html like: 一种可能的方法是在html中使用数据属性,例如:

<div id="attachments" data-url="@Url.Action("Attachments" "Transactions",new {id=1297})"></div>

and in your javascript: 并在您的JavaScript中:

var $attachments = $('#attachments'),
    url = $attachments.data('url'); 

$attachments.load(url)

or even a hidden input: 甚至是隐藏的输入:

<input type="hidden" id="attachmentsUrl" value="@Url.Action("Attachments" "Transactions",new {id=1297})" />

var url = $('#attachmentsUrl').val(); 

$('#attachments').load(url)

or you can try some other variations, just be attended to render your url in your markup 或者您可以尝试其他一些变体,只需注意在标记中呈现您的网址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM