c#MVC在剃刀视图中显示来自viewmodel的图像

时间:2016-05-02 16:15:13

标签: c# asp.net-mvc razor

我正在尝试将存储在viewmodel中的图像从我的控制器传递到我的剃刀视图。两个[条形码]图像(System.Drawing.Image)定义如下:

ShippingSummaryViewModel shippingSummaryVm = ShippingSummaryViewModel.ToShippingSummaryFromOrder(orders);
shippingSummaryVm.CustIdBarcode = CreateBarcode(shippingSummaryVm.customer_code);
shippingSummaryVm.SalesOrderBarcode = CreateBarcode(shippingSummaryVm.order_id);

在我看来,我希望能够使用<img>标记来引用这些图像,但我不确定要为源设置什么,因为将src设置为等于@Model.CustIdBarcode不会由于获得404资源未找到错误而导致工作。感谢所有帮助

编辑:在我的剃刀视图中,我使用<img src="@Model.SalesOrderBarcode"/><img src="@Model.CustIdBarcode" />来尝试显示图像。我的viewmodel中的这些字段如下所示:

public Image SalesOrderBarcode { get; set; }
public Image CustIdBarcode { get; set; }

以及渲染页面时出现的错误是:

GET http://localhost:63957/Orders/System.Drawing.Bitmap 404 (Not Found)

3 个答案:

答案 0 :(得分:3)

HTML图像通常链接到服务器上的另一个请求,因此传递numpy.transpose(numpy.transpose(rect_array) * latcos) 不是最好的方法。现代浏览器可以使用内嵌图像(base64编码),但最好的办法是在控制器上创建一个可以链接到的不同操作:

System.Drawing.Image

然后在你看来,你有类似的东西:

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
  }
}

https://stackoverflow.com/a/11546957/179311

答案 1 :(得分:1)

在你的视图中尝试这样

<img src= "@Url.Content(Model.ImagePath)" alt="Image" />

答案 2 :(得分:0)

请尝试以下语法:

POST
相关问题