我的MVC3剃刀视图中的if语句出现问题

时间:2012-04-08 14:43:16

标签: c# asp.net-mvc asp.net-mvc-3 razor

我需要修改剃刀视图中元素的显示,具体取决于模型中变量的值。

我所拥有的是Model.PartitionKey,它是一个六个字符的字符串。我需要编写一个if语句来检查该字符串的第3个和第4个字符是否为“00”。

这是我到目前为止所拥有的:

@using (Ajax.BeginForm(
    action,
    "Contents",
    null,
    new AjaxOptions
    {
        UpdateTargetId = "update-message",
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "POST",
        OnSuccess = success,
        OnFailure = "ajaxOnFailure"
    }, new { @id = "dialogForm", @class = "content ui-widget dialog-admin" }))
{

<div class="float-left">
    @Html.LabelFor(model => model.Status)
    @if (action == "Edit" || action == "Create") {
        @Html.DropDownList("Status", null, new { @class="drop-down", id = "dialogStatus", style="width: 120px" })
    }
    else
    {
        @Html.TextBox("Status", (string)ViewBag.Status, new { @readonly = true })      
    }
</div> 

@if ( Model.PartitionKey.Substring(2,2) == "00") {  
<div class="float-left">
    Html.LabelFor(model => model.Link)
    if (action == "Edit" || action == "Create") {
        Html.TextBoxFor(model => model.Link, new { size = 25 })
    } else {
        Html.TextBoxFor(model => model.Link, new { size = 25, @readonly = true })
    }
</div> 
}

问题在于,第3个字符“f”上的@if开头有一条错误信息,说“如果@字符后面的关键字未被预料到”。

请注意,我在第一个代码块中的if之前有@个字符。然而,它只是在它抱怨的第二个代码块中。

4 个答案:

答案 0 :(得分:4)

尝试

@using (Ajax.BeginForm(
    action,
    "Contents",
    null,
    new AjaxOptions
    {
        UpdateTargetId = "update-message",
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "POST",
        OnSuccess = success,
        OnFailure = "ajaxOnFailure"
    }, new { @id = "dialogForm", @class = "content ui-widget dialog-admin" }))
{

<div class="float-left">
    @Html.LabelFor(model => model.Status)
    @if (action == "Edit" || action == "Create") {
        @Html.DropDownList("Status", null, new { @class="drop-down", id = "dialogStatus", style="width: 120px" })
    }
    else
    {
        @Html.TextBox("Status", (string)ViewBag.Status, new { @readonly = true })      
    }
</div> 

if ( Model.PartitionKey.Substring(2,2) == "00") {  
<div class="float-left">
    @Html.LabelFor(model => model.Link)
    @if (action == "Edit" || action == "Create") {
        @Html.TextBoxFor(model => model.Link, new { size = 25 })
    } else {
        @Html.TextBoxFor(model => model.Link, new { size = 25, @readonly = true })
    }
</div> 
}
}

答案 1 :(得分:3)

我认为在这种情况下,你不能拥有一个if if。 尝试使用

打开代码块
@{ if (boolean)
    {
         // some code
         if (boolean)
         {
             //some code
         }
    } 
}

尝试它是否有效。

答案 2 :(得分:2)

使用类似的东西

@{
  if (Boolean)
  {
    // execute code here
  }
  else
  {
    // execute else code here
  }
}

答案 3 :(得分:0)

根据以下链接:

  1. http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx
  2. Unexpected "foreach" keyword after "@" character
  3. http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx
  4. 我知道它已在上面的评论中提到过,但它可能会有所帮助。因此,基于附件,以下代码是否有效:

    @using (Ajax.BeginForm(
    action,
    "Contents",
    null,
    new AjaxOptions
    {
        UpdateTargetId = "update-message",
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "POST",
        OnSuccess = success,
        OnFailure = "ajaxOnFailure"
    }, new { @id = "dialogForm", @class = "content ui-widget dialog-admin" }))
    {
    
    <div class="float-left">
        @Html.LabelFor(model => model.Status)
        @if (action == "Edit" || action == "Create") {
            @Html.DropDownList("Status", null, new { @class="drop-down", id = "dialogStatus", style="width: 120px" })
        }
        else
        {
            @Html.TextBox("Status", (string)ViewBag.Status, new { @readonly = true })      
        }
    </div> 
    
    @if ( Model.PartitionKey.Substring(2,2) == "00") {  
    <div class="float-left">
        Html.LabelFor(model => model.Link)
        if (action == "Edit" || action == "Create") {
            @Html.TextBoxFor(model => model.Link, new { size = 25 })
        } else {
            @Html.TextBoxFor(model => model.Link, new { size = 25, @readonly = true })
        }
    </div> 
    }