【发布时间】:2016-12-21 22:43:59
【问题描述】:
我的应用程序是 MVC 5 C#。 我正在尝试使用此 Ajax 脚本从控制器获取字符串值:
function testing() {
$.ajax({
url: "../../Home/Test",
type: "GET",
data: {
id: 1
},
success: function (result) {
if (result.PhotoURL) {
$("#Photo").html(result.PhotoURL);
}
},
error: function (result) {
alert("error");
}
});
}
这里是控制器:
public ActionResult Test(int id)
{
int updateid = 1;
string thephoto = "<img src=\"@Url.Action(\"TheImage\",
new {id = " + updateid +
"})\" class=\"mg-responsive\" style=\"margin-left:
auto;margin-right: auto\"/> ";
var result = new
{
PhotoURL = thephoto
};
return Json(result, JsonRequestBehavior.AllowGet);
}
我明白了:
<img class="mg-responsive" style="margin-right: auto; margin-left: auto;"
src="@Url.Action(" {id='1})"' new="" thespillimage",="">
【问题讨论】:
-
你希望得到什么?
-
-
这正是您的
Test()方法返回的内容:)。在string中使用@Url.Action之类的东西是没有意义的——它的剃须刀代码在服务器中进行评估,不会在您的脚本中进行评估。只需返回实际的url(在方法中使用string url = Url.Action(...);生成url -
谢谢斯蒂芬,它成功了。
标签: c# json ajax asp.net-mvc