你可以更新的局部视图,而不是整页文章?(Can you just update a partial

2019-07-20 18:20发布

有没有办法提交在asp.net mvc的局部视图形式无需刷新父页面,但只有重装到新状态的局部视图? 类似于如何使用knockout.js数据绑定更新。

我的数据表的列/名称数量可变的呈现,所以我不认为knockout.js是这一个选项,所以我试图用一个局部视图来代替。

Answer 1:

不是没有jQuery的。

什么你必须做的就是把你的部分在一个div,像这样:

<div id="partial">
    @Html.Partial("YourPartial")
</div>

然后,更新(例如点击带有ID按钮button ),你可以这样做:

$("#button").click(function () {
   $.ajax({
       url: "YourController/GetData",
       type: "get",
       data: $("form").serialize(), //if you need to post Model data, use this
       success: function (result) {
           $("#partial").html(result);
       }
   });
})

然后,你的行动看起来是这样的:

public ActionResult GetData(YourModel model) //that's if you need the model
{
    //do whatever

    return View(model);
}


Answer 2:

其实,如果你偏生了孩子的操作方法,你可以发布(或者甚至可以使用一个锚链接),直接给孩子的行动,并得到类似的Ajax的影响。 我们这样做的几点看法。

语法

@Html.Action("MyPartial")

儿童的行动

public ActionResult MyPartial()
{
    return PartialView(Model);
}

如果您的表单提交给孩子行动

@using (Html.BeginForm("MyPartial"))
{
    ...
}

局部视图会从孩子的行动返回的局部视图进行更新。

jQuery是仍然更新部分合法的方式。 但在技术上,在回答你的问题是肯定的。



Answer 3:

我会使用局部视图和@ html.RenderPartial(“partialName”)使用这样的场景阿贾克斯表单辅助部分助手



Answer 4:

由于正常看东西时,这样的事情是人给予太多的信息有限,所以我会尝试在这里帮我找到。 关键是要建立一个div一个ID可以追加返回的HTML。 还打你的控制器时,确保它返回的部分。 有这种方法,但一个很好的一天它应该工作的一些潜在的问题。

<div id="CategoryList" class="widget">
    @{
        Html.RenderPartial("WidgetCategories.cshtml");
    }
</div>

          function DeleteCategory(CategoryID) {
            $.get('/Dashboard/DeleteWidgetCategory?CategoryID=' + CategoryID,
                function (data) {
                    if (data == "No") {
                        alert('The Category has report widgets assigned to it and cannot be deleted.');
                    }
                    else {
                        $('#CategoryList').html(data);
                    }

                }
            );
        }


    [HttpGet("DeleteWidgetCategory")]
    [HttpPost("DeleteWidgetCategory")]
    public IActionResult DeleteWidgetCategory(string CategoryID)
    {
        string Deleted = CategoryModel.DeleteCategory(CategoryID);

        if (Deleted == "Yes")
        {
            return PartialView("WidgetCategories");
        }
        else
        {
            return this.Json("No");
        }
    }


文章来源: Can you just update a partial view instead of full page post?