Asp.net mvc 5 局部视图在更新后显示错误的数据(asp.net MVC5)

Asp.net mvc 5 局部视图在更新后显示错误的数据(asp.net MVC5),asp.net-mvc-5,Asp.net Mvc 5,这里有一些类似的问题,我已经检查过了,但似乎没有一个能回答我的问题,所以这里希望有人能帮助我 我在视图中有一个表单,我使用的部分视图就像子表单一样。局部视图用于显示项目的iList。(下面的屏幕截图显示了这是如何出现的) 在部分视图中,每个项目都有一个复选框,用户可以选中该复选框将其删除。如果我选中第一个项目的复选框,那么第一个项目将从代码中的列表中删除,但是当模型传递回视图时,返回的是错误的项目(选中的项目) 因此,在下面的示例中,如果我检查第一个项目(无回答延迟=18)并提交,则该项目将保留

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

我在视图中有一个表单,我使用的部分视图就像子表单一样。局部视图用于显示项目的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');
            }
        });
    }
这就是模型

 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; }
 }
公共类调用功能模型:UIPluginBaseModel
{
[显示(Name=“电话号码”)]
公共字符串phoneNumber{get;set;}
[显示(Name=“密码”)]
公共字符串密码{get;set;}
[显示(Name=“隐藏呼叫者ID”)]
公共布尔隐藏点goingcallerid{get;set;}
[显示(Name=“呼叫等待”)]
公共bool callWaiting{get;set;}
[显示(Name=“Message”)]
公共字符串消息{get;set;}
公共图书馆


希望有人能指出我的错误所在。

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

这可能会导致本应删除的项目仍然显示,或者表单现在应该为空时,却正在预填充

添加:

ModelState.Clear()

在重新显示数据之前,将清除模型状态并防止标记帮助程序从原始post请求填充自己

您可以共享用于提交的控制器方法吗?您共享的控制器方法看起来就像在“更新按钮”时刷新列表一样您没有将其描述为流程的一部分。如果您在使用某个内容作为帖子的结果时得到奇怪的结果,我也会使用ModelState.Clear()来查看显示的是否是旧数据。它可能会帮助您跟踪issueHi@Apaple,我包含的方法(基本上创建了一个处理按钮点击的方法-这就是我提交的方式)是我正在使用的版本的净化版,因为原始版本中有一些与工作相关的内容我无法共享。谢谢@Slicksim,我在返回PartialView之前尝试将其添加到中,但没有效果。@Slicksim,忘记我最后的评论,这已修复,太棒了,谢谢你。如果你能将其添加为解决方案,我可以接受它。
 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; }
 }
ModelState.Clear()