2

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

我所拥有的是 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 关键字意外”。

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

4

4 回答 4

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> 
}
}
于 2012-04-08T14:55:08.087 回答
3

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

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

试试看是否有效。

于 2012-04-08T15:21:34.350 回答
2

使用这样的东西

@{
  if (Boolean)
  {
    // execute code here
  }
  else
  {
    // execute else code here
  }
}
于 2012-04-08T16:10:57.310 回答
0

根据以下链接:

  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> 
}
于 2012-04-08T18:02:46.987 回答