Changing DisplayFor Text / Value via jquery

I need this displayfor to update its value using jquery.

I tried .val() and .text() , but none of them worked, is there a special way to do this for Displays, I need it to be and remain as a Display.

Also, can the style not be displayed for displays? I could never get this to work; it works for TextboxFor.

Thanks for any advice.

 Estimated Time: @Html.DisplayFor(model => model.EstimatedTime, new { @class = "TitleEstTimeClass", @style = "color: green" }) $(".TitleEstTimeClass").val(result[1]); 
+8
jquery asp.net-mvc razor
source share
1 answer

DisplayFor generates an HTML tag <label id="myId" class="myClass">Some Text</label> . (erroneous)

EDIT

I was wrong and thought you were using the LabelFor . The DisplayFor helper does not display HTML markup (such as a label or span tag). If you want this to work with the DisplayFor helper, you need to wrap the DisplayFor tag with <span> or something similar, as shown below:

 <span class="TitleEstTimeClass" style="color: green"> @Html.DisplayFor(model => model.EstimatedTime) </span> 

Given this, try using the .html() JQuery property, as shown below: this is really no different from updating text in a div or span:

 $(".TitleEstTimeClass").html(result[1]); 

Also, make sure that your jQuery call is in the appropriate <script></script> and that the function you are calling is actually running. I'm not sure if the above code is straight from your project or just fragments, but as written above, nothing will happen.

+18
source share

All Articles