weixin_33743661 2017-02-19 15:54 采纳率: 0%
浏览 33

自刷新局部视图

I set up a small demo application for refreshing partial views via jQuery/Ajax. I found a variety of examples refreshing the partial view from the main view, though not one where it refreshes from the view itself.

This is the code i tried to use:

Main View:

@model OrpheusTestWeb.Models.ViewModel
@{
    ViewBag.Title = "Home Page";
}
<h2>Guid1: @Model.GUID1</h2>
<div id="PartialViewDIV">
    @Html.Partial("_PartialView", Model.GUID2)
</div>

The partial view

@model string

<h3>Guid2: @Model</h3>

<input id="button" type="button" class="btn-danger" />

<script type="text/javascript">
    $("#button").on('click', function () {
        $.ajax({
            url: "Partial/Index",
            type: "get",
            dataType: "html",
            success: function (result) {
               $("#PartialViewDIV").html(result); //the PartialViewDIV-tag is in the main view, how can i "redirect" it then?
               console.log('success', data);
            }
        });
    }
</script>

And the controller for the partial view:

public class PartialController : Controller
    {
       public ActionResult Index()
       {
           return PartialView("_PartialView", Guid.NewGuid().ToString());
       }
    }

What is going wrong here? Thank you in advance!

  • 写回答

1条回答 默认 最新

  • weixin_33722405 2017-02-19 16:41
    关注

    Why not return json? Do something like this:

      public class PartialController : Controller
    {
        [HttpGet]
        public JsonResult Index()
        {
            return Json(new { _PartialView = Guid.NewGuid().ToString()},JsonRequestBehavior.AllowGet);
    
        }
    }
    
    <script type="text/javascript">
    $("#button").on('click', function () {
    
        $.ajax({
            url: "Partial/Index",
            type: "get",
            dataType: "html",
            success: function (result) {
                var json = $.parseJSON(result);
                $('#YourId').remove();
                $('#PartialViewDIV').append('<div id="YourId">' + json._PartialView + '</div>');
                //$("#PartialViewDIV").html(result); //the PartialViewDIV-tag is in the main view, how can i "redirect" it then?
               // console.log('success', data);
            }
        });
    });
    

    评论

报告相同问题?

悬赏问题

  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块