I am doing:
var url = '@Url.Action("Attachments", "Transactions")';
url += '/?id=' + 3201;
$("#attachments").load(url);
However, on load it doesn't do anything. Am i missing something?
I essentially want to call something similar to:
@{Html.RenderAction("Attachments", "Transactions", new { id = 3301 });}
I get the following error on console:
http://server:54137/Transactions/@Url.Action(%22Attachments%22,
I am doing:
var url = '@Url.Action("Attachments", "Transactions")';
url += '/?id=' + 3201;
$("#attachments").load(url);
However, on load it doesn't do anything. Am i missing something?
I essentially want to call something similar to:
@{Html.RenderAction("Attachments", "Transactions", new { id = 3301 });}
I get the following error on console:
http://server:54137/Transactions/@Url.Action(%22Attachments%22,
Share
Improve this question
edited Jun 19, 2015 at 8:28
hutchonoid
33.3k15 gold badges101 silver badges106 bronze badges
asked Jun 19, 2015 at 7:43
SamIAmSamIAm
2,5117 gold badges38 silver badges55 bronze badges
5
-
what are you trying to do?
.load()
- Load data from the server and place the returned HTML into the matched element api.jquery./load – Jossef Harush Kadouri Commented Jun 19, 2015 at 7:46 -
Put a
console.log(url);
statement immediately aftervar url = ...
and check what it returns. – user3559349 Commented Jun 19, 2015 at 7:52 -
1
your code works fine for me. I used
console.log(url);
and it printed -/Transactions/Attachments/?id=3201
. So please check the other parts of your code. – ramiramilu Commented Jun 19, 2015 at 7:54 -
Note also it should be
url += '?id=' + 3201;
(no forward slash) – user3559349 Commented Jun 19, 2015 at 7:56 - Where are you 'doing' the javascript? What's the filename? Does it end in .js ? – fdomn-m Commented Jun 19, 2015 at 8:38
2 Answers
Reset to default 5You must be using an external JavaScript file which will not parse your razor syntax hence the error in your console of @Url.Action(%22Attachments%22.
.
You have a couple of options:
Create a JavaScript function and pass in the url:
function loadUrl(url) { $("#attachments").load(url); }
Then in your razor call it within a script tag:
loadUrl(@Url.Action("Attachments", "Transactions", new { id = @Model.Id })
- Add the url to the html element as data and read it from your JavaScript with the
data
method.
In your razor markup add this:
<button data-url="@Url.Action("Attachments", "Transactions", new { id = @Model.Id })" />
From your JavaScript event handler read it with:
var url = $(this).data('url');
$("#attachments").load(url);
I prefer the second option.
You Need to use Html.Raw check below
var url = "@Html.Raw(Url.Action("Attachments", "Transactions"))";
url += '/?id=' + 3201;
$("#attachments").load(url);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744167874a4561392.html
评论列表(0条)