HtmlHelper在Controller中生成验证标记

时间:2016-01-04 22:04:40

标签: asp.net-mvc asp.net-core-mvc unobtrusive-validation razorgenerator html-generation

这与The Question

有某种关联

我正在使用jquery不显眼的验证。我有以下型号

public  class MyModel
{

   [Required]
   public int ID { get; set; }


   [MinLength(2)]
   public string Description { get; set; }

}

我想用验证生成一个html标记(这是我想用代码生成我自己的东西

<input class="FormElement form-control valid" type="text" data-val="true" data-val-required="The ID field is required." id="ID" name="ID">

<input class="FormElement form-control valid" type="text" data-val="true" data-val-minlength="The field Description must be a string or array type with a minimum length of '2'." data-val-minlength-min="2" id="Description" name="Description" value="description changed 6">

我的问题是如何从控制器内的模型属性手动生成标记。

所以我的代码看起来就像这样

    private readonly IHtmlGenerator htmlGenerator;
    private readonly IHtmlHelper helper;
    public MyController(IHtmlGenerator htmlGenerator, IHtmlHelper hh)
    {
        if (htmlGenerator == null)
            throw new ArgumentNullException("htmlGenerator");

        this.htmlGenerator = htmlGenerator;
        this.helper = hh;
    }
   public IActionResult GenerateHtml()
    {

     HtmlHelper tempHelper= this.HtmlHelper;
      // Generate the markup for ID and Description here from model MyModel(With VALIDATION) 
    }

0 个答案:

没有答案