ModelState 即将出现无效?
ModelState is coming up as invalid?
我正在开发 MVC5 Code-First 应用程序。
在一个模型的 Edit()
视图中,我包含了 [Create]
按钮,用于从 Edit()
视图中向其他模型添加新值,然后在 [=17= 中重新填充新值] 在 Edit()
.
对于第一次尝试,我通过 AJAX 将 model_description
传递到我的控制器方法 createNewModel()
:
[HttpPost]
public JsonResult createNewModel(INV_Models model)
{
// model.model_description is passed in via AJAX -- Ex. 411
model.created_date = DateTime.Now;
model.created_by = System.Environment.UserName;
model.modified_date = DateTime.Now;
model.modified_by = System.Environment.UserName;
// Set ID
int lastModelId = db.INV_Models.Max(mdl => mdl.Id);
model.Id = lastModelId+1;
//if (ModelState.IsValid == false && model.Id > 0)
//{
// ModelState.Clear();
//}
// Attempt to RE-Validate [model], still comes back "Invalid"
TryValidateModel(model);
// Store all errors relating to the ModelState.
var allErrors = ModelState.Values.SelectMany(x => x.Errors);
// I set a watch on [allErrors] and by drilling down into
// [allErrors]=>[Results View]=>[0]=>[ErrorMessage] I get
// "The created_by filed is required", which I'm setting....?
try
{
if (ModelState.IsValid)
{
db.INV_Models.Add(model);
db.SaveChangesAsync();
}
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
return Json(
new { ID = model.Id, Text = model.model_description },
JsonRequestBehavior.AllowGet);
}
我不明白为什么我的 ModelState
会变成 Invalid
?
在 ModelState
检查之前指定所有属性;模型定义如下:
public class INV_Models
{
public int Id { get; set; }
[Required(ErrorMessage = "Please enter a Model Description.")]
public string model_description { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime created_date { get; set; }
[Required]
public string created_by { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime modified_date { get; set; }
public string modified_by { get; set; }
}
编辑:
添加了查看代码:
输入形式:
<span class="control-label col-md-2">Type:</span>
<div class="col-md-4">
@Html.DropDownListFor(model => model.Type_Id, (SelectList)ViewBag.Model_List, "<<< CREATE NEW >>>", htmlAttributes: new { @class = "form-control dropdown" })
@Html.ValidationMessageFor(model => model.Type_Id, "", new { @class = "text-danger" })
</div>
<div class="col-md-1">
<div class="btn-group">
<button type="button" class="btn btn-success" aria-expanded="false">CREATE NEW</button>
</div>
</div>
脚本:
$('#submitNewModel').click(function () {
var form = $(this).closest('form');
var data = { model_description: document.getElementById('textNewModel').value };
$.ajax({
type: "POST",
dataType: "JSON",
url: '@Url.Action("createNewModel", "INV_Assets")',
data: data,
success: function (resp) {
alert("SUCCESS!");
$('#selectModel').append($('<option></option>').val(resp.ID).text(resp.Text));
alert("ID: " + resp.ID + " // New Model: " + resp.Text); // RETURNING 'undefined'...?
form[0].reset();
$('#createModelFormContainer').hide();
},
error: function () {
alert("ERROR!");
}
});
});
当您无法快速推断出 ModelState 验证失败的原因时,快速迭代错误通常会很有帮助。
foreach (ModelState state in ModelState.Values.Where(x => x.Errors.Count > 0)) { }
或者你可以直接找出错误。
var allErrors = ModelState.Values.SelectMany(x => x.Errors);
请记住,ModelState 是在执行 Action 主体之前构建的。因此,无论您在控制器操作中如何设置模型的属性,都已经设置了 IsValid。
如果您希望灵活地手动设置属性然后重新评估对象的有效性,您可以在设置属性后在您的 Action 中手动重新运行验证。如评论中所述,您应该在尝试重新验证之前清除您的 ModelState。
ModelState.Clear();
ValidateModel(model);
try
{
if (ModelState.IsValid)
{
db.INV_Models.Add(model);
db.SaveChangesAsync();
}
}
...
顺便说一句,如果模型仍然无效,ValidateModel(model) 将抛出异常。如果您想防止这种情况发生,请使用 TryValidateModel,其中 returns true/false 改为:
protected internal bool TryValidateModel(Object model)
模型状态是在完成从 post 数据到模型的绑定时计算的。
ModelState.IsValid 属性 只告诉你 ModelState.Errors 是否有错误。
设置创建日期时,您需要从 ModelState.Errors
中删除与之相关的错误
您不应该使用像 ModelState.Clear()
这样的 hack,也不需要 TryValidateModel(model);
。您的问题源于您在 created_date
和 created_by
属性上都有一个 [Required]
属性,但您没有 post 返回一个值,所以它们是 null
并且验证失败。如果你要 post 支持一个更复杂的模型,那么你会使用一个视图模型,它甚至没有 created_date
和 created_by
的属性(它是一个 Create 方法,所以它们不应该一直设置到你 post 回来)。
在你的情况下,视图模型不是必需的,因为你唯一的 posting 返回单个值(对于 model-description
)用于创建新的 INV_Models
模型。
将脚本中的第 2 行更改为
var data = { description: $('#textNewModel').val() };
将您的 post 方法更改为
[HttpPost]
public JsonResult createNewModel(string description)
{
// Initialize a new model and set its properties
INV_Models model = new INV_Models()
{
model_description = description,
created_date = DateTime.Now,
created_by = System.Environment.UserName
};
// the model is valid (all 3 required properties have been set)
try
{
db.INV_Models.Add(model);
db.SaveChangesAsync();
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
return Json( new { ID = model.Id, Text = model.model_description }, JsonRequestBehavior.AllowGet);
}
旁注:
- 我建议
modified_date
为 DateTime?
(在数据库中可为空
还)。您正在创建一个新对象,并且正在设置
created_date
和 created_by
属性,但设置
modified_date
和 modified_by
属性好像没有
合适(还没有修改)。
- 我怀疑你真的不想将
created_by
设置为
System.Environment.UserName
(没有意义
每条记录都设置为 administrator
或任何 UserName
服务器 returns。相反,您需要从 Identity
获取用户名
或者 Membership
无论您使用什么授权系统。
我正在开发 MVC5 Code-First 应用程序。
在一个模型的 Edit()
视图中,我包含了 [Create]
按钮,用于从 Edit()
视图中向其他模型添加新值,然后在 [=17= 中重新填充新值] 在 Edit()
.
对于第一次尝试,我通过 AJAX 将 model_description
传递到我的控制器方法 createNewModel()
:
[HttpPost]
public JsonResult createNewModel(INV_Models model)
{
// model.model_description is passed in via AJAX -- Ex. 411
model.created_date = DateTime.Now;
model.created_by = System.Environment.UserName;
model.modified_date = DateTime.Now;
model.modified_by = System.Environment.UserName;
// Set ID
int lastModelId = db.INV_Models.Max(mdl => mdl.Id);
model.Id = lastModelId+1;
//if (ModelState.IsValid == false && model.Id > 0)
//{
// ModelState.Clear();
//}
// Attempt to RE-Validate [model], still comes back "Invalid"
TryValidateModel(model);
// Store all errors relating to the ModelState.
var allErrors = ModelState.Values.SelectMany(x => x.Errors);
// I set a watch on [allErrors] and by drilling down into
// [allErrors]=>[Results View]=>[0]=>[ErrorMessage] I get
// "The created_by filed is required", which I'm setting....?
try
{
if (ModelState.IsValid)
{
db.INV_Models.Add(model);
db.SaveChangesAsync();
}
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
return Json(
new { ID = model.Id, Text = model.model_description },
JsonRequestBehavior.AllowGet);
}
我不明白为什么我的 ModelState
会变成 Invalid
?
在 ModelState
检查之前指定所有属性;模型定义如下:
public class INV_Models
{
public int Id { get; set; }
[Required(ErrorMessage = "Please enter a Model Description.")]
public string model_description { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime created_date { get; set; }
[Required]
public string created_by { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime modified_date { get; set; }
public string modified_by { get; set; }
}
编辑:
添加了查看代码:
输入形式:
<span class="control-label col-md-2">Type:</span>
<div class="col-md-4">
@Html.DropDownListFor(model => model.Type_Id, (SelectList)ViewBag.Model_List, "<<< CREATE NEW >>>", htmlAttributes: new { @class = "form-control dropdown" })
@Html.ValidationMessageFor(model => model.Type_Id, "", new { @class = "text-danger" })
</div>
<div class="col-md-1">
<div class="btn-group">
<button type="button" class="btn btn-success" aria-expanded="false">CREATE NEW</button>
</div>
</div>
脚本:
$('#submitNewModel').click(function () {
var form = $(this).closest('form');
var data = { model_description: document.getElementById('textNewModel').value };
$.ajax({
type: "POST",
dataType: "JSON",
url: '@Url.Action("createNewModel", "INV_Assets")',
data: data,
success: function (resp) {
alert("SUCCESS!");
$('#selectModel').append($('<option></option>').val(resp.ID).text(resp.Text));
alert("ID: " + resp.ID + " // New Model: " + resp.Text); // RETURNING 'undefined'...?
form[0].reset();
$('#createModelFormContainer').hide();
},
error: function () {
alert("ERROR!");
}
});
});
当您无法快速推断出 ModelState 验证失败的原因时,快速迭代错误通常会很有帮助。
foreach (ModelState state in ModelState.Values.Where(x => x.Errors.Count > 0)) { }
或者你可以直接找出错误。
var allErrors = ModelState.Values.SelectMany(x => x.Errors);
请记住,ModelState 是在执行 Action 主体之前构建的。因此,无论您在控制器操作中如何设置模型的属性,都已经设置了 IsValid。
如果您希望灵活地手动设置属性然后重新评估对象的有效性,您可以在设置属性后在您的 Action 中手动重新运行验证。如评论中所述,您应该在尝试重新验证之前清除您的 ModelState。
ModelState.Clear();
ValidateModel(model);
try
{
if (ModelState.IsValid)
{
db.INV_Models.Add(model);
db.SaveChangesAsync();
}
}
...
顺便说一句,如果模型仍然无效,ValidateModel(model) 将抛出异常。如果您想防止这种情况发生,请使用 TryValidateModel,其中 returns true/false 改为:
protected internal bool TryValidateModel(Object model)
模型状态是在完成从 post 数据到模型的绑定时计算的。 ModelState.IsValid 属性 只告诉你 ModelState.Errors 是否有错误。
设置创建日期时,您需要从 ModelState.Errors
中删除与之相关的错误您不应该使用像 ModelState.Clear()
这样的 hack,也不需要 TryValidateModel(model);
。您的问题源于您在 created_date
和 created_by
属性上都有一个 [Required]
属性,但您没有 post 返回一个值,所以它们是 null
并且验证失败。如果你要 post 支持一个更复杂的模型,那么你会使用一个视图模型,它甚至没有 created_date
和 created_by
的属性(它是一个 Create 方法,所以它们不应该一直设置到你 post 回来)。
在你的情况下,视图模型不是必需的,因为你唯一的 posting 返回单个值(对于 model-description
)用于创建新的 INV_Models
模型。
将脚本中的第 2 行更改为
var data = { description: $('#textNewModel').val() };
将您的 post 方法更改为
[HttpPost]
public JsonResult createNewModel(string description)
{
// Initialize a new model and set its properties
INV_Models model = new INV_Models()
{
model_description = description,
created_date = DateTime.Now,
created_by = System.Environment.UserName
};
// the model is valid (all 3 required properties have been set)
try
{
db.INV_Models.Add(model);
db.SaveChangesAsync();
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
return Json( new { ID = model.Id, Text = model.model_description }, JsonRequestBehavior.AllowGet);
}
旁注:
- 我建议
modified_date
为DateTime?
(在数据库中可为空 还)。您正在创建一个新对象,并且正在设置created_date
和created_by
属性,但设置modified_date
和modified_by
属性好像没有 合适(还没有修改)。 - 我怀疑你真的不想将
created_by
设置为System.Environment.UserName
(没有意义 每条记录都设置为administrator
或任何UserName
服务器 returns。相反,您需要从Identity
获取用户名 或者Membership
无论您使用什么授权系统。