开发者

In MVC, how can I make this example of a checkbox checked by default?

开发者 https://www.devze.com 2023-02-09 21:24 出处:网络
I am trying to make a checkbox in MVC be checked by default. Below is my code.For all examples I have seen of this, the person uses something different than what I am trying so I have not been able t

I am trying to make a checkbox in MVC be checked by default.

Below is my code. For all examples I have seen of this, the person uses something different than what I am trying so I have not been able to find help on this yet.

<%=Html.CheckBoxFor(Function(model) model.Delete)%>

I have tried adding the , new { @checked = "checked" } approach and that didnt work. I got a squiggly line under the first "{".... the entire line looked like this...

<%=Html.CheckBoxFor(Function(model) model.Delete, new { @checked = "checked" }))%>

Any advice on how I can get this checkbox to be auto checked?

thanks for any help group.

Will

This is what I am using in my controller. This is the start of the COntroler... this is not work. FirstNameAsBusiness is the name of the checkbox

In the view...

<%=Html.CheckBoxFor(Function(model) model.FirstNameAsBusiness)%> <%=Html.LabelFor(Function(model) model.FirstNameAsBusiness)%>

In the controller....

Function Edit(ByVal a As AuditModel) As ActionResult开发者_如何学JAVA
    a.FirstNameAsBusiness = True 
    Return View(a)
End Function


Did you try

<%=Html.CheckBoxFor(Function(model) model.Delete, New With { .checked = "checked" })%>

The only other thing I can think of is to do as SLaks has suggested.

Function Edit(ByVal a As AuditModel) As ActionResult
    a.FirstNameAsBusiness = True 
    a.Delete = True ''# This should do it.
    Return View(a)
End Function

View

<%=Html.CheckBoxFor(Function(model) model.Delete)%>


You need to set model.Delete to true.

EDIT: You need to set it before calling Checkbox.


You could always just set the 'Delete' property to true on the constructor of your model (therefore the default value).

Very similar to what has been suggested above but this way no matter where you use the 'Delete' property it will always be ticked by default.

It all depends on if this is classed as business logic or more a View specific decision.

0

精彩评论

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