Thursday, March 24, 2011

ASP.NET MVC 3's Razor tag nuances

Guidelines obtained from http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@for (int i = 0; i < 10; ++i)
{
    <input type="button" id="hello@(i)" value="button@(i)" />
}


<script>

$(function() {

    @for (int i = 0; i < 10; ++i)
    {
        <text>
        $("#hello@(i)").click(function() {
            alert("@i");
        });
        </text>
    }
});

</script>



Output:

<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
    <script src="/Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
</head>

<body>
    
<h2>Index</h2>

    <input type="button" id="hello0" value="button0" />
    <input type="button" id="hello1" value="button1" />
    <input type="button" id="hello2" value="button2" />
    <input type="button" id="hello3" value="button3" />
    <input type="button" id="hello4" value="button4" />
    <input type="button" id="hello5" value="button5" />
    <input type="button" id="hello6" value="button6" />
    <input type="button" id="hello7" value="button7" />
    <input type="button" id="hello8" value="button8" />
    <input type="button" id="hello9" value="button9" />


<script>

$(function() {

        
        $("#hello0").click(function() {
            alert("0");
        });
        
        
        $("#hello1").click(function() {
            alert("1");
        });
        
        
        $("#hello2").click(function() {
            alert("2");
        });
        
        
        $("#hello3").click(function() {
            alert("3");
        });
        
        
        $("#hello4").click(function() {
            alert("4");
        });
        
        
        $("#hello5").click(function() {
            alert("5");
        });
        
        
        $("#hello6").click(function() {
            alert("6");
        });
        
        
        $("#hello7").click(function() {
            alert("7");
        });
        
        
        $("#hello8").click(function() {
            alert("8");
        });
        
        
        $("#hello9").click(function() {
            alert("9");
        });
        
});

</script>
</body>
</html>

No comments:

Post a Comment