【问题标题】:An expression tree may not contain a dynamic operation but it's not telling me where表达式树可能不包含动态操作,但它没有告诉我在哪里
【发布时间】:2015-04-13 20:51:07
【问题描述】:

我在收到消息时遇到问题:

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

这是调用网络表单的类:

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 的某些部分,但我无法得到明确的答案,说明它的哪一部分导致了错误。

我不知所措...有人知道我做错了什么吗?

【问题讨论】:

  • 如果我计算正确,它会抱怨模型类的 PaymentMethod 成员。有什么具体的吗?我没有看到你在操作代码中设置它。
  • @WiktorZychla 你能告诉我你是怎么想到的吗?我不明白你所说的计数是什么意思?
  • 我已经回答过了,但是为了解释更多关于@WiktorZychla 的评论,错误指示\OrderGas.cshtml(24):,它对应于该特定文件顶部的第24 行。这恰好是一个看起来相当正常的 lambda,但很明显要寻找 lambda 不会被处理的原因。
  • @Claies 啊我不知道 24 是指第 24 行!很高兴知道!这将极大地帮助将来找到我的问题。我想现在我知道这才有意义...... Tyvm!

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


【解决方案1】:

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

在你的cshtml顶部,添加@model OrderGasModel

【讨论】:

  • dangit... 这么简单。我一直在努力解决这个问题,完全忽略了这一点。 Tyvm
【解决方案2】:

在我的例子中,@model 存在于我的 cshtml 页面的顶部,但因为我引用了一个对象列表,所以我的“@model”语句仍然被编译器视为动态操作。我的代码如下所示:

@model List<MyDataStructure>

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

@model ListMyDataStructure

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-13
    • 2013-07-27
    • 1970-01-01
    • 2011-10-30
    • 2012-05-05
    • 1970-01-01
    • 2012-09-07
    相关资源
    最近更新 更多