简体   繁体   English

MVC4,使用jQuery在对话框中渲染部分视图,如何刷新

[英]MVC4, Render Partial View in Dialog Box with jQuery, How to refresh

I have a partial view that is popping up in a dialog with the code below. 我有一个局部视图,该对话框在带有以下代码的对话框中弹出。 However, after the user saves the partial view the data does not refresh in that partial view when I click the ActionLink again until I stop debugging and restart the app. 但是,在用户保存部分视图之后,当我再次单击ActionLink之前,直到停止调试并重新启动应用程序之前,数据不会在该部分视图中刷新。 However, the new record is in my databsae. 但是,新记录在我的数据库中。 The other issue is when I restart the app I cannot update the record because of the error below. 另一个问题是,当我重新启动应用程序时,由于以下错误,我无法更新记录。 What am I missing? 我想念什么? Thanks. 谢谢。

    An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

The div tag in my View 我的视图中的div标签

    <div id="assign-dialog"></div>

The ActionLink in the same view 同一视图中的ActionLink

    @Html.ActionLink("Assign", "Edit", "Assignment", new { id = Model.InfoId}, new { @class = "assign-modal" })

The jQuery jQuery的

    $(function () {

$(function () {
    $('#assign-dialog').dialog({
        autoOpen: false,
        width: 400,
        height: 500,
        resizable: true,
        modal: true
    });

    $('.assign-modal').click(function () {
        $('#assign-dialog').load(this.href, function () {
            $(this).dialog('open');
        });
        return false;
    });

}); });

The HTTP GET action HTTP GET操作

    [HttpGet]
    [Authorize(Roles="Admin")]
    public ActionResult ViewAssignment(int id = 0)
    {
            RequestAssignment query = _assignmentRepository.GetCurrentAssignment(id);

            return PartialView("_ViewAssignment", query)
    }

UPDATE: 更新:

I originally followed the steps in javascript/jquery modal popup dialog MVC 4 / render partial view under the dynamic section of Jasen's answer, but did not know what to put in the "Client Partial, empty for now " 我最初遵循的是javascript / jquery模态弹出对话框MVC 4中的步骤/在Jasen的答案的动态部分下呈现部分视图 ,但不知道要在“客户端部分,现在为空”中添加什么

...okay so going off some other posts I have read this is what I was able to come up with, but nothing happens when I click my link. ...好吧,所以从其他一些文章中我读到了这就是我能想到的内容,但是当我单击链接时什么也没有发生。

View html 查看HTML

    <a href="#" class="dialog-trigger" data-infoId="@Model.InfoId">Assign</a>
    <div id="assign-modal">

    </div>

jQuery jQuery的

    //Dialog Box for Assignments
 $(".dialog-trigger").on("click", function(event) {
    event.preventDefault();
    var infoId= $(this).data("infoId");
    $.ajax({
        url: "RequestAssignment/Edit/" + infoId,
        type: "GET"
    })
    .done(function(result) {
        $("#assign-modal").html(result).dialog("open");
    });
});

so after some long searching, I decided to use Ajax.ActionLink. 因此,经过长时间的搜索,我决定使用Ajax.ActionLink。 I know there are better ways out there, but this one was working for me. 我知道那里有更好的方法,但这是为我工作的方法。 I want to thank @MattBodily for all the help along the way. 我要感谢@MattBodily在此过程中提供的所有帮助。

    @Ajax.ActionLink("Approve", "QuickAssign", "Assignment", new { id = Model.InfoId}, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "result", InsertionMode = InsertionMode.Replace, OnSuccess = "openDialog" })

then my javascript function 然后我的JavaScript函数

function openDialog() {

     //set the diaglog properties
     $("#result").dialog({
         title: 'Assign',
         width: 550,
         height: 'auto',
         modal: true
     });


     $("#result").dialog("open");
}

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

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