【问题标题】:'System.Web.Mvc.HtmlHelper' has no applicable method named 'Partial'“System.Web.Mvc.HtmlHelper”没有名为“Partial”的适用方法
【发布时间】:2015-04-12 11:01:29
【问题描述】:

我收到此错误:

错误 CS1973:“System.Web.Mvc.HtmlHelper”不适用 名为“部分”的方法,但似乎有一个扩展方法 姓名。扩展方法不能动态调度。考虑 强制转换动态参数或调用扩展方法 扩展方法语法。"}

从我在这里读到的Razor View Engine : An expression tree may not contain a dynamic operation 是由于使用了我真正使用 Session 的 viewbag(?)。

这是我的网络表单:

@using SuburbanCustPortal.MiscClasses

@{
    ViewBag.Title = "Account Screen";
 }

<h2>AccountScreen</h2>

<div class="leftdiv">
  <fieldset>
    <legend>Customer Info</legend>
    @Html.Partial("CustomerInfo")
  </fieldset>

  <fieldset>
    <legend>Delivery Address</legend>
    @Html.Partial("DeliveryAddress")
  </fieldset>

  <fieldset>
    <legend>Delivery Info</legend>
    @Html.Partial("DeliveryInfo")
  </fieldset>
</div>

<div class="rightdiv">
  <fieldset> 
    <legend>Balance</legend>
      @Html.Partial("AccountBalance")
  </fieldset>

             @if (SessionHelper.ShowPaymentOptions || SessionHelper.ShowHistory)
             {
               <fieldset>
                 <legend>Account Options</legend>
                 <br/>

                 @using (Html.BeginForm("AccountScreenButton", "Customer", FormMethod.Post))
                 {
                   <div class="sidebysidebuttons">
                     <div class="box">
                       @if (SessionHelper.ShowHistory && SessionHelper.ShowAccountScreenPaymentButton)
                       {
                         <button class="sidebysidebutton1" name="options" value="payment">Make a Payment</button>
                         <button class="sidebysidebutton2" name="options" value="activity">Display Activity</button>
                       }
                       else
                       {
                         if (SessionHelper.ShowAccountScreenPaymentButton)
                         {
                           <button class="leftpaddedsinglebutton" name="options" value="payment">Make a Payment</button>
                         }

                         if (SessionHelper.ShowHistory)
                         {
                           <button class="leftpaddedsinglebutton" name="options" value="activity">Display Activity</button>
                         }
                       }
                     </div>
                   </div>
                 }    
               </fieldset>
             }

  <fieldset> 
      <legend>Billing Info</legend>
        @Html.Partial("BillingInfo", Model)
    </fieldset>
</div>

这是我的 SessionHelper 的一部分,给你一个想法:

public static CustomerData CustomerSessionData
{
  get
  {
    try
    {
      return (CustomerData) HttpContext.Current.Session["CustomerSessionData"];
    }
    catch (Exception)
    {
      return null;
    }
  }
  set { HttpContext.Current.Session["CustomerSessionData"] = value; }
}

    public static bool ShowPaymentTab
    {
      get { return HttpContext.Current.Session["ShowPaymentTab"].ToBool(); }
      set { HttpContext.Current.Session["ShowPaymentTab"] = value; }
    }

我不确定问题是否出在表单中,因为当我在表单中设置断点时,它并没有就此停止。

我有两个问题:

  1. 如何调试表单上的问题所在?
  2. 能否不将类用作会话并在表单中引用它?我假设这就是问题所在。

【问题讨论】:

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


    【解决方案1】:

    您的问题是您没有在视图顶部定义模型。因此,默认类型是dynamic

    通常,这不是问题,但你有这个:

    @Html.Partial("BillingInfo", Model)
    

    这实际上是将动态类型传递给您的Html.Partial(),这就是引发错误的原因。

    无论如何,这是一个毫无意义的调用,只需删除它的模型部分,它应该可以工作。无论如何,传递模型是默认操作,因此您不会尝试做任何不默认的操作。

    【讨论】:

    • 哈...我什至没有注意到这一点。泰!关于在 mvc 中调试 Web 表单的任何想法?
    • @ErocM - 您需要将光标直接设置在要调试的对象上,然后按 F9。例如,如果要调试这种情况,请将光标放在 .Partial 部分,然后按 F9。如果您不这样做,它将无法获得正确的上下文。
    • 类似问题,但只是尝试从动态对象传递字符串。只需要先转换为字符串。
    【解决方案2】:

    以下情况也会出现此错误:

    • 假设您有两个局部视图名称 A 和 B
    • 如果您在 index.html 中调用局部视图 A 并将其中的模型作为 @Html.RenderPartialAsync("A",Model) 传递,那么局部视图 A 将捕获模型
    • 现在,如果您尝试调用 Partialview B 并再次传递模型,则会产生错误,因为这次模型是作为动态对象传递的。
    • 您需要将部分视图 B 称为 @Html.RenderPartialAsync("B"),模型将自动继承到第二个部分视图中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-08
      • 2012-08-16
      • 2014-01-29
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 2022-08-04
      • 2018-10-21
      相关资源
      最近更新 更多