0

I have one problem in my Asp.net Mvc 3 project. I am using data first approach and strong type view. I hide one value on view from model as,

@Html.TextBoxFor(model => model.myfield1, new { style=" display:none" })

when I access this field value on java script I get null value or blank.

var abc= s("#myfield1").val();
alert(abc)

I will try like

@Html.DisplayFor(model => model.myfield1, new{})

value will display on view. I also try using hidden field but not work

have any issue or solution for this. I not display this field on view but this value required for further purpose.

1
  • I assume its $("#myfield1").val(); (not s). And if its returning null, then its because your myfield1 property is null - check the html your generating - it will be showing value="". But if DisplayFor() is showing a value, then its a ModelState issue and you need to shown the code in your controller methods associated with this view Commented Jul 26, 2016 at 10:59

2 Answers 2

1

For fields with display:none, it seems that val() does not work.

I bypass this behavior with attr():

$('input').attr('value',myNewValue);
Sign up to request clarification or add additional context in comments.

1 Comment

Of course .val() works (try it!). And if .val() returns null, then so will .attr('value') - but you code is setting the value, not getting it
0

because strongly typed html helper can't accessed when hidden You can use this helper below instead of what you are using now @Html.HiddenFor(m => m.myfield1) this helper are used for store hidden value in strongly typed view

1 Comment

@ Naveen Ganeshe I try as you suggest but it does not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.