HTML.EditorFor 添加 class 无效

HTML.EditorFor adding class not working

我在将 css class 添加到 HTML.EditorFor() 时遇到问题。如果有人有同样的问题,我正在网上搜索,但我找不到任何东西。

你能帮帮我吗?因此,class 未定义并不是 css 的问题,它只是没有添加到输入字段中。 这是视图中的代码:

@model eVinogradarstvo.Models.DetaljiViewModel

@{
    ViewBag.Title = "Edit";
}

@using (Html.BeginForm("PromijeniPodatke", "KorisnikDetalji", FormMethod.Post, new { enctype = "multipart/form-data",@class="form-horizontal" }))
{
@Html.AntiForgeryToken()

<div class="col-sm-12">
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.kd.id)
    @Html.HiddenFor(model => model.kd.korisnik_id)
    @Html.HiddenFor(model => model.kd.slika)
    @Html.HiddenFor(model => model.kd.dokument_id)
    <table class="table table-responsive">
        <tr>
            <td>Slika profila</td>
            <td><input type="file" name="uploadImages" class="input-files" /></td>
        </tr>
        <tr>
            <td>@Html.DisplayNameFor(model => model.kd.ime)</td>
            <td>
                @Html.EditorFor(model => model.kd.ime, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.kd.ime)
            </td>
        </tr>
        <tr>
            <td>@Html.DisplayNameFor(model => model.kd.prezime)</td>
            <td>
                @Html.EditorFor(model => model.kd.prezime, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.kd.prezime)
            </td>
        </tr>
        <tr>
            <td>@Html.DisplayNameFor(model => model.kd.adresa)</td>
            <td>
                @Html.EditorFor(model => model.kd.adresa, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.kd.adresa)
            </td>
        </tr>
        <tr>
            <td>@Html.DisplayNameFor(model => model.kd.grad)</td>
            <td>
                @Html.EditorFor(model => model.kd.grad)
                @Html.ValidationMessageFor(model => model.kd.grad)
            </td>
        </tr>
        <tr>
            <td>@Html.DisplayNameFor(model => model.kd.drzava)</td>
            <td>
                @Html.EditorFor(model => model.kd.drzava)
                @Html.ValidationMessageFor(model => model.kd.drzava)
            </td>
        </tr>
        <tr>
            <td>@Html.DisplayNameFor(model => model.kd.datum_rodenja)</td>
            <td>
                @Html.EditorFor(model => model.kd.datum_rodenja)
                @Html.ValidationMessageFor(model => model.kd.datum_rodenja)
            </td>
        </tr>
        <tr>
            <td>Mjerna jedinica težine</td>
            <td>
                @Html.DropDownListFor(model => model.kd.mj_tezine, new SelectList(Model.DDLtezina, "vrijednost", "opis"))
                @Html.ValidationMessageFor(model => model.kd.mj_tezine)
            </td>
        </tr>
        <tr>
            <td>Mjerna jedinica tekućine</td>
            <td>
                @Html.DropDownListFor(model => model.kd.mj_tekucine, new SelectList(Model.DDLtekucina, "vrijednost", "opis"))
                @Html.ValidationMessageFor(model => model.kd.mj_tekucine)
            </td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Spremi" class="btn btn-default" /></td>
        </tr>
    </table>
</div>

}

有什么建议吗?

EditorFor in ASP.NET MVC 3 不支持以这种方式将 CSS 传递给生成的 HTML。您必须创建一个自定义编辑器模板并传递自定义 ViewData 条目并手动执行(如上面的链接答案所示)。

这已在 MVC 5.1 中得到纠正,但如果您不愿意创建自己的模板,则无法在 MVC 3 中使用 EditorFor 进行此操作。

使用 TextBox 而不是 EditFor 这里不支持特殊的 html 属性,只有 MVC 5 才支持。 TextBox 应该支持 MVC 4,我不知道其他版本。

@Html.TextBox("test", "", new { @id = "signupform", @class = "form-control", @role = "form",@placeholder="Name" })

最简单的方法是使用 jquery 并使用控件 ID 名称添加

$(document).ready(function () {
    $('#LastName').addClass('form-control');
});

Set the class attribute to Html.EditorFor