2013-02-27 97 views
0

我需要使用IF-ELSE条件来检查该人是否被命名为Perry。如果是这样,我需要执行IF声明,否则如果人名不是Perry,那么我将执行else块。混合C#和HTML帮助器标记

即使他们说我们可以混合使用C#HTML helper标签,但我无法使用IF-ELSE条件。 IF-ELSE显示为文本。我如何纠正我的代码?

if(person == "Perry") 
{ 
    <div class="content"> 
    @using (Html.BeginForm("Go", "MyPro", new { }, FormMethod.Post, new { })) 
    { 
     @Html.TextBoxFor(mod => mod.id, new { id = "nameid" }) 
     <input type="text" id="name" name="name" /> 
     <input type="submit" value="send" class="send"/>  
     </div> 
    } 
}else { 
    <div class="content1"> 
     @using (Html.BeginForm("Come", "MyPro", new { }, FormMethod.Post, new { })) 
     { 
      @Html.TextBoxFor(mod => mod.id, new { id = "schoolid" }) 
      <input type="submit" value="Send School" class="submitschool"/> 
     } 
    </div> 
} 

回答

4

只要把@之前的C#代码的HTML标签,这是所有...

@{var person = "text";} 

@if(person == "Perry") 
{ 
    <div class="content"> 
     @using (Html.BeginForm("Go", "MyPro", new { }, FormMethod.Post, new { })) 
     { 
      @Html.TextBoxFor(mod => mod.id, new { id = "nameid" }) 
      <input type="text" id="name" name="name" /> 
      <input type="submit" value="send" class="send"/>  
     } 
    </div> 
} 
else 
{ 
    <div class="content1"> 
     @using (Html.BeginForm("Come", "MyPro", new { }, FormMethod.Post, new { })) 
     { 
      @Html.TextBoxFor(mod => mod.id, new { id = "schoolid" }) 
      <input type="submit" value="Send School" class="submitschool"/> 
     } 
    </div> 
} 

RAZOR SYNTAX EXAMPLES

+0

如果我想在IF语句之外创建一个变量,该怎么办?它是@var n =“”;因为这不起作用。 – 2013-02-27 08:07:20

+0

'@ {var person =“text”;}'。我更新了答案。 – 2013-02-27 08:09:06

+0

if-else表现单块,所以你不需要使用括号。 – 2013-02-27 08:11:25

0

需要在if前面前面加上一个@,否则Razor视图引擎不知道你想切换到“代码模式”。

+0

我应该如何创建if语句之外的变量。 @var something =“”不起作用 – 2013-02-27 08:08:05