改变html.editorfor帮手id属性与剃刀MVC4 [英] Change id attribute of an html.editorfor helper in MVC4 with Razor

查看:144
本文介绍了改变html.editorfor帮手id属性与剃刀MVC4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了与此相关的一些不同的答案,看上去但都被用于MVC3或不剃刀。

I have looked through some various answers related to this but all were for mvc3 or not for Razor.

我有一个具有多种形式,在局部视图,是用于编辑不同的机型单页。然而,大多数这些模型中有姓名字段。我期待能够与特定的id作为这种指定editorfor:

I have a single page that has multiple forms, in partial views, that are for editing a different models. However, most of those models have a 'name' field. I am looking to be able to specify an editorfor with a specific id as such:

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name,  new {id = "PersonName"})
        @Html.ValidationMessageFor(model => model.Name)
    </div>

我曾尝试过各种其他的东西,但还没有找到一种令人满意的方式来处理这个问题。有两种选择,我似乎是:

I have tried various other things but have not found a satisfactory way to handle this. The two choices I seem to have are:

1)创建一个表单使用普通的HTML辅助手动和构建模型控制器

1) Create a form manually using the normal html helpers and construct the model in the controller

2)重命名模型中的所有领域,以适应格式

2) Rename all the fields in the model to fit the format

这些都不是让我兴奋,所以我希望有一个选择,但恐怕id是绑定的形式模型时,有什么用。

Neither of these are exciting to me so I am hoping for an alternative but am afraid that the id is what is used when binding the form to a model.

推荐答案

EditorFor 更改您的帮手 TextBoxFor 该EditorFor,没有超载覆盖HTML属性

Change your helper from EditorFor to TextBoxFor the EditorFor, doesn't have a overload to override html properties

更改此

@Html.EditorFor(model => model.Name,  new {id = "PersonName"})

为了这个

@Html.TextBoxFor(model => model.Name,  new {id = "PersonName"})

这篇关于改变html.editorfor帮手id属性与剃刀MVC4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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