Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make hiddenfor have value true

Tags:

asp.net-mvc

My form has to set a Boolean to true but, the user wont' be able to interact with the control to change this.

I think the best approach is to use the HiddenFor as it's not desirable to set this in the Controller for various reasons but I can't set the Boolean to true...

My code

            @using (Html.BeginForm())
            {
                @Html.LabelFor(mod => mod.EmailAddress)<br />
                @Html.TextBoxFor(mod => mod.EmailAddress)

                @Html.HiddenFor(mod => mod.IsSubsribed, new { value = true })
            }

I've tried

      @Html.HiddenFor(mod => mod.IsSubsribed, new { value = true })
      @Html.HiddenFor(mod => mod.IsSubsribed, new { value = "true" })
      @Html.HiddenFor(mod => mod.IsSubsribed, new { value = "checked" })

What do I need to do

like image 951
MyDaftQuestions Avatar asked Feb 05 '23 19:02

MyDaftQuestions


1 Answers

helper methods will ultimately render the input elements. So why not write an hidden input element tag ?

<input type="hidden" name="IsSubsribed" value="true" />

Or if you want to use the helper method, you can override the value explcititly (Usually the helper method use the value of the expression (your property))

@Html.HiddenFor(d=>d.IsSubsribed,new { Value="true"})

V in Value should be caps for this to work

But remember, user can still update this value and send it . So do not rely on values coming from client. If you know this should be always true, use true in your http post action method(server code) instead of relying on this value coming from client browser

In short, Do not blindly trust the data coming from client browser. It can be easily altered

like image 180
Shyju Avatar answered Feb 27 '23 19:02

Shyju