Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 通过Ajax调用局部视图_Jquery_Html_Ajax_Model View Controller_Partial Views - Fatal编程技术网

Jquery 通过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();

我通过Ajax调用部分视图,但它给出的结果未定义

我的控制器是:

[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()块?不,我认为这是浏览器的问题。现在它可以正常工作了。再次感谢你。