24

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.
Community
  • 1
  • 1
ErocM
  • 4,505
  • 24
  • 94
  • 161

2 Answers2

49

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.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Ha... I didn't even notice that. Ty! Any ideas on debugging a web form in mvc? – ErocM Feb 11 '15 at 22:03
  • 1
    @ErocM - You need to set the cursor directly on the object you want to debug, then hit F9. For instance, if you want to debug this situation, you would put the cursor on the .Partial part, and press F9. If you don't do that, it won't get the right context. – Erik Funkenbusch Feb 11 '15 at 22:06
  • Similar issue, but just trying to pass a string from a dynamic object. Just had to cast to string first. – Randy Hall Nov 22 '19 at 20:10
0

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.