How can i generate downloadable PDF from the items in my Cart - e-commerce

I am using dompdf to generate a downloadable pdf from my multi-vendor eCommerce shopping cart and I am encountering this error even after defining $cartItems in QuotationController. $cartItems is defined in CartController I intend to fetch the items of CartController to the pdf
Here is the error:
Undefined variable: cartItems (View: C:\laragon\www\procure\resources\views\cart\quote.blade.php)
Here is QuoteController code:
<?php
namespace App\Http\Controllers;
use Barryvdh\DomPDF\PDF;
use Darryldecode\Cart\Cart;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class QuotationController extends Controller
{
public function quote(Request $request)
{
$product = DB::table('products')->get();
$cartItems = \Cart::session(auth()->id())->getContent();
$pdf = \PDF::loadView('cart.quote');
return $pdf->download('quotation.pdf');
//return view('cart.quote');
}
}
Heres is CartController:
<?php
namespace App\Http\Controllers;
use App\Product;
use Darryldecode\Cart\Cart;
class CartController extends Controller
{
public function add(Product $product)
{
// add the product to cart
\Cart::session(auth()->id())->add(array(
'id' => $product->id,
// 'img' => $product->cover_img,
'name' => $product->name,
'price' => $product->price,
'quantity' => 1,
'attributes' => array(),
'associatedModel' => $product,
));
return redirect()->route('cart.index');
}
public function index()
{
$cartItems = \Cart::session(auth()->id())->getContent();
view()->share('cartItems', $cartItems);
return view('cart.index', compact('cartItems'));
}
public function destroy($itemId)
{
\Cart::session(auth()->id())->remove($itemId);
return back();
}
public function update($rowId)
{
\Cart::session(auth()->id())->update($rowId, [
'quantity' => [
'relative' => false,
'value' => request('quantity'),
],
]);
return back();
}
public function checkout()
{
return view('cart.checkout');
}
}
And finally the blade file(quote.blade.php):
<section class="shopping-cart">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="cart-table table-responsive">
<table class="table">
<thead>
<tr>
<th class="t-pro">Product</th>
<th class="t-price">Price</th>
<th class="t-qty">Quantity</th>
<th class="t-total">Total</th>
<th class="t-rem"></th>
</tr>
</thead>
<tbody>
#foreach ($cartItems as $item)
<tr>
<td class="t-pro d-flex">
<div class="t-content">
<p class="t-heading">{{ $item->name }}</p>
</div>
</td>
<td class="t-price">KES {{ $item->price }} </td>
<td class="t-qty">
<div class="qty-box">
<div class="quantity buttons_added">
<form action="{{route('cart.update', $item->id)}}" method="get">
<input name="quantity" type="number" value="{{ $item->quantity }}">
<input class="button" type="submit" value="save">
</form>
</div>
</div>
</td>
<td class="t-total">{{Cart::session(auth()->id())->get($item->id)->getPriceSum()}}</td>
<td class="t-rem"><i class="far fa-trash-alt"></i></td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
<div class="col-md-4">
<div class="coupon">
<h6>Discount Coupon</h6>
<p>Enter your coupon code if you have one</p>
<form action="#">
<input type="text" name="zip" value="" placeholder="Your Coupon">
<button type="button">Apply Code</button>
</form>
Print Quotation
</div>
</div>
<div class="col-md-4">
<div class="crt-sumry">
<h5>Cart Summery</h5>
<ul class="list-unstyled">
<li>Subtotal <span>KES {{\Cart::session(auth()->id())->getTotal()}}</span></li>
<li>Shipping & Tax <span>0.00</span></li>
<li>Grand Total <span>KES {{\Cart::session(auth()->id())->getTotal()}}</span></li>
</ul>
<div class="cart-btns text-right">
<button onclick="location.href='{{route('home')}}'" class="up-cart">Update Cart</button>
<button onclick="location.href='{{route('cart.checkout')}}'" class="chq-out ">Checkout</button>
</div>
</div>
</div>
</div>
</div>
</section>

Just visit this https://github.com/arafkarim/HTML-to-PDF i have just applied this thing in my similar project.

Related

ASP .NET core Razor page - Avoid refreshing the page

I have tow buttons in one Form, first button for add website info to a local table and second button for add social media info to another table, after all info added locally,
then I can click on 'add all info' button for add all info in same time to database.
My question is how can I add info to a table without refreshing the page?
AddAllInfo.cshtml:
<div class="col-md-6 mb-3">
<label class="form-label" asp-for="NewWebSiteInfo.websiteName">Website URL</label>
<input type="text" asp-for="NewWebSiteInfo.websiteName" class="form-control" />
</div>
<div class="col-md-6 mb-3">
<label class="form-label" asp-for="NewWebSiteInfo.websiteUrl">Website URL</label>
<input type="text" asp-for="NewWebSiteInfo.websiteUrl" class="form-control" />
</div>
<button type="submit" validatedisable="True" asp-page-handler="AddWebsiteInfo" class="btn btn-primary" >Add Website info</button>
<div class="mb-3">
#if (AddInfoModel.WebSitelist.Count > 0)
{
<div class="col-12 border p-3">
<table class="table table-striped table-bordered">
<thead style="background-color:lightgray">
<tr>
<th>WebsiteName</th>
<th>websiteURL</th>
</tr>
</thead>
<tbody>
#foreach (Website item in AddAllInfoModel.WebSitelist)
{
<tr>
<td>#item.WebsiteName</td>
<td>#item.websiteURL</td>
</tr>
}
</tbody>
</table>
</div>
}
</div>
</br>
</br>
<div class="col-md-6 mb-3">
<label class="form-label" asp-for="NewSocialMediaInfo.SocialMediaName">Social Media</label>
<input type="text" asp-for="NewSocialMediaInfo.SocialMediaName" class="form-control" />
</div>
<div class="col-md-6 mb-3">
<label class="form-label" asp-for="NewSocialMediaInfo.SocialMediaAccount">Account</label>
<input type="text" asp-for="NewSocialMediaInfo.SocialMediaAccount" class="form-control" />
</div>
<button type="submit" validatedisable="True" asp-page-handler="AddSocialMediaInfo" class="btn btn-primary" >Add socil Media info</button>
<div class="mb-3">
#if (AddInfoModel.SocialMedialist.Count > 0)
{
<div class="col-12 border p-3">
<table class="table table-striped table-bordered">
<thead style="background-color:lightgray">
<tr>
<th>SocialMediaName</th>
<th>SocialMediaAccount</th>
</tr>
</thead>
<tbody>
#foreach (SocialMedia item in AddAllInfoModel.SocialMedialist)
{
<tr>
<td>#item.SocialMediaName</td>
<td>#item.SocialMediaAccount</td>
</tr>
}
</tbody>
</table>
</div>
}
</div>
</br>
<div class="col-4 offset-2">
<button type="submit" class="btn btn-primary form-control"> Add all info </button>
</div>
</form> ```
AddAllInfo.cshtml.cs:
public void OnPostAddSocialMediaInfo()
{
SocialMedialist.Add(new SocialMedia { SocialMediaName = NewSocialMediaInfo.SocialMediaName,
SocialMediaAccount=NewSocialMediaInfo.SocialMediaAccount});
}
public void OnPostAddWebsiteInfo()
{
WebSitelist.Add(new WebSite { WebSiteName = NewWebSiteInfo.WebsiteName,
websiteUrl =NewWebSiteInfo.websiteUrl});
}
You can use js to pass data to handler,and then use js to add html to tbody,here is a demo to add data to table without refresh the page:
<div class="col-md-6 mb-3">
<label class="form-label" asp-for="NewWebSiteInfo.websiteName">Website URL</label>
<input type="text" asp-for="NewWebSiteInfo.websiteName" class="form-control" />
</div>
<div class="col-md-6 mb-3">
<label class="form-label" asp-for="NewWebSiteInfo.websiteUrl">Website URL</label>
<input type="text" asp-for="NewWebSiteInfo.websiteUrl" class="form-control" />
</div>
<input type="button" onclick="AddWebsiteInfo()" class="btn btn-primary" value="Add Website info">
<div class="mb-3">
#if (AddInfoModel.WebSitelist.Count > 0)
{
<div class="col-12 border p-3">
<table class="table table-striped table-bordered">
<thead style="background-color:lightgray">
<tr>
<th>WebsiteName</th>
<th>websiteURL</th>
</tr>
</thead>
<tbody>
#foreach (Website item in AddAllInfoModel.WebSitelist)
{
<tr>
<td>#item.WebsiteName</td>
<td>#item.websiteURL</td>
</tr>
}
</tbody>
</table>
</div>
}
</div>
</br>
</br>
<div class="col-md-6 mb-3">
<label class="form-label" asp-for="NewSocialMediaInfo.SocialMediaName">Social Media</label>
<input type="text" asp-for="NewSocialMediaInfo.SocialMediaName" class="form-control" />
</div>
<div class="col-md-6 mb-3">
<label class="form-label" asp-for="NewSocialMediaInfo.SocialMediaAccount">Account</label>
<input type="text" asp-for="NewSocialMediaInfo.SocialMediaAccount" class="form-control" />
</div>
<input type="button" onclick="AddSocialMediaInfo()" class="btn btn-primary" value="Add socil Media info">
<div class="mb-3">
#if (AddInfoModel.SocialMedialist.Count > 0)
{
<div class="col-12 border p-3">
<table class="table table-striped table-bordered">
<thead style="background-color:lightgray">
<tr>
<th>SocialMediaName</th>
<th>SocialMediaAccount</th>
</tr>
</thead>
<tbody>
#foreach (SocialMedia item in AddAllInfoModel.SocialMedialist)
{
<tr>
<td>#item.SocialMediaName</td>
<td>#item.SocialMediaAccount</td>
</tr>
}
</tbody>
</table>
</div>
}
</div>
</br>
<div class="col-4 offset-2">
<input type="button" onclick="AddAllInfo()" class="btn btn-primary" value="Add all info">
</div>
</form> ```
js:
#section Scripts{
<script>
function AddWebsiteInfo() {
var NewWebSiteInfo = {
'websiteName': $("#NewWebSiteInfo_websiteName").val(),
'websiteUrl': $("#NewWebSiteInfo_websiteUrl").val()
};
$.ajax({
type: 'POST',
url: '?handler=AddWebsiteInfo',
data: NewWebSiteInfo,
headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
dataType: 'json',
success: function (data) {
var html = "<tr><td>" + data.websiteName + "</td><td>" + data.websiteUrl + "</td></tr>";
$("tbody")[0].innerHTML = $("tbody")[0].innerHTML + html;
}
});
}
function AddSocialMediaInfo() {
var NewAddSocialMediaInfo = {
'SocialMediaName': $("#NewSocialMediaInfo_SocialMediaName").val(),
'SocialMediaAccount': $("#NewSocialMediaInfo_SocialMediaAccount").val()
};
$.ajax({
type: 'POST',
url: '?handler=AddSocialMediaInfo',
data: NewAddSocialMediaInfo,
headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
dataType: 'json',
success: function (data) {
var html = "<tr><td>" + data.socialMediaName + "</td><td>" + data.socialMediaAccount + "</td></tr>";
$("tbody")[1].innerHTML = $("tbody")[1].innerHTML + html;
}
});
}
function AddAllInfo() {
AddWebsiteInfo();
AddSocialMediaInfo();
}
</script>
}
handler:
[BindProperty]
public WebSiteInfo NewWebSiteInfo { get; set; }
[BindProperty]
public SocialMediaInfo NewSocialMediaInfo { get; set; }
public void OnGet()
{
}
public JsonResult OnPostAddWebsiteInfo()
{
WebSitelist.Add(new WebSite { WebSiteName = NewWebSiteInfo.WebsiteName,
websiteUrl =NewWebSiteInfo.websiteUrl});
return new JsonResult(NewWebSiteInfo);
}
public JsonResult OnPostAddSocialMediaInfo()
{
SocialMedialist.Add(new SocialMedia { SocialMediaName = NewSocialMediaInfo.SocialMediaName,
SocialMediaAccount=NewSocialMediaInfo.SocialMediaAccount});
return new JsonResult(NewSocialMediaInfo);
}
The best way to do this is to run the post request in javascript, this will not refresh the page.
2nd option is by calling the get method again at the end of the post method and then passing the model with the data to the get. So that when the page refreshes the data is filled in again

Confirmation Boostrap modal posting

I'm attempting to take a confirm event and turning it into a modal. Here is the original code:
#page
#model AllCustomerModel
#{
ViewData["Title"] = "AllCustomer";
}
<h2>All Customers</h2>
<p>
<a asp-page="Customer">Create New Customer</a>
</p>
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayName("Name")
</th>
<th>
#Html.DisplayName("Address")
</th>
<th>
#Html.DisplayName("Country")
</th>
<th>
#Html.DisplayName("City")
</th>
<th>
#Html.DisplayName("Phone")
</th>
<th>Edit | Delete</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.customerList)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address)
</td>
<td>
#Html.DisplayFor(modelItem => item.Country)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
<td>
#Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
<a asp-page="./EditCustomer" asp-route-id="#item.CustomerID">Edit</a> |
<a asp-page="./AllCustomer" onclick="return confirm('Are you sure you want to delete this item?');" asp-page-handler="Delete" asp-route-id="#item.CustomerID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
Code Behind,
public ActionResult OnGetDelete(int? id)
{
if (id != null)
{
var data = _context.CustomerTB.Find(id);
_context.Remove(data);
_context.SaveChanges();
}
return RedirectToPage("AllCustomer");
}
Here is what I've tried/currently trying (for brevity sake, I haven't pasted the MVP)
<button type="button" class="btn btn-primary" data-toggle="modal" data-id="#item.CustomerID" data-target="#myModal">
Delete
</button>
<div class="modal fade" id="myModal">
<form id="myForm" method="post">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Delete Customer</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
Are you sure you want to delete this customer?
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-danger">Yes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
</div>
</div>
</div>
</form>
</div>
Code Behind,
[BindProperty]
public Customer Customer { get; set; }
public void OnPost()
{
Customer customer = Customer;
if (customer != null)
_context.Remove(_context.CustomerTB.Find(customer.CustomerID));
customerList = _context.CustomerTB.ToList();
}
It will hit the OnPost method but the customer object is empty. Is this a proper way to tackle this issue? What more do I need to add in order to pick up the object on the code behind?
You need to pass the selected customerId to modal(with a hidden type) by javascript when click the button,and then bind the Customer.CustomerId in page using asp-for tag helper.
For example:
<button type="button" class="btn btn-primary myButton" data-toggle="modal" data-id="#item.CustomerID" data-target="#myModal">
Delete
</button>
<div class="modal fade" id="myModal">
<form id="myForm" method="post">
<div class="modal-dialog">
<div class="modal-content">
//..
<div class="modal-body">
<input type="hidden" class="hiddenid" asp-for="Customer.CustomerID" />
Are you sure you want to delete this customer?
</div>
//..
</div>
</div>
</form>
</div>
#section Scripts{
<script>
$('.myButton').on('click', function (event) {
var passedID = $(this).data('id');
$(".modal-body .hiddenid").val(passedID);
});
</script>
}

How to use extra column in a pivot table

I need to make a custom field just like checklist, but in a table and with a second field "order".
This is what I have:
Model 1: Conditions with relations "belongsToMany"
Model 2: SubServices
These two models are in relations with the table products_service that has the two foreign_keys plus the column "order"
What I can't understand is:
How could save the extra information with backpack.
I know that I can use something like this:
$user->roles()->updateExistingPivot($roleId, $attributes);
but where should I put it?
Here's my code:
class Condition extends Model
{
use CrudTrait;
use Searchable;
public function relService()
{
//dd($this->belongsToMany(SubService::class, 'conditions_service')->withPivot('weight')->withTimestamps());
return $this->belongsToMany(SubService::class, 'conditions_service')->withPivot('weight')->withTimestamps();
}
}
class SubService extends Model
{
use Searchable;
use CrudTrait;
public function conditions()
{
return $this->belongsToMany(Condition::class, 'conditions_service')->withPivot('weight')->withTimestamps();
}
}
Here's my Custom Field Type:
<!-- select2 -->
<div #include('crud::inc.field_wrapper_attributes') >
<label>{!! $field['label'] !!}</label>
#include('crud::inc.field_translatable_icon')
<?php $entity_model = $crud->getModel(); ?>
<div class="row">
<div class="col-sm-12">
<table id="crudTable" class="table table-bordered table-striped display">
<thead>
<tr>
#if ($crud->details_row)
<th></th> <!-- expand/minimize button column -->
#endif
{{-- Table columns --}}
<th>Seleziona</th>
<th>Ordine</th>
{{--<th>Ordine <i class="fa fa-arrow-up" aria-hidden="true"></i></th>
<th>Ordine <i class="fa fa-arrow-down" aria-hidden="true"></i></th>--}}
{{--$tst = $connected_entity_entry->relService->whereIn('id', $connected_entity_entry->id)--}}
</tr>
</thead>
<tbody>
#foreach ($field['model']::all() as $connected_entity_entry) {{--var_dump((empty($connected_entity_entry->conditions->toArray())?"puppa":(empty($connected_entity_entry->conditions->whereIn('id', $connected_entity_entry->id)->toArray())?"puppa uguale":$connected_entity_entry->conditions->whereIn('id', $connected_entity_entry->id)))) --}}
{{-- dump($connected_entity_entry->getPriority($connected_entity_entry->id)) --}}
<tr>
<th scope="row">
<div class="col-sm-4">
<div class="checkbox">
<label>
<input type="checkbox"
name="{{ $field['name'] }}[]"
value="{{ $connected_entity_entry->id }}"
#if( ( old( $field["name"] ) && in_array($connected_entity_entry->id, old( $field["name"])) ) || (isset($field['value']) && in_array($connected_entity_entry->id, $field['value']->pluck('id', 'id')->toArray())))
checked="checked"
#endif > {!! $connected_entity_entry->{$field['attribute']} !!}
</label>
</div>
</div>
</th>
<td>{{--#include('crud::fields.number')--}}
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
{{-- HINT --}}
#if (isset($field['hint']))
<p class="help-block">{!! $field['hint'] !!}</p>
#endif
</div>
</div>
Thank you for your help!
Dave
I found a solution thanks dense-team with his pull request (https://github.com/Laravel-Backpack/CRUD/pull/351)
Here's my code updated:
CrudController:
$this->crud->addField([
'label' => 'Servizi legati',
'type' => 'checklist_ordered',
'name' => 'relService',
'entity' => 'relService',
'attribute' => 'title',
'model' => "App\Models\SubService",
'pivot' => true,
'pivotFields' => [
'weight' => 'Ordinamento',
],
], 'update/create/both');
Here's my Custom Field Type:
<!-- select2 -->
<div #include('crud::inc.field_wrapper_attributes') >
<label>{!! $field['label'] !!}</label>
#include('crud::inc.field_translatable_icon')
#if (isset($field['model']))
<?php $entity_model = $crud->getModel();
$pivot_entries = null;
if (isset($entry)) {
$pivot_entries = $entry->{$field['entity']}->keyBy(function ($item) {
return $item->getKey();
});
}
?>
<div class="row">
<div class="col-sm-12">
<table id="crudTable" class="table table-bordered table-striped display">
<thead>
<tr>
#if ($crud->details_row)
<th></th> <!-- expand/minimize button column -->
#endif
{{-- Table columns --}}
<th>Seleziona</th>
#foreach($field['pivotFields'] as $pivot_chunk)
<th>{{$pivot_chunk}}</th>
#endforeach
</tr>
</thead>
<tbody>
#foreach ($field['model']::all() as $connected_entity_entry)
<tr>
<th scope="row">
<div class="col-sm-4">
<div class="checkbox">
<label>
<input type="checkbox"
name="{{ $field['name'] }}[]"
value="{{ $connected_entity_entry->id }}"
#if( ( old( $field["name"] ) && in_array($connected_entity_entry->id, old( $field["name"])) ) || (isset($field['value']) && in_array($connected_entity_entry->id, $field['value']->pluck('id', 'id')->toArray())))
checked="checked"
#endif > {!! $connected_entity_entry->{$field['attribute']} !!}
</label>
</div>
</div>
</th>
<td>
#foreach(array_chunk($field['pivotFields'], 2, true) as $pivot_chunk)
#foreach ($pivot_chunk as $pivot_field => $pivot_name)
<?php
$pivot_attr = null;
if ($pivot_entries) {
if ($pivot_entries->has($connected_entity_entry->getKey())) {
$pivot = $pivot_entries->get($connected_entity_entry->getKey())->pivot;
$pivot_attr = $pivot->getAttribute($pivot_field);
}
}
?>
<input type="number"
name="{!! $pivot_field !!}[{{ $connected_entity_entry->getKey() }}]"
value="{{ $pivot_attr }}" #include('crud::inc.field_attributes') />
#endforeach
#endforeach
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endif
{{-- HINT --}}
#if (isset($field['hint']))
<p class="help-block">{!! $field['hint'] !!}</p>
#endif
</div>
</div>

Display product options in Wishlist module - DNN Hotcakes

The current default view for the wish list module shows the product but not the options that were selected.
How can this be changed so that the option label and selected value are displayed as well?
I'm going to make an assumption that you're using Hotcakes 1.xx and not version 2.xx for this, but the code should be the same for both. I just only tested it in 01.10.04.
I've built an example of how to do this based upon the Cart view you'll find in the viewset already.
The original Wish List view looks like this:
#model IEnumerable<Hotcakes.Modules.Core.Areas.Account.Models.SavedItemViewModel>
<h2>#Localization.GetString("SavedItems")</h2>
#Html.Raw((string)TempData["messages"])
<div class="hc-record-list hc-wishlist clearfix">
#foreach (var item in Model)
{
<div class="hc-record">
<div class="hc-recimage">
<a href="#item.FullProduct.ProductLink">
<img src="#item.FullProduct.ImageUrls.SmallUrl" border="0" alt="#item.FullProduct.ImageUrls.SmallAltText" />
</a>
</div>
<div class="hc-recname">
<h2>#item.FullProduct.Item.ProductName</h2>
<div class="hc-recdescription">
#Html.Raw(item.FullProduct.Item.LongDescription)
</div>
</div>
<div class="hc-reccontrols">
<table class="dnnFormItem">
<tr>
<td class="hc-recprice">
#Html.Raw(item.FullProduct.UserPrice.DisplayPrice(true))
</td>
<td>
#if(!item.FullProduct.Item.IsGiftCard && !item.FullProduct.Item.IsUserSuppliedPrice)
{
using (Html.BeginHccRouteForm(HccRoute.WishList, new { action = "addtocart" }, FormMethod.Post))
{
<input type="hidden" name="itemid" value="#item.SavedItem.Id" />
<input class="dnnPrimaryAction" type="submit" value="#Localization.GetString("AddToCart")" />
}
}
</td>
<td>
#using (Html.BeginHccRouteForm(HccRouteNames.WishList, new { action = "delete" }, FormMethod.Post))
{
<input type="hidden" name="itemid" value="#item.SavedItem.Id" />
<input type="submit" class="hc-delete" value="#Localization.GetString("RemoveSavedItem")" />
}
</td>
</tr>
</table>
</div>
</div>
}
</div>
Below the description of the product, I added the following snippet:
#using Hotcakes.Commerce.Catalog
#if (item.SavedItem.SelectionData != null && item.SavedItem.SelectionData.OptionSelectionList != null && item.SavedItem.SelectionData.OptionSelectionList.Count > 0)
{
<div class="clearfix">
#Html.Raw(item.FullProduct.Item.Options.CartDescription(item.SavedItem.SelectionData.OptionSelectionList))
</div>
}
That makes the entire view look like this:
#using Hotcakes.Commerce.Catalog
#model IEnumerable<Hotcakes.Modules.Core.Areas.Account.Models.SavedItemViewModel>
<h2>#Localization.GetString("SavedItems")</h2>
#Html.Raw((string)TempData["messages"])
<div class="hc-record-list hc-wishlist clearfix">
#foreach (var item in Model)
{
<div class="hc-record">
<div class="hc-recimage">
<a href="#item.FullProduct.ProductLink">
<img src="#item.FullProduct.ImageUrls.SmallUrl" border="0" alt="#item.FullProduct.ImageUrls.SmallAltText" />
</a>
</div>
<div class="hc-recname">
<h2>#item.FullProduct.Item.ProductName</h2>
<div class="hc-recdescription">
#Html.Raw(item.FullProduct.Item.LongDescription)
</div>
#if (item.SavedItem.SelectionData != null && item.SavedItem.SelectionData.OptionSelectionList != null && item.SavedItem.SelectionData.OptionSelectionList.Count > 0)
{
<div class="clearfix">
#Html.Raw(item.FullProduct.Item.Options.CartDescription(item.SavedItem.SelectionData.OptionSelectionList))
</div>
}
</div>
<div class="hc-reccontrols">
<table class="dnnFormItem">
<tr>
<td class="hc-recprice">
#Html.Raw(item.FullProduct.UserPrice.DisplayPrice(true))
</td>
<td>
#if(!item.FullProduct.Item.IsGiftCard && !item.FullProduct.Item.IsUserSuppliedPrice)
{
using (Html.BeginHccRouteForm(HccRoute.WishList, new { action = "addtocart" }, FormMethod.Post))
{
<input type="hidden" name="itemid" value="#item.SavedItem.Id" />
<input class="dnnPrimaryAction" type="submit" value="#Localization.GetString("AddToCart")" />
}
}
</td>
<td>
#using (Html.BeginHccRouteForm(HccRouteNames.WishList, new { action = "delete" }, FormMethod.Post))
{
<input type="hidden" name="itemid" value="#item.SavedItem.Id" />
<input type="submit" class="hc-delete" value="#Localization.GetString("RemoveSavedItem")" />
}
</td>
</tr>
</table>
</div>
</div>
}
</div>

Reloading of layout causes Model to go empty (MVC4)

I'm trying to show a table of results in my view by use of HttpPost, but my view always turns up empty.
I have found out by now that the httpPost action works and returns a result, but before completing the action, the layout is called again, where the RenderBody is called again. Once the RenderBody is done, the view contains an empty model again and results in the page showing up empty.
Looking for advice how to solve this issue! I broke my head on this for hours and don't seem to see the light...
The View code:
#using MovieRental.DAL.Entities; #model List
<Movie>
#{ ViewBag.Title = "MyRentedMovies"; }
<h2>MyRentedMovies</h2>
<div class="container">
<div class="col-md-12 column">
<form id="frmmymovies" class="col-md-12">
<input id="currentuserid" type="hidden" name="userid" value="#ViewBag.user.UserId">
</form>
<div id="mymovies" class="btn btn-primary">Show my rented movies</div>
</div>
</div>
<div>
#ViewBag.cost;
</div>
#if (Model != null) {
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<table id="" class="table table-striped table-bordered">
<thead>
<tr>
<th>Action</th>
<th>Movie Title</th>
<th>Release Date</th>
<th>Storyline</th>
<th>Location</th>
<th>Movie Genre</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Action</th>
<th>Movie Title</th>
<th>Release Date</th>
<th>Storyline</th>
<th>Location</th>
<th>Movie Genre</th>
</tr>
</tfoot>
<tbody>
#foreach (var item in Model) {
<tr>
<td><i class="glyphicon glyphicon-plus" movieId="#item.MovieId"></i>
</td>
<td>#item.MovieName</td>
<td>#item.ReleaseDate</td>
<td>#item.Storyline</td>
<td>#item.Location.LocationName.ToString()</td>
<td>#item.MovieGenre.GenreName.ToString()</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
} else {
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="alert alert-info" role="alert">
You currenty don't have any rented movies in your possession.
</div>
</div>
</div>
</div>
}
The Controller code:
public ActionResult MyRentedMovies()
{
return View();
}
[HttpPost]
public ActionResult MyRentedMovies(int userid)
{
var myrentdetails = rentrep.GetAll().Where(e => e.UserId == userid);
List<Movie> mymovies = new List<Movie>();
foreach (var item in myrentdetails)
{
Movie movie = movierep.FindById(item.MovieId);
mymovies.Add(movie);
}
return View(mymovies);
}
The JS script:
$(function () {
$('#mymovies').click(function () {
//var user = $('#currentuserid').val();
console.log('gethere');
$.post('/useronly/myrentedmovies', $('#frmmymovies').serialize(), function (data) {
if (data) {
console.log(data);
document.location = '/useronly/myrentedmovies';
}
else {
console.log('error');
}
});
});
});