2

I need to modify the display of elements in my razor view depending on the value of a variable that's in my model.

What I have is Model.PartitionKey which is a six character string. I need to code an if statement that will check if the 3rd and 4th characters of this string are "00".

Here's what I have so far:

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

The problem is that it there is an error message on line starting with @if on the 3rd character "f" saying "unexepected if keyword after the @ character".

Note that I have @ characters before the if in the first block of code. However it is just in the second block of code that it complains.

Eranga
  • 32,181
  • 5
  • 97
  • 96
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

4 Answers4

4

Try

@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> 
}
}
Eranga
  • 32,181
  • 5
  • 97
  • 96
  • Tried it but this gives same problem. Your code above works great if I remove the first and last lines :-( OR if I enclose your code in a DIV. Very strange indeed. I am thinking it is like this because of the Ajax form that the code is inside of. – Samantha J T Star Apr 08 '12 at 15:08
  • 1
    @SamanthaJ the `@` symbol is only required when your code is contained within an HTML element. The first `if` does not need the `@` because it is a direct decendent of your `@using` block. That is why it works when you wrap it in a `div`. – danludwig Apr 08 '12 at 16:19
3

I think you can't have an if inside an if in that sort. Try opening a code block with

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

Try if it works.

oamsel
  • 475
  • 3
  • 10
2

use something like this

@{
  if (Boolean)
  {
    // execute code here
  }
  else
  {
    // execute else code here
  }
}
Community
  • 1
  • 1
MHF
  • 511
  • 8
  • 22
0

As per the following links:

  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

I know its been mentioned in comments above, but it maybe helpful. So based on that attached, does the following code work:

@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> 
}
Community
  • 1
  • 1
garfbradaz
  • 3,424
  • 7
  • 43
  • 70