开发者

Accessing html elements in .cshtml files

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开发者_JAVA百科 .cshtml file?

For example:

@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?


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 .change() on the checkbox in question. Check it in that block.


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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜