通过ajax post更新部分视图时更新主视图模型

时间:2013-02-18 15:16:41

标签: asp.net-mvc jquery razor

我正在尝试通过ajax帖子更新部分视图时页面上的viewmodel进行更新。部分视图正确更新,但在下次更新调用时,模型看到已恢复到原始状态。

部分视图是一个表,我要么添加要么删除一行。关于如何做到这一点,密码包括在任何想法之下。

页面代码是     
                                      

  •                 
  •                 
  •             

            <div class="filters">
                <fieldset class="source">
                    <legend>Search Attributes</legend>
                    <div id="attributes-filter">
                        @Html.Partial("_EditSearchQuery")
                    </div>
                </fieldset>
            </div>
            <div>              
                <a id="addRowLink" class="add-row-link" href="#">Add new clause</a>                
            </div>
        </div>
    </div>
    

    - 部分视图

    <table id="searchClauses" class="clauses">
    <tbody>
        <tr class="header">
            <td class="add-remove"></td>                                                      
            <td class="logical">And/Or</td>
            <td class="field">Field</td>
            <td class="operator">Operator</td>
            <td class="value">Value</td>
        </tr>
            @foreach (SearchClause item in Model.searchClauses)
            { 
                <tr class="clause clause-row" id=@item.RowID>
                    <td class="add-remove">   
                        <a href="#" title="Remove this filter line" id=@item.ID >Delete</a>                      
                    </td>
                    <td>  
                        @Html.DropDownListFor(modelitem => item.logicalTypeValue, new SelectList(item.logicalTypeList, "Value", "Text", "Selected"), new { style = "width: 60px" })                                                                
                    </td>
                    <td>
                        @Html.DropDownListFor(modelitem => item.fieldListValue, new SelectList(item.fieldList, "Value", "Text", "Selected"))                               
                    </td>
                    <td>                              
                        @Html.DropDownListFor(modelitem => item.operatorListValue, new SelectList(item.operatorList, "Value", "Text", "Selected"), new { style = "width: 60px" })                    
                    </td>
                    <td>                               
                        @Html.TextBoxFor(modelitem => item.valuesList[0], new { style = "width: 130px" })
                    </td>
                </tr>    
            }                   
    </tbody>
    

    - 脚本

    <script type="text/javascript">     
    $(function () {
        // Save quiz view - new or existing.
        $("#attributes-filter").on("click", "a", function () { // A jQuery delegated event - #EditQuiz is always present, a.SaveQuiz only exists when the _ElearningQuiz partial view is loaded.
            deleteRow($(this).attr("id"));
        });
    
        function deleteRow (id) {
            var rowData = {
                'id': id,
                'model' : @Html.Raw(Json.Encode(Model))
                };
    
            $.ajax({
                url: "/Participant/DeleteClause",
                type: "POST",
                data: JSON.stringify(rowData),
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    $("#attributes-filter").html(result);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("Status: " + textStatus + " Error: " + errorThrown);
                }
            });
        };
    
        $("#addRowLink").click(function () {
            var model = @Html.Raw(Json.Encode(Model))
    
            $.ajax({
                url: "/Participant/AddClause",
                type: "POST",
                data: JSON.stringify(model),
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    $("#attributes-filter").html(result);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("Status: " + textStatus + " Error: " + errorThrown);
                }
            });
        });
    }); 
    </script>
    

    - 控制器

    [HttpPost]
        public ActionResult AddClause(DynamicSearchModel model)
        {
            int campaignId = SessionManager.CampaignId;
            int clientId = SessionManager.ClientId;
    
            var newClause = _participantServiceClient.NewSearchClause(campaignId, clientId, 2);
            newClause.ID = model.searchClauses.Count + 1;
    
            model.searchClauses.Add(newClause);
    
            return PartialView("_EditSearchQuery", model);
        }
    
        [HttpPost]
        public ActionResult DeleteClause(string id, DynamicSearchModel model)
        {
            int _id = int.Parse(id);
    
            model.searchClauses.RemoveAt(_id - 1);
    
            return PartialView("_EditSearchQuery", model);
        }
    

    0 个答案:

    没有答案