非常简单Controller
public JsonResult A1(Guid? id)
{
var model = new MyModel { Id = g2, Id2 = g2, Name = "A1" };
return new JsonResult
{
Data = new
{
html = this.RenderPartialView("A1", model),
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
public JsonResult A2(Guid? id)
{
ModelState.Clear();
var model = new MyModel { Id = g2, Id2 = g2, Name = "A2" };
return new JsonResult
{
Data = new
{
html = this.RenderPartialView("A1", model),
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
非常简单View,带有两个按钮和脚本
<p><a class="btn btn-default" href="#" onclick="F1(1)">NO ModelState.Clear()</a></p>
<p><a class="btn btn-default" href="#" onclick="F1(2)">ModelState.Clear()</a></p>
function F1(a) {
$.ajax({
type: 'GET',
url: '@Url.Content("~/Home/A")' + a,
data: {
id: '@ModelBug.Controllers.HomeController.g1'
},
dataType: 'json',
beforeSend: function (xhr) {
},
success: function (data) {
$("#col" + a).append('<div class="row"><div class="col-md-12">' + data.html + '</div></div>');
},
error: function () {
}
});
}
非常简单的渲染View
<div>Model.Id = @Model.Id</div>
<div>TextBoxFor(model => model.Id) = @Html.TextBoxFor(model => model.Id)</div>
<div>TextBoxFor(model => Model.Id) = @Html.TextBoxFor(model => Model.Id)</div>
<div></div>
<div>Model.Id2 = @Model.Id2</div>
<div>TextBoxFor(model => model.Id2) = @Html.TextBoxFor(model => model.Id2)</div>
<div>TextBoxFor(model => Model.Id2) = @Html.TextBoxFor(model => Model.Id2)</div>
<div></div>
<div>Model.Name = @Model.Name</div>
<div>TextBoxFor(model => model.Name) = @Html.TextBoxFor(model => model.Name)</div>
<div>TextBoxFor(model => Model.Name) = @Html.TextBoxFor(model => Model.Name)</div>
<div></div>
<div></div>
在没有ModelState.Clear()的TextBoxFor(model =&gt; Model.Id)的结果视图中
show 4a01aadd-c61a-4524-95f6-e88a665c0745(来自param(Guid?id))
必须是d2afb9a8-fb43-4237-9f34-abc7e5b59a41 **(来自公共静态Guid g2)
主要问题
为什么来自param id 的TextBoxFor()中的值不是来自 model.Id ?
P.S。这个问题来自大型项目,其中模型从DataBase获取但在视图模型中.Id与Database,model.Id =查询参数不同。