C# Img路径是必需的。ASP.NET MVC中使用EF时出现错误

C# Img路径是必需的。ASP.NET MVC中使用EF时出现错误,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我正在插入产品、图像和产品细节,一切正常,但当我插入图像时,我会出错。当我使用额外表格图像时,ImagePath未插入,则会发生此错误。如果我在产品中使用imgpath列,则它正常工作,我使用额外表格,因为我要插入多个图像,这就是我使用额外表格的原因 错误是: imgPath是必需的 当我使用断点时,我可以看到图像,然后光标将到达保存函数,我不知道图像对象中的值 有专家能告诉我哪里错了吗?如果你有任何简单的解决方法,请指导我 图片: public partial class Image {

我正在插入产品、图像和产品细节,一切正常,但当我插入图像时,我会出错。当我使用额外表格图像时,ImagePath未插入,则会发生此错误。如果我在产品中使用imgpath列,则它正常工作,我使用额外表格,因为我要插入多个图像,这就是我使用额外表格的原因

错误是:

imgPath是必需的

当我使用断点时,我可以看到图像,然后光标将到达保存函数,我不知道图像对象中的值

有专家能告诉我哪里错了吗?如果你有任何简单的解决方法,请指导我

图片:

public partial class Image
{
    public int imgID { get; set; }
    public string ImgName { get; set; }
    public bigint prodid{ get; set; }
}
产品型号:

public partial class Product
{
    public long ProductID { get; set; }
    public string PName { get; set; }
    public string PDescription { get; set; }
}
产品主信息:

public partial class MasterDetail
{
    public int mDID { get; set; }
    public long ProductID { get; set; }
    public string OS { get; set; }
    public string SimType { get; set; }
    public string Other { get; set; }
}
产品界面:

public interface IProduct
{
    void Add_NewProduct(Product prod, Specification spec, Image pic);
}
产品实施:

public void Add_NewProduct(Product prod, Specification spec, Image pic)
{
    try
    {
        _db.Products.Add(prod);
        _db.Specifications.Add(spec);
        _db.Images.Add(pic);

        _db.SaveChanges();
    }
    catch (Exception)
    {
        throw;
    }
}
产品控制员:

[HttpPost]
public ActionResult AddNewProducts(ProductViewModel prod, List<HttpPostedFileBase> file)
{
    try
    {
        List<ProductType> PTlist = _IproductType.PTList();
        ViewBag.Ptlist = new SelectList(PTlist, "PType_ID", "P_Name");

        // Product Color List
        List<P_Color> pColorList = _IProductColor.PColorlist();
        ViewBag.pColor_List = new SelectList(pColorList, "C_ID", "C_Name_OR_Code");

        List<P_Size> pSizeList = _ISize.pSizeList();
        ViewBag.pSizeLists = new SelectList(pSizeList, "S_ID", "S_Size");

        string PathDB = string.Empty;

        foreach (HttpPostedFileBase files in file)
        {
            string filename = Path.GetFileName(files.FileName);
            string _filename = DateTime.Now.ToString("yymmssff") + filename;
            string extension = Path.GetExtension(files.FileName);
            string path = Path.Combine(Server.MapPath("~/Upload/"), _filename);

            PathDB = "~/Upload/" + _filename;

            if (extension.ToLower() == ".jpeg" || extension.ToLower() == ".jpg" || extension.ToLower() == ".png")
            {
                if (files.ContentLength <= 1000000)
                {
                    img = new Image();
                    prod.ImgID = img.imgID;
                    prod.ImgPath = PathDB;
                    prod.ProductID = img.ProdID;
                }
                else
                    ViewBag.sizemsg = "Size Limit accessed ";
            }
            else
                ViewBag.fileformat = "File is not Format is not Correct";
        }

        Product pro = new Product();
        pro.ProductID = prod.ProductID;
        pro.PName = prod.PName;
        pro.PDescription = prod.PDescription;

        Specification p_spec = new Specification();
        p_spec.PSpecificationID = prod.PSpecificationID;
        p_spec.ProductID = prod.ProductID;
        p_spec.OS = prod.OS;
        p_spec.DualSim = prod.DualSim;
        p_spec.TouchScreen = prod.TouchScreen;
        p_spec.Other = prod.Other;

        _IProducts.Add_NewProduct(pro, p_spec, img);

        ViewBag.message = "Record Saved Successfully";
        ModelState.Clear();
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return RedirectToAction("ProductsList");
}
[HttpPost]
公共操作结果AddNewProducts(ProductViewModel产品,列表文件)
{
尝试
{
List PTlist=_IproductType.PTlist();
Ptlist=新的选择列表(Ptlist,“PType_ID”,“P_Name”);
//产品颜色表
List pColorList=\u IPProductColor.pColorList();
ViewBag.pColor_List=新选择列表(pColorList,“C_ID”、“C_Name_或_Code”);
List pSizeList=_ISize.pSizeList();
ViewBag.pSizeLists=新的选择列表(pSizeList,“S_ID”,“S_Size”);
string PathDB=string.Empty;
foreach(文件中的HttpPostedFileBase文件)
{
字符串文件名=Path.GetFileName(files.filename);
字符串_filename=DateTime.Now.ToString(“yymmssff”)+文件名;
字符串扩展名=Path.GetExtension(files.FileName);
字符串路径=path.Combine(Server.MapPath(“~/Upload/”),文件名);
PathDB=“~/Upload/”+\u文件名;
if(extension.ToLower()==”.jpeg“| | extension.ToLower()==”.jpg“| | extension.ToLower()==”.png”)
{
if(files.ContentLength)

第一:您在哪里创建了
img
变量?第二:您正在将
img
赋值给一个图像。但仅此而已。因此,
addNewProduct
方法总是为空。您能试着设置
img
的值并再次保存产品吗?第一:您在哪里创建了
img
variable?其次:您正在将
img
分配给图像。但仅此而已。因此,
addNewProduct
方法的值总是为空。您可以尝试设置
img
的值并再次保存产品吗?
@using (Html.BeginForm("AddNewProducts", "Items", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal" }))
            {    <div id="tabimage" class="tab-pane">
                                <h3 class="mgtp-15 mgbt-xs-20"> Images</h3>

                                    <div class="vd_panel-menu">
                                        <div> <a class="btn vd_btn vd_bg-blue btn-sm save-btn"><i class="fa fa-save append-icon"></i> Save Changes</a> <a class="btn btn-default btn-sm" data-toggle="modal" data-target="#myModal"><i class="fa fa-times append-icon"></i> Cancel Changes</a></div>
                                    </div>

                                    <div class="form-group">
                                        <label class="control-label col-lg-3 file_upload_label"> <span title="" data-toggle="tooltip" class="label-tooltip" data-original-title="Format JPG, GIF, PNG. Filesize 8.00 MB max."> Add a new image to this product </span> </label>
                                        <div class="col-lg-9">
                                            <span class="btn vd_btn vd_bg-green fileinput-button">
                                                <i class="glyphicon glyphicon-plus"></i> <span>Add files...</span>
                                                <!-- The file input field used as target for the file upload widget -->
                                                <input type="file" name="file" required="required" multiple id="file" />

                                            </span> 
                                        </div>
                                    </div>
                            </div>   }