这个相当简单LINQ的查询用于根据其ID字段提取记录:
string s = (from i in db.Categories
where i.CategoryID == model.SelectedCategory
select i.Name).ToString();
if (ModelState.IsValid && model.ImageUpload.ContentLength > 0)
{
string InitialPath = string.Format("/Images/Products/" + s + "/");
var PathWithFileName = Path.Combine(InitialPath, model.ImageUpload.FileName);
模型:
public class ItemVM
{
public int? ID { get; set; }
[Display(Name ="Category")]
[Required(ErrorMessage ="Please select a category")]
public int? SelectedCategory { get; set; }
[Display(Name = "Brand")]
[Required(ErrorMessage = "Please select a brand")]
public int? SelectedBrand { get; set; }
[Display(Name = "Product name")]
[Required(ErrorMessage = "Please enter the product name")]
public string ItemName { get; set; }
[Display(Name = "Price")]
[Required(ErrorMessage = "Please enter the price")]
[Range(1, Int32.MaxValue, ErrorMessage = "Value should be greater than or equal to 1")]
public decimal? ItemPrice { get; set; }
[Display(Name = "Image Upload"), Required(ErrorMessage = "Product Image must be added.")]
[NotMapped]
[DataType(DataType.Upload)]
public HttpPostedFileBase ImageUpload { get; set; }
public IEnumerable<SelectListItem> CategoryOptions { get; set; }
public IEnumerable<SelectListItem> BrandOptions { get; set; }
}
我需要使用s(字符串对象)来命名文件夹。不幸的是,这个查询没有返回一个字符串,我在最后一行代码中遇到错误: Illegal characters in path.
有人可以请指导。
翻阅古今
相关分类