我的 MVC3 剃须刀视图中的 if 语句存在问题



我需要根据模型中变量的值修改剃刀视图中元素的显示。

我拥有的是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 开头的行上有一个错误消息,说"@ 字符后面的 unexepected if 关键字"。

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

尝试

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

我认为你不能在那种 if 中有一个 if。尝试使用 打开代码块

@{ if (boolean)
    {
         // some code
         if (boolean)
         {
             //some code
         }
    } 
}

试试它是否有效。

使用类似的东西

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

根据以下链接:

  1. http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx
  2. "@"字符后出现意外的"foreach"关键字
  3. http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx

我知道它在上面的评论中提到过,但它可能会有所帮助。因此,基于附加的内容,以下代码是否有效:

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

最新更新