Gre*_*egH 2 c# asp.net-mvc razor
我试图将存储在视图模型中的图像从我的控制器传递到我的剃刀视图。两个[条形码]图像( System.Drawing.Image)定义如下:
ShippingSummaryViewModel shippingSummaryVm = ShippingSummaryViewModel.ToShippingSummaryFromOrder(orders);
shippingSummaryVm.CustIdBarcode = CreateBarcode(shippingSummaryVm.customer_code);
shippingSummaryVm.SalesOrderBarcode = CreateBarcode(shippingSummaryVm.order_id);
Run Code Online (Sandbox Code Playgroud)
在我看来,我希望能够使用标签<img>来引用这些图像,但我不确定要为源添加什么内容,因为@Model.CustIdBarcode由于出现 404 资源未找到错误,将 src 设置为等于不起作用。感谢所有帮助
编辑:在我的剃刀视图中,我正在使用<img src="@Model.SalesOrderBarcode"/>和<img src="@Model.CustIdBarcode" />来尝试显示图像。我的视图模型中的这些字段如下所示:
public Image SalesOrderBarcode { get; set; }
public Image CustIdBarcode { get; set; }
Run Code Online (Sandbox Code Playgroud)
渲染页面时出现的错误是:
GET http://localhost:63957/Orders/System.Drawing.Bitmap 404 (Not Found)
Run Code Online (Sandbox Code Playgroud)
HTML 图像通常链接到服务器上的另一个请求,因此传递System.Drawing.Image并不是最好的方法。现代浏览器可以有内联图像(base64 编码),但最好的选择是在控制器上创建一个可以链接到的不同操作:
public class MyController {
public ActionResult GetCustIdBarCode(Guid code) {
var img = CreateBarcode(code);
// I'm not sure if the using here will work or not. It might work
// to just remove the using block if you have issues.
using (var strm = new MemoryStream()) {
img.Save(strm, "image/png");
return File(strm, "image/png");
}
}
public ActionResult GetSalesBarcode(Guid salesId) {
// Similar to above method
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你看来,你会得到类似的东西:
<img src="@Url.Action("GetCustIdBarCode", new { code = customer_code })" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7371 次 |
| 最近记录: |