[英]Dropdown Custom Validation message being overriden in MVC
I am a MVC noob and I am trying to validate dropdownlists and show custom Error message by adding ModelState.modelerror. 我是MVC菜鸟,并且尝试通过添加ModelState.modelerror验证下拉列表并显示自定义错误消息。 Internally my field "Board" which is a dropdown list shown on screen is called "ProviderId".
在我内部,字段“ Board”是屏幕上显示的下拉列表,称为“ ProviderId”。 On the UI, I want to see the message "Board cannot be empty" when i dont select a value from the dropdown , but I just see "ProviderId field is required" .
在UI上,当我没有从下拉列表中选择一个值时,我希望看到消息“ Board不能为空”,但我只看到“ ProviderId字段为必填”。 This method seems to work for other edit fields.
此方法似乎适用于其他编辑字段。
This is my Controller Code 这是我的控制器代码
[HttpPost]
public ActionResult Create(CourseList courselist)
{
//This works! It shows Class Name cannot be Empty next to class field on submit
if (courselist.CourseName == null)
{
ModelState.AddModelError("CourseName", " Class Name cannot be Empty");
ViewBag.ProviderID = new SelectList(db.ProviderLists, "ProviderID", "ProviderName", courselist.ProviderID);
return View(courselist);
}
//This does not work! It shows the internal binding message instead of this custom message
if (courselist.ProviderID == null)
{
ModelState.AddModelError("ProviderID", " Board cannot be Empty");
ViewBag.ProviderID = new SelectList(db.ProviderLists, "ProviderID", "ProviderName", courselist.ProviderID);
return View(courselist);
}
if (ModelState.IsValid)
{
db.CourseLists.Add(courselist);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ProviderID = new SelectList(db.ProviderLists, "ProviderID", "ProviderName", courselist.ProviderID);
return View(courselist);
}
This is my relevant View code 这是我的相关查看代码
<div class="editor-field">
@Html.DropDownList("ProviderID", String.Empty)
@Html.ValidationMessageFor(model => model.ProviderID)
</div>
It works fine for the other two edit fields ie it prints the custom message next to the field when I submit, but it always puts the internal binding message "The providerId field is required" for the dropdown list. 对于其他两个编辑字段,它工作正常,即在我提交时在该字段旁边打印自定义消息,但始终将内部绑定消息“ providerId字段为必需”作为下拉列表。 What do I have to do to change the message to the custom added message.
我必须怎么做才能将消息更改为自定义添加的消息。 This happens to all my dropdownlists in the code!
这发生在代码中所有我的下拉列表中! The ProviderId is a required field in the DB, and henc im seeing the message but how do I override it to show a custom message?
ProviderId是数据库中的必填字段,可能会立即看到该消息,但是如何覆盖它以显示自定义消息? Maybe this is a simple fix, but I am a total noob(2 hours) to MVC and want to fix this
也许这是一个简单的修复程序,但是我对MVC总体上不满意(2小时),并且想解决此问题
"Board cannot be empty"
“木板不能为空”
EDITED : the field is ProviderID 编辑 :该字段是ProviderID
I guess, you are using database-first (or model-first) approach and so you have an edmx
file and the corresponding .Designer.cs
file. 我想,你正在使用的数据库第一(或模型一)的方式,所以你有一个
edmx
文件和相应的.Designer.cs
文件。 In this last file, you probably have something like this: 在最后一个文件中,您可能会有类似以下内容:
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 ProviderID
Here the type int
is not nullable and the property has the metadata attribute IsNullable=false
. 在这里,类型
int
不可为空,并且属性具有元数据属性IsNullable=false
。 That's why, ASP.NET MVC gives the default error message. 这就是ASP.NET MVC提供默认错误消息的原因。
A simple workaround is to set as not required the field on the db, then update the model. 一个简单的解决方法是将db上的字段设置为非必需字段,然后更新模型。 After that, you should see in your
.Designer.cs
something like this: 之后,您应该在
.Designer.cs
看到以下内容:
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public Nullable<global::System.Int32> ProviderID
Now, you can see your custom message "Board cannot be empty". 现在,您可以看到自定义消息“ Board不能为空”。
A better approach (instead of changing db and model) is to delete from your controller if (courselist.ProviderID == null)...
and to use metadata. 更好的方法(而不是更改数据库和模型)是从
if (courselist.ProviderID == null)...
删除并使用元数据。 Here's a snippet: 这是一个片段:
[MetadataType(typeof(YourEntityMetaData))]
public partial class YourEntity
{
}
public class YourEntityMetaData
{
[Required(ErrorMessage = "Board cannot be empty")]
public int ProviderID { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.