I have been working on this for a couple of weeks now. I’ve tried displaying my partial view into a view using many different methods (Html.Action, Html.Partial, Html.RenderAction & Html.RenderPartial). I’m getting a 404 error, which to me means that the ID is not getting passed into the controller action. This is just an assumption. The partial view I’m working with can be displayed directly by passing a URL parameter like so. https://siteurl.net/PersonalOffers/Snippet/2763795c-2d7b-462a-a7f6-ff966be83cfe I would like for the _Snippet Partial View to display in the Index page like so. https://siteurl.net/PersonalOffers/Index/2763795c-2d7b-462a-a7f6-ff966be83cfe I believe the answer lies in this post, but I can’t get the correct code combinations to work. How can I pass parameters to a partial view in mvc 4
What code should I use for the View? Here is the code I'm working with. Thanks!
ViewModel (SnippetViewModel.cs)
using SiteName.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SiteName.Web.ViewModels
{
public class SnippetViewModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Terms { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public string Image { get; set; }
public string QrImageCode { get; set; }
public string RedemptionLimit { get; set; }
public List<OfferLocation> OfferLocations { get; set; }
}
}
Partial View (_Snippet.cshtml)
@using Zen.Barcode;
@using Zen.Barcode.Web;
@using Zen.Barcode.Web.Mvc;
@model SiteName.Web.ViewModels.SnippetViewModel
<!doctype html>
<html lang="en">
<head>
<title>Offer Snippet</title>
@Styles.Render("~/Content/css")
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.centerPS {
margin: auto;
width: 20%;
padding: 5px;
}
</style>
</head>
<body>
<div id="content-wrapper" style="padding:5px; font-family: 'Times New Roman', Times, serif; font-size:22px;">
<div class="container">
<div class="page-header" style="text-align:center;"><h1><b>@Model.Name</b></h1></div>
<div class="well" style="text-align:center;">
<img src="@Model.Image" alt="Offer Image" /><br /><br />
<!-- Consumer Summary -->
<div class="row">
<div class="col-md-3 consumer-summary">
<div class="preloader-con">
<span><img src="~/Content/images/loader5.gif" /> Loading Consumer Details ...</span>
</div>
</div>
</div>
<div style="text-align:center; margin-bottom:20px; font-size:18px;"><div style="width:250px; display:inline-block;">@Model.Description</div></div>
<div style="text-align:center; margin-bottom:20px; font-size:18px;"><div style="width:250px; display:inline-block;">@Model.Terms</div></div>
<div style="text-align:center; margin-bottom:20px; font-size:18px;">
<div style="text-align:center;">Valid at:</div>
<div style="font-size: 14px; text-align:center;">
@foreach (var offerLocation in @Model.OfferLocations)
{
<p style="margin:0; padding:0;">@offerLocation.MerchantLocation.CompleteAddress</p><br />
<p style="margin:0; padding:0;">@offerLocation.MerchantLocation.Name</p>
}
</div>
</div>
<div style="clear:both; margin-bottom:20px;"></div>
<div style="text-align:center; font-size:18px;">
<div style="text-align:center;">
<div>Starts on: @Convert.ToDateTime(Model.StartDate).ToString("MM/dd/yyyy hh:mm tt")</div>
<div>Redeem by: @Convert.ToDateTime(Model.EndDate).ToString("MM/dd/yyyy hh:mm tt")</div>
<br />
<div>Redemption Limit: @Model.RedemptionLimit</div>
</div><br />
<div style="text-align:center;">
<img width="130" src="data:image/jpeg;base64,@Model.QrImageCode" />
</div>
</div>
<div style="clear:both; margin-bottom:20px;"></div>
<div style="text-align:center; margin-bottom:20px; font-size:20px;">@Model.Id</div>
<div style="text-align:center;"><h3>Mobile Wallet Pass</h3></div>
<!-- PassSlot widget -->
<div id="pslot-widget-container" class="centerPS" data-passtemplate="https://d.pslot.io/BqRFTzQ7M" data-show="true" data-zoom="50" data-placeholder-name="@Model.Name" data-placeholder-description="@Model.Description" data-placeholder-termsconditions="@Model.Terms" data-placeholder-expirydate="@Convert.ToDateTime(Model.EndDate).ToString("MM/dd/yyyy hh:mm tt")"></div>
<br />
<div style="text-align:center; margin-bottom:20px; font-size:20px;"><b>Powered by</b> <a href="http://www.sitename.com" target="_blank"><img src="~/Content/images/SiteName-blue.png" width="150" alt="SiteName Logo" style="margin-bottom: -6px;" /></a></div>
</div>
<!-- End Well -->
</div>
<!-- End Container -->
</div>
<!-- End Wrapper -->
<!-- PassSlot widget Java Script -->
<script id="pslot-wjs" src="https://www.passslot.com/public/passslot/pslot/widget/widget.js" type="text/javascript"></script>
<!-- Consumer Details Java Script -->
<script src="~/Scripts/SiteNameApp/consumer-details.js" type="text/javascript"></script>
</body>
</html>
Controller (PersonalOffersContoller.cs)
using SiteName.Infrastructure.Services;
using SiteName.Web.ViewModels;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Zen.Barcode;
using Zen.Barcode.Web;
using Zen.Barcode.Web.Mvc;
namespace SiteName.Web.Controllers
{
public class PersonalOffersController : Controller
{
private IOfferService _offerService;
private IOfferLocationService _offerLocationService;
private IUniqueOfferService _uniqueOfferService;
private IMerchantConsumerService _merchantConsumerService;
public PersonalOffersController(IOfferService offerService, IOfferLocationService offerLocationService, IUniqueOfferService uniqueOfferService, IMerchantConsumerService merchantConsumerService)
{
_offerService = offerService;
_offerLocationService = offerLocationService;
_uniqueOfferService = uniqueOfferService;
_merchantConsumerService = merchantConsumerService;
}
// Index view
public ActionResult Index(Guid id)
{
ViewData["id"] = id;
return View();
}
// GET: PublicOffers
public async Task<ActionResult> Snippet(Guid id)
{
var offer = await _offerService.FindAsync(id);
var offerLocations = await _offerLocationService.GetByOfferIdAsync(offer.Id);
var barcodeString = "https://sitename.net/PersonalOffers/Snippet/" + offer.Id.ToString();
CodeQrBarcodeDraw bd = BarcodeDrawFactory.CodeQr;
Image img = bd.Draw(barcodeString, 30, 3);
string limit = "";
if (offer.Limited && offer.RedemptionLimit != 0)
limit = offer.RedemptionLimit.ToString();
else
limit = "Unlimited";
SnippetViewModel model = new SnippetViewModel()
{
Id = offer.Id,
Name = offer.Name,
Description = offer.Description,
Terms = offer.TermsConditions,
OfferLocations = offerLocations.ToList(),
StartDate = offer.StartDate,
EndDate = offer.EndDate,
Image = "https://sitenamestorage.blob.core.windows.net/offers/thumb_" + offer.OfferImage,
RedemptionLimit = limit,
QrImageCode = this.getBase64Code(img)
};
return PartialView("_Snippet", model);
}
private string getBase64Code(Image img)
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, ImageFormat.Jpeg);
byte[] imageBytes = ms.ToArray();
string base64String = Convert.ToBase64String(imageBytes);
//string src = "data:image/jpeg;base64," + base64String;
return base64String;
}
}
}
}