如何获得自定义编辑模板模型的字段名 [英] How to get model's field name in custom editor template

查看:209
本文介绍了如何获得自定义编辑模板模型的字段名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建设我的第一个自定义编辑模板的文本区域控制。我的code到目前为止 -

 <%@控制语言=C#继承=System.Web.Mvc.ViewUserControl<动态>中%GT;
<%= Html.TextAreaFor(型号=>产品型号,2,30,
     新{@class =HTML,@placeholder = ViewData.ModelMetadata.Watermark})%GT;

这不是太大,到目前为止,但它确实工作。但是,我们需要增加一个字符计数器字段,以显示剩余用户可以键入的字符数。我知道该怎么做的所有的JavaScript,使这项工作。

所以要保持命名系统一样,我要添加一个名为.charCounter显示左剩余的字符数控制。我的问题是,我无法找出正确的语法能够为模型检索字段名。

的最终版本将看起来像(JavaScript的省略) -

 <%@控制语言=C#继承=System.Web.Mvc.ViewUserControl<动态>中%GT;
<%= Html.TextAreaFor(型号=>产品型号,2,30,
     新{@class =HTML,@placeholder = ViewData.ModelMetadata.Watermark})%GT;
<跨度类=XXX>其余字符 -
     <输入只读类型=文本名称=<&fieldName的GT; .charCounter/>
< / SPAN>


解决方案

您可以使用 ViewData.TemplateInfo.HtmlField preFIX ,就像这样:

 <输入
    只读=只读
    TYPE =文本
    NAME =<%= ViewData.TemplateInfo.HtmlField preFIX%GT; .charCounter
/>

I'm building my first custom editor template for a text area control. My code so far is -

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%= Html.TextAreaFor( Model => Model , 2, 30, 
     new { @class = "html", @placeholder = ViewData.ModelMetadata.Watermark }) %>

It's not much so far, but it does work. But we need to add a character counter field to show remaining number of characters that the user can type in. I know how to do all the JavaScript to make this work.

So to keep naming system same, I'm going to add a control named ".charCounter" to display number of remaining characters left. My problem is that I cannot figure out the correct syntax to be able to retrieve the field name for the model.

The final version will look something like (JavaScript omitted) -

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%= Html.TextAreaFor( Model => Model , 2, 30, 
     new { @class = "html", @placeholder = ViewData.ModelMetadata.Watermark }) %>
<span class="xxx">Remaining characters - 
     <input readonly type="text" name="<fieldName>.charCounter" />
</span>

解决方案

You could use ViewData.TemplateInfo.HtmlFieldPrefix, like this:

<input 
    readonly="readonly" 
    type="text" 
    name="<%= ViewData.TemplateInfo.HtmlFieldPrefix %>.charCounter" 
/>

这篇关于如何获得自定义编辑模板模型的字段名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆