Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML.EditorFor adding class not working

I have problem with adding css class to HTML.EditorFor(). I was searching on the net if someone had the same problem but I couldn't find anything.

Can you please help me? So, its not problem about css that class is not defined, it is, but simply, its not added to input field. This is the code in view:

@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>

}

Any suggestions?

like image 843
Veki Avatar asked Jan 13 '15 15:01

Veki


1 Answers

EditorFor in ASP.NET MVC 3 does not support passing CSS to the generated HTML in this manner. You have to create a custom editor template and pass custom ViewData entries and do it manually (as the linked answers above show).

This was corrected in MVC 5.1, but there is no way to make this work in MVC 3 with EditorFor if you aren't willing to create your own templates.

like image 88
Erik Funkenbusch Avatar answered Sep 22 '22 12:09

Erik Funkenbusch