【问题标题】:Ajax load partialview accepting a parameterAjax加载partialview接受参数
【发布时间】:2011-12-07 07:51:57
【问题描述】:

我有一个从数据库表 PartList 中以 htmltable 格式列出部件的视图。 htmltable 包含一个链接,用于向数据库表 Bom 输入值。单击链接时,它应该加载该特定部分的局部视图。 partialview 包含一个用于选择子部件的下拉列表、一个用于输入数量的文本框和一个显示该部件的其他 bom 输入的 htmltable。我感到震惊的是我需要使用 ajax 加载接受 partId 的部分视图。部分视图应加载到 listPart 视图中的 div。如何做到这一点?

【问题讨论】:

  • 你需要阅读this并关注演示

标签: jquery asp.net-mvc ajax partial-views


【解决方案1】:

没有显示当前代码,您不清楚您的问题。据我了解,这篇博文应该能给你一个思路:

Working With JQuery Ajax API on ASP.NET MVC 3.0 - Power of JSON, JQuery and ASP.NET MVC Partial Views

你需要做的很简单:

  1. 对您的控制器操作进行 ajax 调用:

    $.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");
        }
    });
    
  2. 返回您的部分视图:

    [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 是一个控制器扩展,它在控制器中呈现局部视图并返回一个字符串值作为输出。您将在博文中找到所有代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 2017-06-25
    • 2015-06-08
    • 2012-10-20
    相关资源
    最近更新 更多