简体   繁体   English

当我尝试添加图像时,MVC总是返回null

[英]MVC always return me null when I try to add an image

Hi I have a problem with MVC I need to save an image into a table in my database but always give me an error "ImageFile.get return null" when I try to add the image 嗨,我有一个MVC的问题我需要将图像保存到我的数据库中的表,但总是给我一个错误“ImageFile.get return null”当我尝试添加图像

Here is my code 这是我的代码

My Model 我的模特

public partial class Inventario
{
    public int IdProduct { get; set; }
    [DisplayName("Product")]
    public string Name_Product { get; set; }
    public Nullable<decimal> Price{ get; set; }
    public Nullable<int> Stock{ get; set; }
    [DisplayName("Category")]
    public Nullable<int> IdCategory { get; set; }
    [DisplayName("Upload Image")]
    public string ImagePath { get; set; }


    public HttpPostedFileBase ImageFile { get; set; }
}

My View 我的观点

@using (Html.BeginForm("Create", "AccionesInventarios", FormMethod.Post, new {enctype = "multipart/form-data" }))

<input type="file" name="ImageFile" required>

My Controller 我的控制器

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "IdProduct,Name_Product,Price,Stock,IdCategory,ImagePath")] Inventario inventario)
{
    string fileName = Path.GetFileNameWithoutExtension(inventario.ImageFile.FileName);
    string extension = Path.GetExtension(inventario.ImageFile.FileName);
    fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
    inventario.ImagePath = "~/Image/" + fileName;
    fileName = Path.Combine(Server.MapPath("~/Image/"), fileName);
    inventario.ImageFile.SaveAs(fileName);

    if (ModelState.IsValid)
    {
        db.Inventarios.Add(inventario);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(inventario);
}

You are using the Bind attribute to explicitly limit the properties model binder will map from the posted form data. 您正在使用Bind属性显式限制属性模型绑定器将从发布的表单数据映射。 You did not include the ImageFile property and hence the default model binder did not map it from the posted form data. 您没有包含ImageFile属性,因此默认的模型绑定器没有从发布的表单数据中映射它。

Add it to the Bind Include list and it will work. 将它添加到Bind Include列表中,它将起作用。

public ActionResult Create([Bind(Include = "IdProduct,Name_Product, Price,Stock,
                                            IdCategory,ImageFile")] Inventario inventario)
{  
    // to do : Your existing code
}

A more loosely coupled solution is to create a view model with properties needed for the view and use that. 更松散耦合的解决方案是创建具有视图所需属性的视图模型并使用它。 That is the best way to prevent over posting. 这是防止过度发布的最佳方法。 Also using the entity classes from your data access layer in your views/view layer is not a great idea. 在视图/视图层中使用数据访问层中的实体类也不是一个好主意。 It makes it tightly coupled to those classes. 它使它与这些类紧密耦合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试上传图像 ASP.NET 时,图像是 NULL - Image is NULL when I try to upload image ASP.NET 我的SQL函数有什么问题,当我尝试在C#中检索其值时,它总是返回null? - What is wrong with my SQL function that it always returns null when I try to retrieve its value in C#? 当我尝试在ASP.NET MVC中上传文件时,它显示为null - When I try to upload a file in ASP.NET MVC, it shows as null 我希望“(int)null”返回0 - I want “(int)null” to return me 0 HttpPostedFileBase始终返回null ASP.NET MVC 5 - HttpPostedFileBase always return null ASP.NET MVC 5 Asp.net MVC 2 Request.files [“”]始终返回null - Asp.net MVC 2 Request.files[“”] return null always 无论我尝试使用哪种对象/网站,GetElementById始终返回null - GetElementById always returns null, no matter what object/website i try 当我尝试通过XAML将TextBlocks添加到自定义控件时,ObservableCollection为Null - ObservableCollection is Null when I try to add TextBlocks through XAML to a custom control LINQ to SQL为什么在尝试将新对象添加到数据库时认为我的新对象为空? - Why does LINQ to SQL think think that my new object is null when I try to add it to the database? 当我尝试达到Rectangle时为空值 - Null values when I try to reach Rectangle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM