我已经检查了一些类似的问题,但似乎都没有回答我的问题,因此希望有人可以帮助我。
我在视图中有一个表单,而我正在使用的局部视图就像一个子表单。部分视图用于显示项目的iList。 (下面的屏幕截图显示了它的显示方式。)
在部分视图中,每个项目都有一个复选框,用户可以选中该复选框以将其删除。如果我选中第一个项目的复选框,则第一个项目将从代码列表中删除,但是当模型传递回View时,返回的项目是错误的(选中的项目)。
因此,在下面的示例中,如果我检查第一个项目(无应答延迟= 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; }
}
这是该示例的屏幕截图。看来我还不允许嵌入图像。
希望有人可以指出我要去哪里。
答案 0 :(得分:0)
在发布数据然后在同一请求中重新显示数据时,将使用原始帖子中的数据填充ModelState。
这可能会导致本应删除的项目仍然显示或在表格现在为空白时预填表格的情况。
添加:
ModelState.Clear()
在重新显示数据之前,将清除模型状态并防止标签助手从原始发布请求中填充自身