开发者

Manually bind MVC model to checkbox without HTML helper

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

    public class Start开发者_运维知识库ViewModel {

        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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜