Asp.net mvc 5 尝试使用MVC 5和ASP.net 4.5将部分视图加载到视图时出现404错误
我已经为此工作了几个星期了。我尝试过使用许多不同的方法(Html.Action、Html.partial、Html.RenderAction和Html.RenderPartial)将部分视图显示到视图中。我得到一个404错误,这对我来说意味着ID没有被传递到控制器操作中。这只是一个假设。我正在使用的部分视图可以通过如下方式传递URL参数直接显示。我希望_Snippet部分视图能够像这样显示在索引页面中。我相信答案就在这篇文章中,但我无法得到正确的代码组合 视图应该使用什么代码?这是我正在使用的代码。谢谢 ViewModel(SnippetViewModel.cs)Asp.net mvc 5 尝试使用MVC 5和ASP.net 4.5将部分视图加载到视图时出现404错误,asp.net-mvc-5,partial-views,Asp.net Mvc 5,Partial Views,我已经为此工作了几个星期了。我尝试过使用许多不同的方法(Html.Action、Html.partial、Html.RenderAction和Html.RenderPartial)将部分视图显示到视图中。我得到一个404错误,这对我来说意味着ID没有被传递到控制器操作中。这只是一个假设。我正在使用的部分视图可以通过如下方式传递URL参数直接显示。我希望_Snippet部分视图能够像这样显示在索引页面中。我相信答案就在这篇文章中,但我无法得到正确的代码组合 视图应该使用什么代码?这是我正在使用的代
使用SiteName.Entities;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
命名空间SiteName.Web.ViewModels
{
公共类视图模型
{
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共字符串项{get;set;}
公共日期时间?开始日期{get;set;}
公共日期时间?结束日期{get;set;}
公共字符串图像{get;set;}
公共字符串QrImageCode{get;set;}
公共字符串赎回限制{get;set;}
位置{get;set;}的公共列表
}
}
部分视图(_Snippet.cshtml)
@使用Zen.Barcode;
@使用Zen.Barcode.Web;
@使用Zen.Barcode.Web.Mvc;
@模型SiteName.Web.ViewModels.SnippetViewModel
报价片段
@style.Render(“~/Content/css”)
.centerPS{
保证金:自动;
宽度:20%;
填充物:5px;
}
@型号.名称
正在加载消费者详细信息。。。
@型号.说明
@模型.术语
有效日期:
@foreach(var offerLocation in@Model.OfferLocations)
{
@offerLocation.MerchantLocation.completedAddress
@offerLocation.MerchantLocation.Name
}
开始日期:@Convert.ToDateTime(Model.StartDate).ToString(“MM/dd/yyyy hh:MM tt”)
兑换人:@Convert.ToDateTime(Model.EndDate).ToString(“MM/dd/yyyy hh:MM tt”)
赎回限额:@Model.RedemptionLimit
@Model.Id
移动钱包通行证
技术支持
控制器(PersonalOffersController.cs)
使用SiteName.Infrastructure.Services;
使用SiteName.Web.ViewModels;
使用制度;
使用System.Collections.Generic;
使用系统图;
使用系统、绘图、成像;
使用System.IO;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Web;
使用System.Web.Mvc;
使用Zen.条形码;
使用Zen.Barcode.Web;
使用Zen.Barcode.Web.Mvc;
命名空间SiteName.Web.Controllers
{
公共类PersonalOffers控制器:控制器
{
私人IOfferService(私人IOfferService);
私人IOfferLocationService(私人IOfferLocationService);
私人IUniqueOfferService(唯一服务);;
私人IMerchantConsumerService(商品消费者服务);;
公共个人报价控制器(IOfferService OFFERSSERVICE、IOfferLocationService OFFERLLOCATIONSERVICE、IUniqueOfferService uniqueOfferService、IMerchantConsumerService merchantConsumerService)
{
_offerService=offerService;
_offerLocationService=offerLocationService;
_uniqueOfferService=uniqueOfferService;
_merchantConsumerService=merchantConsumerService;
}
//索引视图
公共操作结果索引(Guid id)
{
ViewData[“id”]=id;
返回视图();
}
//获取:公开报价
公共异步任务段(Guid id)
{
var offer=wait _offerService.FindAsync(id);
var offerLocations=wait _offerLocationService.GetByOfferIdAsync(offer.Id);
var barcodeString=”https://sitename.net/PersonalOffers/Snippet/“+offer.Id.ToString();
CodeQrBarcodeDraw bd=BarcodeDrawFactory.CodeQr;
图像img=bd.Draw(条形码字符串,30,3);
字符串限制=”;
if(offer.Limited&&offer.RedemptionLimit!=0)
limit=offer.RedemptionLimit.ToString();
其他的
limit=“无限”;
SnippetViewModel=新SnippetViewModel()
{
Id=offer.Id,
Name=offer.Name,
描述=报价。描述,
条款=报价。条款条件,
OfferLocations=OfferLocations.ToList(),
StartDate=报价。StartDate,
EndDate=offer.EndDate,
图像=”https://sitenamestorage.blob.core.windows.net/offers/thumb_“+offer.OfferImage,
赎回限额=限额,
QrImageCode=this.getBase64Code(img)
};
返回PartialView(“\u Snippet”,model);
}
私有字符串getBase64Code(图像img)
{
使用(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;
返回base64String;
}
}
}
}
您应该查看web服务器日志,准确查看请求的内容,而不是假设。也可能有b
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; }
}
}
@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>
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;
}
}
}
}