“System.Web.Mvc.HtmlHelper”没有名为“Partial”的适用方法

2024-04-23

我收到此错误:

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

从我在这里读到的Razor View Engine:表达式树可能不包含动态操作 https://stackoverflow.com/questions/4155392/razor-view-engine-an-expression-tree-may-not-contain-a-dynamic-operation是因为使用了 viewbag(?),我实际上正在使用 Session。

这是我的网络表单:

@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. 我可以不使用类作为会话并在表单中引用它吗?我假设这就是问题所在。

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

通常,这不是问题,但你会遇到这样的情况:

@Html.Partial("BillingInfo", Model)

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

“System.Web.Mvc.HtmlHelper”没有名为“Partial”的适用方法 的相关文章

随机推荐