简体   繁体   中英

'System.Web.Mvc.HtmlHelper' has no applicable method named 'Partial'

I am getting this error:

error CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'Partial' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax."}

From what I read here Razor View Engine : An expression tree may not contain a dynamic operation is that it is due to using viewbag(?) which I am really using Session.

This is my web form:

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

This is part of my SessionHelper to give you an idea:

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

I'm not sure were the issue is in the form since when I put a break point in the form, it does not stop there.

I have two questions:

  1. How do I debug where the issue is on the form?
  2. Can I not use a class as a session and reference it in the form? I'm assuming that is where the issue is at.

Your problem is that you don't define a model at the top of your view. Because of this, the default type is of type dynamic .

Normally, this isn't a problem, but you have this:

@Html.Partial("BillingInfo", Model)

This is, in effect, passing a dynamic type to your Html.Partial() , which is what is throwing the error.

This is a pointless call anyways, just remove the Model part of it and it should work. Passing the Model is the default operation anyways, so you are not trying to do anything that wouldn't be the default.

This error occurs in following condition as well:

  • Let's say you have two partial views names A and B
  • If you are calling the partial view A in index.html and passing model in it as @Html.RenderPartialAsync("A",Model) then the partial view A will capture the model
  • Now if you try to call Partialview B and pass the model again, it will create an error because this time the model is passing as a dynamic object.
  • You need to call the partialview B as @Html.RenderPartialAsync("B") and model will automatically inherit in the second partial view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM