表达式树可能不包含动态操作,但它没有告诉我在哪里

时间:2015-02-12 22:22:15

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

我遇到的问题是我收到消息:

  

\ OrderGas.cshtml(24):错误CS1963:表达式树可能不包含   动态操作“}

以下是我的网络表单代码:

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

以下是调用Web表单的类:

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");
  }
}

我把这与我的历史进行了比较,看看我改变了什么,我不明白为什么它不再起作用了。当我对我的PaymentMethods进行调试时,它会在它到达之前给出异常。

我已经尝试评论cshmtl的部分内容,但我无法得到一个明确的答案,即它的哪一部分导致错误。

我不知所措......有谁看到我做错了什么?

2 个答案:

答案 0 :(得分:13)

您缺少HTML中的@model引用。它遇到lambda的第一行,找不到模型,就会抛出这个错误。

在cshtml的顶部,添加@model OrderGasModel

答案 1 :(得分:1)

在我的情况下@model存在于我的cshtml页面的顶部,但由于我引用了一个对象列表,我的“@model”语句仍然被编译器视为动态操作。我的代码看起来像这样:

@model List<MyDataStructure>

所以我必须声明一个包含有问题列表的类,然后我不得不改变代码来加载列表(因为我的“.ToList()”语句将不再起作用)。我最终得到了这个:

@model ListMyDataStructure

public class ListMyDataStructure
{
    public List<MyDataStructure> ListOfData { get; set; }
}