3

I am building a screen from an array of ViewModel objects that contain three fields:

    public class StartViewModel {

        public string Id{ get; set; }

        public string Name { get; set; }

        public bool Accept { get; set; }
    }

I am then trying to bind it to my view like so:

<input type="hidden" name="StartRequests[<%: i.ToString()  %>].Name" value="<%: StartRequests.Name %>" />
<input type="hidden" name="StartRequests[<%: i.ToString()  %>].Id" value="<%: StartRequests.Id%>" />

I then want to bind the Accept flag to a checkbox. I understand that the checkbox lacks a name attribute so I can't use it immediately and I understand that the Html.CheckBoxFor will generate a second hidden field that will store the value. So here is my issue.

I do not have the ability to use javascript. I need to configure this checkbox to effectively adjust the value in the ViewModel but I do not know how to make this happen. How do the Html.CheckBox helper populate the hidden field that it generates? I assumed it did so with javascript. Can someone help me understand a way to do this? I am still working through the ideas behind Model binding.

2
  • The Html Helpers do not use Javascript, except maybe for client-side validation (which is injected into the page as Javascript). Html Helpers are pure server-side code, using a string helper object to stitch together HTML elements and attributes. If you can't use Javascript, and what you are trying to do needs client-side behaviour (i.e. in the browser before your Submit), you are out of luck. Commented Jun 14, 2011 at 20:45
  • Given this, why is Html.CheckBoxFor out? Commented Jun 14, 2011 at 20:48

1 Answer 1

1

For following model:

public class StartViewModel {

    public string Id{ get; set; }

    public string Name { get; set; }

    public bool Accept { get; set; }
}

//-> Accept = false.

The solution: Change public bool Accept { get; set; } to public string Accept { get; set; }

When submit, if checkbox is checked, The "Accept" value = "on". You can detect checked value by the way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.