Jquery 通过Ajax调用局部视图
我通过Ajax调用部分视图,但它给出的结果未定义 我的控制器是:Jquery 通过Ajax调用局部视图,jquery,html,ajax,model-view-controller,partial-views,Jquery,Html,Ajax,Model View Controller,Partial Views,我通过Ajax调用部分视图,但它给出的结果未定义 我的控制器是: [HttpGet] public ActionResult CallPartial() { if (Request.IsAjaxRequest()) { return PartialView("~/Views/Partial1"); } else { return View();
[HttpGet]
public ActionResult CallPartial()
{
if (Request.IsAjaxRequest())
{
return PartialView("~/Views/Partial1");
}
else
{
return View();
}
}
我的看法是:
<div id="divpartial">hello
<button id="btn" onclick="callpartial()">Click</button>
</div>
<script type="text/javascript" >
$(function callpartial() {
$.ajax({
url: '~/Controllers/Home/CallPartial',
contentType: 'application/html; charset=utf-8',
type: 'Get',
dataType: 'html',
success: function (status) {$("#divpartial").html("Welcome to partial view"); },
error: function (status) {
alert(status.Value);
}
alert("Request Fails");
});
});
<body>
<div>hello this partial</div>
</body>
你好
点击
$(函数callpartial(){
$.ajax({
url:“~/Controllers/Home/CallPartial”,
contentType:'application/html;charset=utf-8',
键入:“Get”,
数据类型:“html”,
成功:函数(状态){$(“#divpartial”).html(“欢迎使用部分视图”);},
错误:功能(状态){
警报(状态值);
}
警报(“请求失败”);
});
});
最后,我的部分观点是:
<div id="divpartial">hello
<button id="btn" onclick="callpartial()">Click</button>
</div>
<script type="text/javascript" >
$(function callpartial() {
$.ajax({
url: '~/Controllers/Home/CallPartial',
contentType: 'application/html; charset=utf-8',
type: 'Get',
dataType: 'html',
success: function (status) {$("#divpartial").html("Welcome to partial view"); },
error: function (status) {
alert(status.Value);
}
alert("Request Fails");
});
});
<body>
<div>hello this partial</div>
</body>
你好,这是部分
看起来您的路线很糟糕,通常在MVC中,您的路线中没有“控制器”这个词。试试这个:
url: '@Url.Action("CallPartial", "Home")',
很乐意帮忙!那么您的代码没有进入Request.IsAjaxRequest()块?不,我认为这是浏览器的问题。现在它可以正常工作了。再次感谢你。