[英]Dynamically rendered MVC 3 validation not working
I'm rendering some input controls dynamically. 我正在动态渲染一些输入控件。 I wrote a method like this 我写了这样的方法
public override MvcHtmlString GetActionControl(HtmlHelper<ItemDetailsViewModel> helper)
{
return
helper.EditorFor(A => A.Triage.ProcessNumber) +
helper.ValidationMessageFor(a => a.Triage.ProcessNumber, "*")
}
And I'm calling this from the main CSTHML file like this: 我从CSTHML主文件中这样调用它:
<!-- Render Action Controls for each possible action -->
@foreach (IActionControl iac in WorkflowActionControls.GetActionControls(Model.WorkflowItem.CurrentState))
{
<div id="@("div" + iac.ControlName)" class="ui-corner-all ui-widget ui-widget-content" style="margin-top: 24px; padding: 15px;">
@iac.GetActionControl(this.Html)
</div>
}
Notice that I'm passing the current HtmlHelper to the GetActionControl() method. 注意,我正在将当前的HtmlHelper传递给GetActionControl()方法。
Problem is, the generated output is only: 问题是,生成的输出仅为:
<input class="text-box single-line" id="Triage_ProcessNumber" name="Triage.ProcessNumber" type="text" value="" />
Ie, no "extended" validation fields, and no validation . 即,没有“扩展的”验证字段,也没有验证。 Aditionally I've discovered that the helper.ValidationMessageFor() method returns NULL. 另外,我发现helper.ValidationMessageFor()方法返回NULL。
However, If I put the editor and the validationMessage on the .cshtml directly everything works like a charm: 但是,如果我直接将编辑器和validateMessage放在.cshtml上,则所有内容都将像一个超级按钮一样工作:
@Html.EditorFor(a => a.Triagem.ProcessNumber)
@Html.ValidationMessageFor(a => a.Triagem.ProcessNumber, "*")
What am I doing wrong here? 我在这里做错了什么? Shouldn't the end result of calling HtmlHelper.EditorFor be the same? 调用HtmlHelper.EditorFor的最终结果不应该相同吗? I'm sorry if this is a lame question but there isn't much information on generating HTML markup dynamically in regards to MVC 3. 抱歉,这是一个me脚的问题,但是关于MVC 3动态生成HTML标记的信息不多。
I've checked the common pitfalls and everything is OK: validation enabled on web.config, the model class has the proper validation attributes, etc. 我检查了常见的陷阱,一切正常:在web.config上启用了验证,模型类具有适当的验证属性,等等。
Any help would be greatly appreciated :) 任何帮助将不胜感激 :)
PS: I've simplified the pasted code and ommited the form. PS:我简化了粘贴的代码,并省略了表格。
I don't know if this will help you, but validation is rendered when Html.ViewContext.FormContext
is not null. 我不知道这是否对您有帮助,但是当Html.ViewContext.FormContext
不为null时,将呈现验证。 When i had same problem, i just initialized form context with new FormContext();
当我遇到同样的问题时,我只是用new FormContext();
初始化了表单上下文new FormContext();
and it worked 它起作用了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.