1
votes

I'm having some trouble understand Partial, RenderPartial, and RenderAction for partial views and what parameters to include. When I run the partial view by itself it works. When I run RenderAction in my AccountHistory, which has the same @model, it works, but if I try Partial or RenderPartial it returns the data as null. I can't get any of these to work in Layout for any page other than AccountHistory. My guess is I'm not sending it through the correct controller. How do I fix this?

Here's my code:

AccountHistoryVM

namespace Infotech.Coverpools.Portal.Tintaglia.Web.Models
{
    public class AccountHistoryVM
    {
        public IEnumerable<CustLedgerEntry> Records { set; get; }
        public IEnumerable<CustAcctSummary> Summary { set; get; }

        public DateTime? LastUpdatedDate { set; get; }
    }
}

CustLedgerEntry Controller

namespace Infotech.Coverpools.Portal.Tintaglia.Web.Controllers
{
    public class CustLedgerEntryController : Controller
    {
     private TintagliaContext db = new TintagliaContext();

       public ActionResult CustAcctSummary(AccountHistoryVM AcctSum)

        {
            var dealerId = db.UserProfiles.Where(d => d.UserName.Equals(User.Identity.Name)).Select(d => d.CustomerId).FirstOrDefault();
            AcctSum = new AccountHistoryVM();
            var OpenOrders = db.CustAcctSummaries.FirstOrDefault(m => m.NoOrders != null);

            AcctSum.Summary = db.CustAcctSummaries.ToList().Where(x => x.CustomerNo_ == dealerId);

            var datetoweb = db.CustLedgerEntries.FirstOrDefault(m => m.DatetoWebSales != null);
            AcctSum.LastUpdatedDate = datetoweb.DatetoWebSales;
            return PartialView(AcctSum);
        }
    }
}

CustAcctSummary

public ActionResult CustAcctSummary(AccountHistoryVM AcctSum)

        {
            var dealerId = db.UserProfiles.Where(d => d.UserName.Equals(User.Identity.Name)).Select(d => d.CustomerId).FirstOrDefault();
            AcctSum = new AccountHistoryVM();
            var OpenOrders = db.CustAcctSummaries.FirstOrDefault(m => m.NoOrders != null);

            AcctSum.Summary = db.CustAcctSummaries.ToList().Where(x => x.CustomerNo_ == dealerId);

            var datetoweb = db.CustLedgerEntries.FirstOrDefault(m => m.DatetoWebSales != null);
            AcctSum.LastUpdatedDate = datetoweb.DatetoWebSales;
            return PartialView(AcctSum);
        }

CustAcctSummary.cshtml (Partial)

@model Infotech.Coverpools.Portal.Tintaglia.Web.Models.AccountHistoryVM
<div id="AccountSummaryContainer">
    <h2>Account Summary</h2>

    <div id="AccountSummary">
       @if (Model.Summary != null)
       {
        foreach (var item in Model.Summary)
        {
            <p class="label">Open Orders </p><p class="value">@Html.DisplayFor(m => item.NoOrders)</p>
            <p class="label">Outstanding Orders </p><p class="value">@Html.DisplayFor(m => item.OutstandingOrders)</p>
            <p class="label">TOTAL BALANCE DUE </p><p class="value">@Html.DisplayFor(m => item.BalanceDue)</p>
            <p class="label">Credit Limit </p><p class="value"> @Html.DisplayFor(m => item.CreditLimit)</p>
            <p class="label">Amount Past Due </p><p class="value"> @Html.DisplayFor(m => item.Balance)</p>
        }
       }
    </div>

    <div class="updateDate">
        @Html.DisplayFor(model => model.LastUpdatedDate)
    </div>
</div>

_Layout.cshtml (condensed with various tries)

@{Html.RenderAction("CustAcctSummary", "CustAccountSummary");}'
//works in AccountHistory, but get the following when I try to go to a difference page (eg Home/Index): A public action method 'CustAcctSummary' was not found on controller 'Infotech.Coverpools.Portal.Tintaglia.Web.Controllers.HomeController'.

@{Html.RenderAction("CustAcctSummary", Model.Summary);}
Error   CS1973  'HtmlHelper<dynamic>' has no applicable method named 'RenderAction' 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.   Infotech.Coverpools.Portal.Tintaglia.Web    F:\sites\OnlineOrder\Coverpools Dealer Portal\Infotech Coverpools Portal Tintaglia\Source\Infotech.Coverpools.Portal.Tintaglia.Web\Views\Shared\_Layout.cshtml  142 Active

@Html.Partial("CustAcctSummary");
//Returns blank Account Summary in AccountHistory: Other pages: The partial view 'CustAcctSummary' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/CustAcctSummary.aspx
~/Views/Home/CustAcctSummary.ascx
~/Views/Shared/CustAcctSummary.aspx
~/Views/Shared/CustAcctSummary.ascx
~/Views/Home/CustAcctSummary.cshtml
~/Views/Home/CustAcctSummary.vbhtml
~/Views/Shared/CustAcctSummary.cshtml
~/Views/Shared/CustAcctSummary.vbhtml

@Html.Partial("/Views/CustLedgerEntry/CustAcctSummary");
//Returns blank Account Summary in AccountHistory: Other Pages: The partial view '/Views/CustLedgerEntry/CustAcctSummary' was not found or no view engine supports the searched locations. The following locations were searched:
/Views/CustLedgerEntry/CustAcctSummary
1

1 Answers

0
votes

Change your action like:

    public ActionResult CustAcctSummary()

    {
        var AcctSum = new AccountHistoryVM();

        var dealerId = db.UserProfiles.Where(d => d.UserName.Equals(User.Identity.Name)).Select(d => d.CustomerId).FirstOrDefault();

        var OpenOrders = db.CustAcctSummaries.FirstOrDefault(m => m.NoOrders != null);

        AcctSum.Summary = db.CustAcctSummaries.ToList().Where(x => x.CustomerNo_ == dealerId);

        var datetoweb = db.CustLedgerEntries.FirstOrDefault(m => m.DatetoWebSales != null);
        AcctSum.LastUpdatedDate = datetoweb.DatetoWebSales;
        return PartialView(AcctSum);
    }

Then

 @{Html.RenderAction("CustAcctSummary", "CustLedgerEntry", new { area = "" });}

Areas are pieces of an MVC application that do not use the main controllers, actions, and views; rather, they mimic this structure within a specific subfolder in the project directory.In Visual Studio, You can scaffold an area by right-clicking on the project file and selecting Add -> Area.

area="" means the controller location is main controller folder (out side areas).

Hopefully it's help you.