C# 在控制器内部构建模型时,如何将字节[]隐式转换为字符串
我正在使用请求C# 在控制器内部构建模型时,如何将字节[]隐式转换为字符串,c#,sql,asp.net-mvc,linq,C#,Sql,Asp.net Mvc,Linq,我正在使用请求product/index/[Category ID]在索引页上加载属于唯一类别的所有产品 我有一个ProductViewModel类,其中包含用于在两者之间转换类型的隐式方法,还有一个产品实体模型类。将产品实体转换为产品视图模型的隐式方法包含将字节转换为base64字符串的方法,我在控制器中成功地使用该方法来创建新的类别和产品 public class ProductViewModel { public int Id { get; set; }
product/index/[Category ID]
在索引页上加载属于唯一类别的所有产品
我有一个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;
}
}
但是,当传递一个模型,该模型包含要在products视图上显示的属于唯一类别ID的所有产品时,我无法将字节隐式转换为字符串。我使用的替代方法不被接受,错误如下:
LINQ to Entities无法识别方法“System.String Format(System.String,System.Object)”方法,并且无法将此方法转换为存储表达式
控制器中的型号
如下所示:
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);
@model List<IEnumerable<ValueVille.Models.ProductViewModel>>
给出视图的模型
类型I如下所示:
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);
@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());
我最初假设您仍然可以将byte[]分配给string属性,以便在setter中有byte[]值,并在setter中从byte转换为string。如果属性是字符串,则分配给该属性的值必须已经是字符串
属性赋值不是一种转换,所以只要您尝试直接将x.Image(一个字节数组)赋值给OutputImage(一个字符串),它就永远不会工作
您可以将Image属性保留为字节数组,并使用ImageHelper,如
您将在模型中保留一个字节数组:
Image=x.Image
所以你要把这个字节数组传递给这个助手