Accediendo a elementos html en archivos .cshtml

in webforms you can attach an onclick and make the checkbox postback, so you can enable/disable elements. Can you do this in MVC as well, in the .cshtml file?

Por ejemplo:

@Html.CheckBox("box")

@if (box.Checked) {
    RenderSection("section")
}

or, if that isn't possible, is there a way to run C# code (like Render calls) from javascript?

preguntado el 16 de mayo de 11 a las 17:05

2 Respuestas

No, in webforms the checkbox is actually a class that outputs a checkbox on render and thus gives you access to variables such as Checked.

If you are trying to determine whether or not to call RenderSection("section") if the checkbox has a true value on rendering, then you will want to check the condition via code - whatever that condition may be.

Alternatively if this is a dynamic thing where checking a checkbox shows the section and clicking again hides it, then you will want to use JavaScript. Doing this in jQuery would be pretty trivial.

In jQuery, you would likely want to use .cambio() on the checkbox in question. Check it in that block.

contestado el 16 de mayo de 11 a las 22:05

Thanks a ton, that's sorta what I thought was needed, and I've got the jQuery handy for that bit. - mvcnewguy

In JavaScript, you can reference MVC3 controls w/ a dollar-sign and hashtag:

$("#myDropDown").show();
$("#otherDropDown").hide();

contestado el 16 de mayo de 11 a las 21:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.