简体   繁体   English

自刷新局部视图

[英]Self refreshing partial view

I set up a small demo application for refreshing partial views via jQuery/Ajax. 我设置了一个小型演示应用程序,用于通过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! 先感谢您!

Why not return json? 为什么不返回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);
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM