HTML.EditorFor添加类不起作用

时间:2015-01-13 15:58:43

标签: html css asp.net-mvc-5

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

你能帮帮我吗?因此,关于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>

}

有什么建议吗?

3 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:1)

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

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

Set the class attribute to Html.EditorFor

答案 2 :(得分:0)

使用 TextBox 代替 EditFor 这里不支持特殊的html属性,它只支持MVC 5. TextBox应该支持MVC 4,我不支持了解其他版本。

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