Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set value of @Html.EditorFor using jquery in in asp.net mvc

I want to set the value of my input text using jquery. I have tried by doing this :

    <div class="editor-label">
        @Html.LabelFor(model => model.ID )
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ID)
        @Html.ValidationMessageFor(model => model.ID)
    </div>

and jquery syntax :

    $("#ID").val(data.ID);

But it is not working. I have tried the same jquery code with this :

<input type="text" id="ID" />

This approach is working , but I don't know why @Html.EditorFor is not working with jquery .Thanks in advance

like image 793
Hammad Shahid Avatar asked Jun 27 '13 05:06

Hammad Shahid


2 Answers

You're better off using the TextBoxFor method as it allows you to change the id and class of the element directly whereas EditorFor doesn't unless you create a custom edit state for it (using EditorTemplate).

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

I believe @Html.EditorFor(model => model.ID) will set the name attribute as that is what mvc depends upon to do its model binding so if the above is not to your liking you can perhaps use the name as a selector.

like image 196
JonVD Avatar answered Sep 20 '22 18:09

JonVD


You can add the @id, @class, etc on EditorFor as follows:

@Html.EditorFor(model => model.ID, new {htmlAttributes = new {@id="someid"}})
like image 28
d2z2 Avatar answered Sep 19 '22 18:09

d2z2