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

Dug*_*gie 3 asp.net-mvc-5

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

我在视图中有一个表单,并且有一个像子表单一样使用的部分视图。部分视图用于显示项目的 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');
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

部分视图的代码是

@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 />
    }
Run Code Online (Sandbox Code Playgroud)

这是方法

[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);
}   
Run Code Online (Sandbox Code Playgroud)

这就是模型

 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; }
 }
Run Code Online (Sandbox Code Playgroud)

这是示例的屏幕截图。看来我还不允许嵌入图像。

截屏

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

Sli*_*sim 7

当发布数据然后在同一请求中重新显示数据时,ModelState 将使用原始发布中的数据填充。

这可能会导致本应删除的项目仍然显示,或者表单现在应该为空白而被预先填写。

添加:

ModelState.Clear()
Run Code Online (Sandbox Code Playgroud)

在重新显示数据之前将清除模型状态并防止标签助手从原始发布请求中填充自己