验证ASP.NET MVC中的字符串数组 [英] Validate String Array In ASP.NET MVC
本文介绍了验证ASP.NET MVC中的字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用ASP.NET MVC.我如何在我的视图模型中验证字符串数组.因为必填"属性不适用于字符串数组.
I use ASP.NET MVC. How can i validate string array in my view model. Because "Required" attribute doesn't work with string array.
[DisplayName("Content Name")]
[Required(ErrorMessage = "Content name is required")]
public string[] ContentName { get; set; }
推荐答案
您应使用自定义验证
[HttpPost]
public ActionResult Index(TestModel model)
{
for (int i = 0; i < model.ContentName.Length; i++)
{
if (model.ContentName[i] == "")
{
ModelState.AddModelError("", "Fill string!");
return View(model);
}
}
return View(model);
}
这篇关于验证ASP.NET MVC中的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文