簡體   English   中英

在控制器內部構建模型時,如何將Byte []隱式轉換為字符串

[英]How to implicitly convert Byte[] to string when building model inside the controller

我正在使用請求product/index/[Category ID]將所有屬於唯一類別的產品加載到索引頁面上。

我有一個ProductViewModel類,其中包含隱式方法以在兩者之間轉換類型,還包含Product實體模型類。 Product實體轉換為ProductViewModel的隱式方法包含將字節轉換為base64字符串的方法,我在控制器中成功使用此方法來創建新的類別和產品。

public class ProductViewModel
    {
        public int Id { get; set; }
        [Required, Display(Name="Product Name")]
        public string Name { get; set; }
        [Required, DataType(DataType.Upload)]
        public HttpPostedFileBase Image { get; set; }
        public string OutputImage { get; set; }
        [Required]
        public Decimal Price { get; set; }

        public static byte[] ConvertToByte(ProductViewModel model)
        {
            if (model.Image != null)
            {
                byte[] imageByte = null;
                BinaryReader rdr = new BinaryReader(model.Image.InputStream);
                imageByte = rdr.ReadBytes((int)model.Image.ContentLength);

                return imageByte;
            }

            return null;
        }

        // ViewModel => Model | Implicit type Operator
        public static implicit operator Product(ProductViewModel viewModel)
        {
            var model = new Product
            {
                Id = viewModel.Id,
                Name = viewModel.Name,
                Image = ConvertToByte(viewModel),
                Price = viewModel.Price
            };

            return model;
        }

        // Model => ViewModel | Implicit type Operator
        public static implicit operator ProductViewModel(Product model)
        {
            var viewModel = new ProductViewModel
            {
                Id = model.Id,
                Name = model.Name,
                OutputImage = string.Format("data:image/jpg;base64,{0}", Convert.ToBase64String(model.Image)),
                Price = model.Price
            };

            return viewModel;
        }

    }

但是,當傳遞包含所有屬於唯一類別ID的所有產品的模型以顯示在產品View ,我無法將字節隱式轉換為字符串。 錯誤不接受我用作替代方法:

LINQ to Entities無法識別方法'System.String Format(System.String,System.Object)',並且該方法無法轉換為商店表達式。

ControllerModel如下:

var products = (await db.Categories.Where(c => c.Id == id).Select(p => p.Products.Select(x => new 
    ProductViewModel { Id = x.Id, Name = x.Name, OutputImage = (string.Format("data:image/jpg;base64,{0}", Convert.ToBase64String(x.Image))), Price = x.Price})).ToListAsync());
return View(products);

我為View提供的Model類型如下:

@model List<IEnumerable<ValueVille.Models.ProductViewModel>>

您不能在LINQ表達式中使用string.Format() ,而可以在Name setter中使用它:

public class ProductViewModel {

    public string Name{
      get
      {
         return this.Name;
      }
      set{
           this.Name = value;
           this.OutputImage  = string.Format("data:image/jpg;base64,{0}", Convert.ToBase64String(value))
      }
    }
}

並在控制器中:

var products = (await db.Categories.Where(c => c.Id == id).Select(p => p.Products.Select(x => new 
                    ProductViewModel { Id = x.Id, Name = x.Name, Convert.ToBase64String(x.Image))), Price = x.Price})).ToListAsync());

最初,我假設您仍可以將string []分配給字符串屬性,以在setter中具有byte []值,並在setter中將字節轉換為字符串。 如果屬性是字符串,則分配給該屬性的值必須已經是字符串。

屬性分配不是轉換,因此只要您嘗試直接將字節數組x.Image分配給字符串OutputImage,它就永遠不會起作用。

您可以將Image屬性保留為字節數組,並擁有一個ImageHelper,例如http://www.itorian.com/2012/10/html-helper-for-image-htmlimage.html

您將在模型中保留一個字節數組:

圖片= x圖片

因此,您可以將此字節數組傳遞給此幫助器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM