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

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

问题描述

我正在为文本区域控件构建我的第一个自定义编辑器模板.到目前为止我的代码是 -

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 }) %>

到目前为止还不算多,但确实有效.但是我们需要添加一个字符计数器字段来显示用户可以输入的剩余字符数.我知道如何使用所有的 JavaScript 来完成这项工作.

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.

所以为了保持命名系统相同,我将添加一个名为.charCounter"的控件来显示剩余的剩余字符数.我的问题是我无法找出正确的语法来检索模型的字段名称.

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.

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

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>

推荐答案

你可以使用 ViewData.TemplateInfo.HtmlFieldPrefix,像这样:

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

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

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

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