0

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>&nbsp;&nbsp;<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;
        }
    }
    }
    }
Community
  • 1
  • 1
Dumber_Texan2
  • 840
  • 2
  • 12
  • 34
  • Rather than assuming, you should look in the web server log to see exactly what was requested. There may also be a sub-status to narrow down why the request failed: [The HTTP status code in IIS 7.0, IIS 7.5, and IIS 8.0](https://support.microsoft.com/en-us/kb/943891). – Andrew Morton Sep 21 '15 at 18:36
  • Good idea @Andrew. I'm new to coding, so I'm not real sure about the best method to diagnose problems. Anyway, here is the error page information from the error logs. Looks like the Requested URL and the Physical Path are different. Requested URL https://sitename:80/PersonalOffers/Index/2763795c-2d7b-462a-a7f6-ff966be83cfe Physical Path D:\home\site\wwwroot\PersonalOffers\Index\2763795c-2d7b-462a-a7f6-ff966be83cfe I'm not sure how to correct this. – Dumber_Texan2 Sep 21 '15 at 20:25
  • They look functionally the same to me. What is the sub-status code? – Andrew Morton Sep 22 '15 at 08:18
  • I've looked all over the LogFiles directory and I can't find anything with a name of Sub-Status. The error is 500.0. Here is some info from the details section. Detailed Error Information: Module ManagedPipelineHandler Notification ExecuteRequestHandler Handler System.Web.Mvc.MvcHandler Error Code 0x00000000 – Dumber_Texan2 Sep 22 '15 at 16:13
  • I most recently tried this code in the Offer.cshtml View. @{ Html.Action("Snippet", "PersonalOffers", new { id = @ViewBag.id }); } I'm able to pass the ViewBag.Id to the page without the Html.Action, but when I add anything to display the Partial View, I get the 500.0 error. I changed the PersonalOffersController to use ViewBag as well. public ActionResult Offer(Guid id) { ViewBag.id = id; return View(); } – Dumber_Texan2 Sep 22 '15 at 16:52
  • I also just tried this in my Offer.cshtml View. @{ Html.RenderPartial("~/Views/PersonalOffers/_Snippet.cshtml", @Model, new ViewDataDictionary(ViewData)); } I'm still getting the same 500.0 error. – Dumber_Texan2 Sep 22 '15 at 17:01
  • Assuming you are using IIS as the web server, you will find the logs somewhere like "C:\inetpub\logs\LogFiles\W3SVC1". The log file will have a header line with something like "#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) sc-status sc-substatus sc-win32-status time-taken". You can select the fields to be logged: [Look at Logging in IIS 7/7.5](http://blogs.technet.com/b/sateesh-arveti/archive/2013/09/13/look-at-logging-in-iis-7-7-5.aspx). There is also a chance there will be more information in the Windows event log (the Application log). – Andrew Morton Sep 22 '15 at 19:11
  • Yes, the app is running on IIS 8. Here is some good info. coming from the event log. "The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character." – Dumber_Texan2 Sep 22 '15 at 22:37

0 Answers0