在剃刀编辑器模板中获取模型变量名称



我想根据模型变量名称使用生成ID。

我有两个模型

public class ConceptViewModel
{
    [AllowHtml]
    public string Content { get; set; }
    [AllowHtml]
    public string EnglishContent { get; set; }
    public string Instructions { get; set; }
}

public class InputDataCollectionViewModel
{
    public int Id { get; set; }
    public ConceptModel Definition { get; set; }
    public ConceptModel Publication { get; set; }
    public bool Exist { get; set; }
}

我正在使用EditorTemplatesfor ConceptViewModel:

@model Models.ConceptViewModel
<tr>
    <td>@Html.LabelFor(m => m, htmlAttributes: new { @class = "control-label" })</td>                
    <td hidden>
        @Html.TextBoxFor(m => Content, (@Model.Exist ? null : (object)new { @disabled = "disabled" }))
        @Html.TextBoxFor(m => m.EnglishContent)
    </td>
    <td hidden>@Html.TextBoxFor(m => m.Instructions)</td>   
    <td><button type="button" class="btn btn-default" onclick="openEditor('UNIQUE_NAME_DEPENDING_ON_MODEL_NAME');">Edit</button></td>
</tr>

和观点:

@model Models.InputDataCollectionViewModel    
<div class="container">
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        {
            <div class="form-group">
                @Html.HiddenFor(model => model.Id)
                <table class="table table-hover table-condensed">
                    <tbody>
                        @Html.EditorFor(model => model.Definition)
                        @Html.EditorFor(model => model.Publication)                            
                    </tbody>
                    <tfoot></tfoot>
                </table>
                <div class="row">
                    <div class="col-md-12">
                        <button class="btn btn-default" type="submit">Save</button>
                    </div>
                </div>    
            </div>
        }
    }

生成包含以下内容的 html:

<label class="control-label" for="Definition">
<input id="Definition_Content" name="Definition.Content" type="text" value="" />
<input id="Definition_EnglishContent" name="Definition.EnglishContent" type="text" value="" />
<input id="Definition_Instructions" name="Definition.Instructions" type="text" value="..." />

其他ConceptViewModels也是如此。因此,由于我在其他地方的其他输入具有唯一的 ID(Definition_、Publication_ 等,我可以轻松地引用它(。

现在我的问题是,我将如何获得包含"定义","发布"的字符串,以便我可以将其与生成的按钮一起使用。我想要的是:

openEditor('Definition');
openEditor('Publication');

在点击中。

我已经设法找到了解决方案。Html 帮助程序 @Html.IdFor(m => m(

最新更新