部分视图在更新后显示错误数据(asp.net MVC5)

Partial View displays wrong data after update (asp.net MVC5)

这里有几个类似的问题,我已经检查过,但 none 似乎回答了我的问题,所以希望有人能帮助我。

我在一个视图中有一个表单,并且我正在使用一个像子表单一样的局部视图。 Partial 视图用于显示项目的 iList。 (下面的屏幕截图显示了它的显示方式)。

在部分视图中,每个项目都有一个复选框,用户可以选中以将其删除。如果我选中第一项的复选框,则第一项从代码列表中删除,但是当模型传递回视图时,返回的是错误的项(选中的项)。

因此在下面的示例中,如果我选中第一项(无应答延迟 = 18)并提交,则同一项目会保留在页面上,而另一项(无应答延迟 = 10)会消失。如果我随后重新加载所有数据,则会出现正确的项目(无应答延迟 = 10)。

我检查了正确的数据被传回的方法,但错误的项目仍然在页面上。如果我随后刷新页面,则会出现正确的项目。请注意,该方法已经过一些清理,但正确的项目确实已从数据库中删除。

还有一点要注意,这是第三方产品的插件,所以我无法 运行 它,除非我发布到其他产品,这使得调试变得棘手。

主视图的代码是

@using(Html.BeginForm("SaveCallFeatures", "CallFeatures", FormMethod.Post, new { id = "CallFeatures", name = "CallFeatures" }))
{
    @Html.AntiForgeryToken()

    <div>
        <h2>Call Features</h2>

        <div class="form-panel">
            <h4>Telephone Call Features</h4>

            <div>
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                @Html.LabelFor(model => model.phoneNumber, htmlAttributes: new { @class = "label" })
                @Html.EditorFor(model => model.phoneNumber, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                @Html.ValidationMessageFor(model => model.phoneNumber, "", new { @class = "text-danger" })
            </div>

            <div>
                @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "label" })
                @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
            </div>

            <div>
                @Html.LabelFor(model => model.hideOutgoingCallerID, htmlAttributes: new { @class = "label" })
                @Html.CheckBoxFor(model => model.hideOutgoingCallerID, new { htmlAttributes = new { @class = "form-control" } })
            </div>

            <div>
                @Html.LabelFor(model => model.callWaiting, htmlAttributes: new { @class = "label" })
                @Html.CheckBoxFor(model => model.callWaiting, new { htmlAttributes = new { @class = "form-control" } })
            </div>
        </div>

        <div id="ForwardRules">
            @Html.Partial("_ForwardingRules")
        </div>

    </div> //form

    @Html.TextArea("Test")

    <div id="form-buttons" class="col-md-offset-4 col-md-6">
        <input type="button" value="Save" id="save-button" class="btn btn-primary" />
    </div>


<script type="text/javascript">
    $("#update-button").on('click', function () {
        GetFwdRules();
    });
</script>




function GetFwdRules() {
        $.ajax
        ({
            url: '@Url.Action("GetFwdRules", "CallFeatures", new { boid = Model.CompanyId })',
            method: 'GET',
            data: $("#CallFeatures").serialize(),
            cache: false,
            success: function (returnData) {
                $("#ForwardRules").html(returnData);
                $("#Test").html(returnData);
                alert('GetFwdRules');
            },
            failure: function () {
                alert('GetFwdRules Failure');
            }
        });
    }

局部视图的代码是

@model XXXXX.Models.CallFeaturesModel

<div class="form-panel">
<h4>Active Forwarding Rules</h4>

    @for(int i = 0; i < Model.FwdRules.Count; i++)
    {
        <div>
            @Html.HiddenFor(model => Model.FwdRules[i].ForwardingRuleID)
        </div>
        <div>
            @Html.LabelFor(model => Model.FwdRules[i].Condition)
            @Html.TextBoxFor(model => Model.FwdRules[i].Condition, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
        </div>
        <div>
            @Html.LabelFor(model => Model.FwdRules[i].Destination)
            @Html.TextBoxFor(model => Model.FwdRules[i].Destination, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
        </div>
        <div>
            @Html.LabelFor(model => Model.FwdRules[i].NoAnswerDelay)
            @Html.TextBoxFor(model => Model.FwdRules[i].NoAnswerDelay)
            @Html.DescriptionFor(model => model.FwdRules[i].NoAnswerDelay)
        </div>
        <div>
            @Html.LabelFor(model => Model.FwdRules[i].ToDelete)
            @Html.CheckBoxFor(model => Model.FwdRules[i].ToDelete)
        </div>
        <br />
    }

这是方法

[HttpGet]
public ActionResult GetFwdRules(CallFeaturesModel CFModel)
{
  // Refresh the list to include on those where the ToDelete variable == false (checkbox is unchecked)
  CFModel.FwdRules = CFModel.FwdRules.Where(x => x.ToDelete == false).ToList();
  return PartialView("_ForwardingRules", CFModel);
}   

这是模型

 public class CallFeaturesModel : UIPluginBaseModel
 {
   [Display(Name = "Phone Number")]
   public string phoneNumber { get; set; }

   [Display(Name = "Password")]
   public string password { get; set; }

   [Display(Name = "Hide Outgoing Caller ID")]
   public bool hideOutgoingCallerID { get; set; }

   [Display(Name = "Call Waiting")]
   public bool callWaiting { get; set; }

   [Display(Name = "Message")]
   public string Message { get; set; }

   public IList<ActiveForwardRule> FwdRules { get; set; }
 }

public class ActiveForwardRule
{
  [Display(Name = "Rule ID")]
   public string ForwardingRuleID { get; set; }

   [Display(Name = "Condition")]
   public string Condition { get; set; }

   [Display(Name = "Destination")]
   public string Destination { get; set; }

   [Display(Name = "No Answer Delay", Description = " seconds (approx. 6 seconds for each ring cycle)")]
   public int NoAnswerDelay { get; set; }

   [Display(Name = "Delete")]
   public bool ToDelete { get; set; }
 }

这是示例的屏幕截图。看来我还不能嵌入图片。

希望有人能指出我哪里错了。

当 post 在同一请求中输入数据然后重新显示数据时,ModelState 将填充原始 post 中的数据。

这可能会导致本应删除的项目仍然显示的情况,或者在本应为空白的情况下预填的表格。

添加:

ModelState.Clear()

在重新显示数据之前将清除模型状态并防止标签助手从原始 post 请求

填充自己