asp.net-mvccheckbox

Checkbox issue with asp.net mvc 3 - missing assembly reference


In my HTML (The view) I have

 <input type="checkbox" name="car-required" id="car-required" value="1" />CAR required

I get a run time error of CAR missing assembly or refence...What gives? It seems to treat my text on the html form as C# code....

Is there an ASP.net MVC specific way to write html for a simple checkbox?

Edited

It is contained within @using block

@using (Html.BeginForm("Action", "ControllerName", FormMethod.Post, new { id = "my-frm" })) {

Curiously, if I put exact same mark up outside the @using, it works fine. Clearly, MVC 3 bug, as HTML is not supposed to give c# error


Solution

  • The Razor parser tries to do as best as it can but in this case it simply falls short. So you can help him by using @: or wrapping the in <text> nodes (which is a special kind of node for the parser indicating text literal. The <text> node is obviously not rendered in the output):

    @using (Html.BeginForm("Action", "ControllerName", FormMethod.Post, new { id = "my-frm" })) 
    {
        <input type="checkbox" name="car-required" id="car-required" value="1" />@:CAR required
    }
    

    or:

    @using (Html.BeginForm("Action", "ControllerName", FormMethod.Post, new { id = "my-frm" })) 
    {
        <input type="checkbox" name="car-required" id="car-required" value="1" /><text>CAR required</text>
    }