简体   繁体   English

在ASP.Net MVC中的操作后替换partialview

[英]Replace partialview after action in ASP.Net MVC

I'm still quite new to ASP.NET MVC and wonder how-to achieve the following: On a normal view as part of my master page, I create a varying number of partial views with a loop, each representing an item the user should be able to vote for. 我仍然是ASP.NET MVC的新手并且想知道如何实现以下内容:在作为母版页的一部分的普通视图中,我使用循环创建不同数量的局部视图,每个视图代表用户应该使用的项目能够投票。 After clicking the vote-button, the rating shall be submitted to the database and afterwards, the particular partial view which the user clicked shall be replaced by the same view, with some visual properties changed. 点击投票按钮后,评级应提交给数据库,之后,用户点击的特定局部视图将被同一视图替换,并改变一些视觉属性。 What is the best practice to achieve this? 实现这一目标的最佳做法是什么?

Here's how I started: 1. I defined the partial view with an if-sentence, distinguishing between the visual appearance, depending on a flag in the particular viewmodel. 这是我开始的方式:1。我使用if-sentence定义局部视图,根据特定视图模型中的标志区分视觉外观。 Hence, if the flag is positive, voting controls are displayed, if it's negative, they're not. 因此,如果标志为正,则显示投票控制,如果是负数,则不显示。

  1. I assigned a Url.Action(..) to the voting buttons which trigger a controller method. 我将Url.Action(..)分配给了触发控制器方法的投票按钮。 In this method, the new rating is added to the database. 在此方法中,新的评级将添加到数据库中。

  2. In the controller method, I return the PartialView with the updated ViewModel. 在控制器方法中,我返回带有更新的ViewModel的PartialView。 UNFORTUNATELY, the whole view get's replaced, not only the partial view. 不幸的是,整个视图被取代,而不仅仅是部分视图。

Any suggestions how-to solve this particular problem or how-to achieve the whole thing would be highly appreciated. 任何建议如何解决这个特定问题或如何实现整个事情将受到高度赞赏。

Thanks very much, Chris 非常感谢,克里斯

Trivial (but by all means correct and usable) solution to your problem is Ajax.BeginForm() helper for voting. 琐碎(但无论如何正确和可用)解决您的问题的方法是Ajax.BeginForm()帮助投票。 This way you change your voting to ajax calls, and you can easily specify, that the result returned by this call (from your voting action, which will return partial view with only 1 changed item) will be used to replace old content (for example one particular div containing old item before voting). 通过这种方式,您可以将投票更改为ajax调用,并且您可以轻松指定此调用返回的结果(来自您的投票操作,将返回仅包含1个更改项的部分视图)将用于替换旧内容(例如一个特殊的div包含投票前的旧项目)。

Update - 11/30/2016 更新 - 2016年11月30日

For example: 例如:

@using (Ajax.BeginForm("SomeAction", "SomeController", new { someRouteParam = Model.Foo }, new AjaxOptions { UpdateTargetId = "SomeHtmlElementId", HttpMethod = "Post" }))

ASP.NET MVC is a perfect framework for this kind of needs. ASP.NET MVC是满足此类需求的完美框架。 What I would do if I were in your possition is to work with JQuery Ajax API. 如果我能够做到的话,我会做的就是使用JQuery Ajax API。

Following blog post should give you a hint on what you can do with PartialViews, JQuery and Ajax calls to the server : 以下博客文章应该为您提供有关使用PartialViews,JQuery和Ajax调用服务器可以执行的操作的提示:

http://www.tugberkugurlu.com/archive/working-with-jquery-ajax-api-on-asp-net-mvc-3-0-power-of-json-jquery-and-asp-net-mvc-partial-views http://www.tugberkugurlu.com/archive/working-with-jquery-ajax-api-on-asp-net-mvc-3-0-power-of-json-jquery-and-asp-net-mvc-局部视图

UPDATE UPDATE

It has been asked to put a brief intro so here it is. 有人要求在这里简要介绍一下。

The following code is your action method : 以下代码是您的操作方法:

    [HttpPost]
    public ActionResult toogleIsDone(int itemId) {

        //Getting the item according to itemId param
        var model = _entities.ToDoTBs.FirstOrDefault(x => x.ToDoItemID == itemId);
        //toggling the IsDone property
        model.IsDone = !model.IsDone;

        //Making the change on the db and saving
        ObjectStateEntry osmEntry = _entities.ObjectStateManager.GetObjectStateEntry(model);
        osmEntry.ChangeState(EntityState.Modified);
        _entities.SaveChanges();

        var updatedModel = _entities.ToDoTBs;

        //returning the new template as json result
        return Json(new { data = this.RenderPartialViewToString("_ToDoDBListPartial", updatedModel) });
    } 

RenderPartialViewToString is an extension method for controller. RenderPartialViewToString是控制器的扩展方法。 You need to use Nuget here to bring down a very small package called TugberkUg.MVC which will have a Controller extension for us to convert partial views to string inside the controller. 你需要在这里使用Nuget来关闭一个名为TugberkUg.MVC的非常小的包,它将为我们提供一个Controller扩展,将部分视图转换为控制器内的字符串。

Then here is a brief info on how you can call it with JQuery : 接下来是一个关于如何使用JQuery调用它的简要信息:

var itemId = element.attr("data-tododb-itemid");
var d = "itemId=" + itemId;
var actionURL = '@Url.Action("toogleIsDone", "ToDo")';

$("#ajax-progress-dialog").dialog("open");

$.ajax({
    type: "POST",
    url: actionURL,
    data: d,
    success: function (r) {
        $("#to-do-db-list-container").html(r.data);
    },
    complete: function () {
        $("#ajax-progress-dialog").dialog("close");
        $(".isDone").bind("click", function (event) {
            toggleIsDone(event, $(this));
        });
    },
    error: function (req, status, error) {
        //do what you need to do here if an error occurs
        $("#ajax-progress-dialog").dialog("close");
    }
});

There needs to be some extra steps to be taken. 需要采取一些额外的步骤。 So look at the blog post which has the complete walkthrough. 请查看具有完整演练的博客文章。

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

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