9

I'm having an issue where I am getting the message:

\OrderGas.cshtml(24): error CS1963: An expression tree may not contain a dynamic operation"}

Here is my code for my web form:

@using SuburbanCustPortal.MiscClasses

@{
    ViewBag.Title = "Order Gas";
}

<h2>Order Gas</h2>

        @using (Html.BeginForm("OrderGasSuccess", "GasOrder", FormMethod.Post))
        {
          @Html.ValidationSummary(true, "Submit was unsuccessful. Please correct the errors and try again.")

          <div>
            <fieldset>
              <legend>Account Information - all fields required</legend>

              <div class="highlightedtext">
                @ViewBag.Account
              </div>

              @if (SessionHelper.ShowPaymentOptions)
              {
                <div class="editor-label">
                  @Html.LabelFor(m => m.PaymentMethod)
                </div>
                <div class="editor-field">
                  @Html.DropDownListFor(x => x.PaymentMethod, SessionHelper.PaymentMethods)
                </div>
              }

              <div class="editor-label">
                @Html.LabelFor(m => m.TankPercent)
              </div>
              <div class="editor-field">
                @Html.TextBoxFor(m => m.TankPercent, new { @class = "GenericSmallTextBox" })
                <label class="SmallBluelabel">Please enter the current percentage in your tank.</label>
                @Html.ValidationMessageFor(m => m.TankPercent)
              </div>             

              <br/>

              <div class="editor-field">
                @Html.CheckBoxFor(x => x.IsFill)
                @Html.LabelFor(m => m.IsFill)
                <label class="SmallBluelabel">Would you like us to fill your tank?</label>
                @Html.ValidationMessageFor(m => m.IsFill)
              </div>

              <div class="editor-label">
                <b> - OR - </b>
              </div>

              @if (!string.IsNullOrWhiteSpace(ViewBag.AlternateGasMessage))
              {
                <fieldset class="pleasenote">
                  <legend>@ViewBag.AlternateGasMessageHeader</legend>
                  <label class="warningLabel">@ViewBag.AlternateGasMessage</label>                  
                </fieldset>
              }

              <div class="editor-label">
                @Html.LabelFor(m => m.Amount)
              </div>

              <div class="editor-field">
                @Html.TextBoxFor(m => m.Amount, new { @class = "GenericSmallTextBox" })
                <label class="SmallBluelabel">Or request a specific amount in the tank?</label>


                @if (ViewBag.MinimumGasMessage != null)
                {
                  <div>
                    <label class="SmallBluelabel">@ViewBag.MinimumGasMessage</label>
                  </div>
                }

                @Html.ValidationMessageFor(m => m.Amount)
              </div>             

              <div class="editor-label">
                @Html.LabelFor(m => m.ContactNumber)
              </div>
              <div class="editor-field">
                @Html.TextBoxFor(m => m.ContactNumber, new { @class = "GenericTextBox" })
                @Html.ValidationMessageFor(m => m.ContactNumber)                   
              </div>

              <div class="editor-label">
                @Html.LabelFor(m => m.Message)
              </div>
              <div class="editor-field">
                @Html.EditorFor(x => x.Message)            
              </div>

              <div>
                <input type="submit" value="Submit" class="typicalbutton"/>
              </div>

            </fieldset>

          </div>
        }

Here is the class calling the web form:

public ActionResult OrderGas()
{
  var control = Logging.StartLog();

  try
  {
    Logging.WriteLog("Starting OrderGas");

    var svc = new SubService();
    var orderGasModel = new OrderGasModel();

    orderGasModel.ContactNumber = svc.GetCustomerPhoneNumber(SessionHelper.TokenId, SessionHelper.CurrentAccountGuid);
    Logging.WriteLog(string.Format("orderGasModel.ContactNumber: {0}", orderGasModel.ContactNumber));

    if (SessionHelper.ShowPaymentOptions)
    {
      SessionHelper.PaymentMethods = GetPaymentMethods2();
    }
    if (SessionHelper.MinimumGasOrderAmount > 0)
    {
      var msg = string.Format("Minimum gas order is {0} gallons.", SessionHelper.MinimumGasOrderAmount);
      Logging.WriteLog(msg);
      ViewBag.MinimumGasMessage = msg;
    }

    var gasordermsg = svc.GetAlternateGasOrderMessage(SessionHelper.TokenId);
    ViewBag.AlternateGasMessageHeader = gasordermsg.Item1;
    ViewBag.AlternateGasMessage = gasordermsg.Item2;
    ViewBag.Account = string.Format("{0}-{1}", SessionHelper.CurrentBranchNumber.ToBranchString(),
      SessionHelper.CurrentAccountNumber.ToAccountString());

    return View(orderGasModel);
  }
  catch (Exception ex)
  {
    Logging.WriteException(ex);
    Logging.WriteLog(ex.Message);
    return View("Error");
  }
  finally
  {
    Logging.WriteLog(control, "End OrderGas");
  }
}

I've compared this to my history to see what I changed and I do not see why it isn't working anymore. When I put a debug on my PaymentMethods, it is giving an exception before it gets there.

I've tried commenting out parts of the cshmtl and I cannot get a clearcut answer as to what part of it is causing the error.

I'm at a loss... anyone see what I am doing wrong?

ErocM
  • 4,505
  • 24
  • 94
  • 161
  • If I count correctly, it complains about the `PaymentMethod` member of the model class. Is there something specific about it? I don't see you setting it in the action code. – Wiktor Zychla Feb 12 '15 at 22:28
  • @WiktorZychla Can you tell me how you came up with that? I don't follow what you mean by count? – ErocM Feb 12 '15 at 22:37
  • 2
    I already answered, but to explain a bit more about the comment from @WiktorZychla, the error indicates `\OrderGas.cshtml(24):`, which corresponds to the 24th line from the top of that particular file. That happened to be a fairly normal looking lambda, but made it obvious to look for a reason that the lambda wouldn't be processed. – Claies Feb 12 '15 at 22:52
  • 1
    @Claies ah I did not know the 24 meant the 24th line! That's good to know!! That will help out greatly in the future finding my issues. I guess it only makes sense now that I know that... Tyvm! – ErocM Feb 12 '15 at 23:47
  • Possible duplicate of [Razor View Engine : An expression tree may not contain a dynamic operation](https://stackoverflow.com/questions/4155392/razor-view-engine-an-expression-tree-may-not-contain-a-dynamic-operation) – LCJ Jun 26 '17 at 17:41

2 Answers2

21

You are missing the @model reference in your HTML. The first line that it encounters a lambda, and cannot find a model, it will throw this error.

At the top of your cshtml, add @model OrderGasModel

Claies
  • 22,124
  • 4
  • 53
  • 77
  • dangit... so easy. I've been banging my head on this and overlooked that completely. Tyvm – ErocM Feb 12 '15 at 22:39
1

In my case @model existed at the top of my cshtml page, but because I was referencing a List of objects, my "@model" statement was still considered a dynamic operation by the compiler. My code looked like this:

@model List<MyDataStructure>

So I had to declare a class containing the list in question, then I had to alter the code to load the list (since my ".ToList()" statement wouldn't work any longer). I ended up with this:

@model ListMyDataStructure

public class ListMyDataStructure
{
    public List<MyDataStructure> ListOfData { get; set; }
}
Brian
  • 3,653
  • 1
  • 22
  • 33