How is @ used in MVC Razor and when is it required?

masimam

New Member
I'm fairly new at MVC 3 but I came across a rather curious problem.I'm using the Razor syntax, and according to VS, I don't need to prefix \[code\]@\[/code\] to statements if they are immediately preceded with another \[code\]@\[/code\] statement, or once inside the code, the prefix \[code\]@\[/code\] is no longer required.So here is my code in my View:\[code\] @using (Html.BeginForm("StudentSelect", "Home", FormMethod.Post, new { id = "sSelect" })) { Html.HiddenFor(m => m.SelectedStudent); foreach (Classes.CStudent item in Model.Students) { <div class="studentSelect"> <div class="studentname">@item.StudentName</div> <div>@item.Address</div> </div> } }\[/code\]Take not that the \[code\]Html.HiddenFor\[/code\] and \[code\]foreach\[/code\] lines do not have \[code\]@\[/code\] prefix.The generated HTML should produce a \[code\]<form>\[/code\] followed by a \[code\]<input type="hidden">\[/code\] field.However, upon checking the HTML on the generated page, the hidden input field is missing.\[code\]<form action="/Home/StudentSelect" id="sSelect" method="post"> <div class="studentSelect"> <div class="studentname">Name1</div> <div>AAA</div> </div> <div class="studentSelect"> <div class="studentname">Name2</div> <div>Address1</div> </div> </form>\[/code\]Am I doing something wrong? Why isn't the hidden input not rendered?Any clues would help. ThanksBy the way, this code compiles correct. However, if I prefix \[code\]@\[/code\] in front of \[code\]Html.HiddenFor\[/code\] my code does not compile and Visual Studio produces an error.
 
Back
Top