最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Saving data from view into database in ASP.NET MVC web application problem - Stack Overflow

programmeradmin4浏览0评论

I want to save images in the database in the following way controller:

[HttpPost]
public ActionResult AddMovie(Movie movie, HttpPostedFileBase Image)
{
       if (Image != null)
       {
           using (Stream inputStream = Image.InputStream)
           {
               MemoryStream memoryStream = inputStream as MemoryStream;

               if (memoryStream == null)
               {
                   memoryStream = new MemoryStream();
                   inputStream.CopyTo(memoryStream);
               }
               
               movie.Image = memoryStream.ToArray();
           }

       }
       else 
           movie.Image = null;

       if (ModelState.IsValid)
       {
           obllmovie.AddMovie(movie);
           return RedirectToAction("Index");
       }

       return View();
}

View:

@using (Html.BeginForm("AddMovie", "Movie", FormMethod.Post, new { enctype = "multipart/form-data" }))
        <div class="row">
            @Html.LabelFor(m => m.Image, new { @class = "col-md-2 col-form-label" })
            <div class="col-md-10">
                <input type="file" name="Image">
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-outline-dark" />
            </div>
        </div>

If in the view I don't put any parameter the Image is empty, but if I write the parameters the app throws the following error:

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded.

I want to save images in the database in the following way controller:

[HttpPost]
public ActionResult AddMovie(Movie movie, HttpPostedFileBase Image)
{
       if (Image != null)
       {
           using (Stream inputStream = Image.InputStream)
           {
               MemoryStream memoryStream = inputStream as MemoryStream;

               if (memoryStream == null)
               {
                   memoryStream = new MemoryStream();
                   inputStream.CopyTo(memoryStream);
               }
               
               movie.Image = memoryStream.ToArray();
           }

       }
       else 
           movie.Image = null;

       if (ModelState.IsValid)
       {
           obllmovie.AddMovie(movie);
           return RedirectToAction("Index");
       }

       return View();
}

View:

@using (Html.BeginForm("AddMovie", "Movie", FormMethod.Post, new { enctype = "multipart/form-data" }))
        <div class="row">
            @Html.LabelFor(m => m.Image, new { @class = "col-md-2 col-form-label" })
            <div class="col-md-10">
                <input type="file" name="Image">
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-outline-dark" />
            </div>
        </div>

If in the view I don't put any parameter the Image is empty, but if I write the parameters the app throws the following error:

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded.

Share Improve this question edited Jan 19 at 13:35 marc_s 754k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 19 at 13:30 StefanIulianStefanIulian 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

In database make sure image property have maximum size and try this code.

[HttpPost]
public ActionResult AddMovie(Movie movie, HttpPostedFileBase Image)
{
    try
    {
        if (Image != null && Image.ContentLength > 0)
        {
            // Convert image into byte array
            using (var memoryStream = new MemoryStream())
            {
                Image.InputStream.CopyTo(memoryStream);
                movie.Image = memoryStream.ToArray();
            }
        }

        if (ModelState.IsValid)
        {
            // Save to database
            
            return RedirectToAction("Index");
        }

        // Model fail return
        return View(movie);
    }
    catch (Exception ex)
    {
        // Handle any errors
        return View(movie);
    }
}
//Make a movie model which will have Image property:
public class Movie
{
    // XYZ properties...
    
    public byte[] Image { get; set; }
}

You can visit here for more details

发布评论

评论列表(0)

  1. 暂无评论