如何获得一个项目的列表中的一个关键的ModelState [英] How to get a ModelState key of an item in a list
问题描述
问题
我有用户可编辑的字段列表。当模型被提交我要检查,如果这项目是有效的。我不能用数据符号,因为每个领域都有不同的验证过程,我不会知道,直到运行时。如果验证失败我使用 ModelState.AddModelError(字符串键,字符串错误)
,其中最关键的是你要的错误信息添加到HTML元素的名称。既然有字段列表,剃刀生成的HTML项目名称是像字段[0] .DisplayName
。我的问题是有没有方法或方式来获得生成的HTML名称从视图模型的关键?
尝试性解决方案
我试过的toString()
为没有运气的主要手段。我还通过的HtmlHelper
类看了,但我没有看到任何有用的方法。
code段
视图模型
公共类CreateFieldsModel
{
公共TemplateCreateFieldsModel()
{
FreeFields =新的List< FieldModel>();
} [HiddenInput(DisplayValue = FALSE)]
公众诠释ID {搞定;组; } 公众的IList< TemplateFieldModel> FreeFields {搞定;组; }
公共类TemplateFieldModel
{
[显示(NAME =Dispay名称)]
公共字符串显示名称{搞定;组; } [需要]
[显示(名称=田)
公众诠释FieldTypeID {搞定;组; }
}
}
控制器
公众的ActionResult CreateFields(CreateFieldsModel模型)
{
如果(!ModelState.IsValid)
{
//我在哪里从视图模型的关键?
ModelState.AddModelError(model.FreeFields [0],测试错误);
返回查看(模型);
}
}
在源$ C $ C周围挖后,我已经找到了解决方案。有一类称为防爆pressionHelper
用于生成字段的HTML名称时, EditorFor()
被调用。在防爆pressionHelper
类有一个方法 GETEX pressionText()
返回一个字符串,它是该HTML元素的名称。下面是如何使用它...
的for(int i = 0; I< model.FreeFields.Count();我++)
{
//生成该项目的前pression
防爆pression<&Func键LT; CreateFieldsModel,串>>前pression = X => x.FreeFields [I] .value的;
//获取我们的HTML输入项目名称
字符串键=前pressionHelper.GetEx pressionText(如pression);
//添加一条错误消息,该项目
ModelState.AddModelError(键,错误!);
}如果(!ModelState.IsValid)
{
返回查看(模型);
}
Problem
I have a list of fields that the user can edit. When the model is submitted I want to check if this items are valid. I can't use data notations because each field has a different validation process that I will not know until runtime. If the validation fails I use the ModelState.AddModelError(string key, string error)
where the key is the name of the html element you want to add the error message to. Since there are a list of fields the name that Razor generates for the html item is like Fields[0].DisplayName
. My question is there a method or a way to get the key of the generated html name from the view model?
Attempted Solution
I tried the toString()
method for the key with no luck. I also looked through the HtmlHelper
class but I didn't see any helpful methods.
Code Snippet
View Model
public class CreateFieldsModel
{
public TemplateCreateFieldsModel()
{
FreeFields = new List<FieldModel>();
}
[HiddenInput(DisplayValue=false)]
public int ID { get; set; }
public IList<TemplateFieldModel> FreeFields { get; set; }
public class TemplateFieldModel
{
[Display(Name="Dispay Name")]
public string DisplayName { get; set; }
[Required]
[Display(Name="Field")]
public int FieldTypeID { get; set; }
}
}
Controller
public ActionResult CreateFields(CreateFieldsModel model)
{
if (!ModelState.IsValid)
{
//Where do I get the key from the view model?
ModelState.AddModelError(model.FreeFields[0], "Test Error");
return View(model);
}
}
After digging around in the source code I have found the solution. There is a class called ExpressionHelper
that is used to generate the html name for the field when EditorFor()
is called. The ExpressionHelper
class has a method called GetExpressionText()
that returns a string that is the name of that html element. Here is how to use it ...
for (int i = 0; i < model.FreeFields.Count(); i++)
{
//Generate the expression for the item
Expression<Func<CreateFieldsModel, string>> expression = x => x.FreeFields[i].Value;
//Get the name of our html input item
string key = ExpressionHelper.GetExpressionText(expression);
//Add an error message to that item
ModelState.AddModelError(key, "Error!");
}
if (!ModelState.IsValid)
{
return View(model);
}
这篇关于如何获得一个项目的列表中的一个关键的ModelState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!