Asp.net mvc 如何在Asp.NETMVC上处理多个图像上的编辑帖子

Asp.net mvc 如何在Asp.NETMVC上处理多个图像上的编辑帖子,asp.net-mvc,Asp.net Mvc,大家好,第一次来这里问。这里是我在多个图像和其他数据项上的createpost方法,它可以解决任何问题,并上传所有图像和内容。我的问题是如何在多个图像上处理edit Post controller 这是创建控制器的步骤 public ActionResult SaveDataAdded([Bind(Include = "SId,Category,SName,LocalName,CommonName,Description,PicTakenBy,ContentBy,EditedBy")

大家好,第一次来这里问。这里是我在多个图像和其他数据项上的createpost方法,它可以解决任何问题,并上传所有图像和内容。我的问题是如何在多个图像上处理edit Post controller

这是创建控制器的步骤

      public ActionResult SaveDataAdded([Bind(Include = "SId,Category,SName,LocalName,CommonName,Description,PicTakenBy,ContentBy,EditedBy")]SpeciesDataTable ARow, HttpPostedFileBase file,HttpPostedFileBase file2, HttpPostedFileBase file3, HttpPostedFileBase file4)
    {if (ModelState.IsValid)

        {

            if (file != null && file.ContentLength > 0)
            {
              using (var Bnryreader = new System.IO.BinaryReader(file.InputStream))
                {
                    ARow.MainPic = Bnryreader.ReadBytes(file.ContentLength);
                }

            } 

            if (file2 != null && file2.ContentLength > 0)
            {
                using (var reader = new System.IO.BinaryReader(file2.InputStream))
                {
                    ARow.SecPic = reader.ReadBytes(file2.ContentLength);
                }

            }
            if (file3 != null && file3.ContentLength > 0)
            {
                using (var reader = new System.IO.BinaryReader(file3.InputStream))
                {
                    ARow.ThirdPic = reader.ReadBytes(file3.ContentLength);
                }

            }
            if (file4 != null && file4.ContentLength > 0)
            {
                using (var reader = new System.IO.BinaryReader(file4.InputStream))
                {
                    ARow.FourthPic = reader.ReadBytes(file4.ContentLength);
                }

            }


            db.SpeciesDataTables.Add(ARow);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(ARow);

    }
这是用于编辑的代码块。可能我的方法不正确。但我需要帮助

    public ActionResult EditSpeciesPost([Bind(Include = "SId,Category,SName,LocalName,CommonName,Description,PicTakenBy,ContentBy,EditedBy")]SpeciesDataTable Editor, HttpPostedFileBase file, HttpPostedFileBase file2, HttpPostedFileBase file3, HttpPostedFileBase file4)
    {

            if (file != null && file.ContentLength > 0)
            {
                using (var reader = new System.IO.BinaryReader(file.InputStream))
                {
                    Editor.MainPic = reader.ReadBytes(file.ContentLength);
                }


            }

            if (file2 != null && file2.ContentLength > 0)
            {

                using (var reader = new System.IO.BinaryReader(file2.InputStream))
                {
                    Editor.SecPic = reader.ReadBytes(file2.ContentLength);
                }



            }

            if (file3 != null && file3.ContentLength > 0)
            {
                using (var reader = new System.IO.BinaryReader(file3.InputStream))
                {
                    Editor.ThirdPic = reader.ReadBytes(file3.ContentLength);
                }

            }
            if (file4 != null && file4.ContentLength > 0)
            {
                using (var reader = new System.IO.BinaryReader(file4.InputStream))
                {
                    Editor.FourthPic = reader.ReadBytes(file4.ContentLength);
                }

            }


        db.Entry(Editor).State = EntityState.Modified;
        db.SaveChanges();

          //return RedirectToAction("Index");

           return View(Editor);

    }      
这是要编辑的视图

@使用(Html.BeginForm(“EditSpecies”,“Species”,null,FormMethod.Post,new{enctype=“multipart/form data”}))
{ 
@Html.AntiForgeryToken()
@如果(Model.MainPic!=null)//要查看第一个图像
{
var base64=Convert.ToBase64String(Model.MainPic);
var imgsrc=string.Format(“数据:image/jpg;base64,{0}”,base64);
}
//第一个图像的输入
@如果(Model.SecPic!=null)//要查看第二个图像
{
var base64=Convert.ToBase64String(Model.SecPic);
var imgsrc=string.Format(“数据:image/jpg;base64,{0}”,base64);
}
第三图像的输入
//接着是第三张和第四张图片

}
您应该做的是,仅为用户未在UI中更新的图像发送null,并将数据发布到httppost操作方法,在该方法中读取现有实体并仅更新需要更新的属性

我将使用视图所需的属性为视图创建视图模型

public class SpeciesVm
{
    public int Id { set; get; }
    public string Name { set; get; }
    public string LocalName { set; get; }

    public HttpPostedFileBase MainImg { set; get; }
    public HttpPostedFileBase SecondImg { set; get; }
    public string MainPicImgSrc { set; get; }
    public string SecondPicImgSrc { set; get; }
}
现在,在GET操作中,创建一个对象,加载属性值

public ActionResult Edit(int id)
{
   var e=db.SpeciesDataTables.Find(id);
   var vm = new SpeciesVm() { Id=id , Name =e.SName };
   vm.LocalName= e.LocalName;

   if(e.MainPic!=null)
   {
     vm.MainPicImgSrc = $"data:image/jpg;base64,{Convert.ToBase64String(e.MainPic)}";
   }
   if(e.SecPic!=null)
   {
     vm.SecondPicImgSrc = $"data:image/jpg;base64,{Convert.ToBase64String(e.SecPic)}";
   }
   return View(vm);
}
现在在您的视图中,您将使用类型为
HttpPostedFileBase
的属性进行文件输入

@model SpeciesVm
@using (Html.BeginForm("Edit","Species", FormMethod.Post, 
                                         new { enctype = "multipart/form-data" }))
{
  @Html.HiddenFor(a=>a.Id)
  @Html.HiddenFor(a=>a.Name)
  @Html.HiddenFor(a=>a.LocalName)

  <div class="col-md-10">
    @if (!string.IsNullOrEmpty(Model.MainPicImgSrc)) 
    {
      <img src='@Model.MainPicImgSrc'  />
    }
    <input type="file" name="MainImg" />
  </div>
  <div class="col-md-10">
    @if (!string.IsNullOrEmpty(Model.SecondPicImgSrc)) 
    {
      <img src='@Model.SecondPicImgSrc'  />
    }
    <input type="file" name="SecondImg" />
  </div>
  <button type="submit">Save</button>
}

您应该做的是,仅为用户未在UI中更新的图像发送null,并将数据发布到httppost操作方法,在该方法中读取现有实体并仅更新需要更新的属性

我将使用视图所需的属性为视图创建视图模型

public class SpeciesVm
{
    public int Id { set; get; }
    public string Name { set; get; }
    public string LocalName { set; get; }

    public HttpPostedFileBase MainImg { set; get; }
    public HttpPostedFileBase SecondImg { set; get; }
    public string MainPicImgSrc { set; get; }
    public string SecondPicImgSrc { set; get; }
}
现在,在GET操作中,创建一个对象,加载属性值

public ActionResult Edit(int id)
{
   var e=db.SpeciesDataTables.Find(id);
   var vm = new SpeciesVm() { Id=id , Name =e.SName };
   vm.LocalName= e.LocalName;

   if(e.MainPic!=null)
   {
     vm.MainPicImgSrc = $"data:image/jpg;base64,{Convert.ToBase64String(e.MainPic)}";
   }
   if(e.SecPic!=null)
   {
     vm.SecondPicImgSrc = $"data:image/jpg;base64,{Convert.ToBase64String(e.SecPic)}";
   }
   return View(vm);
}
现在在您的视图中,您将使用类型为
HttpPostedFileBase
的属性进行文件输入

@model SpeciesVm
@using (Html.BeginForm("Edit","Species", FormMethod.Post, 
                                         new { enctype = "multipart/form-data" }))
{
  @Html.HiddenFor(a=>a.Id)
  @Html.HiddenFor(a=>a.Name)
  @Html.HiddenFor(a=>a.LocalName)

  <div class="col-md-10">
    @if (!string.IsNullOrEmpty(Model.MainPicImgSrc)) 
    {
      <img src='@Model.MainPicImgSrc'  />
    }
    <input type="file" name="MainImg" />
  </div>
  <div class="col-md-10">
    @if (!string.IsNullOrEmpty(Model.SecondPicImgSrc)) 
    {
      <img src='@Model.SecondPicImgSrc'  />
    }
    <input type="file" name="SecondImg" />
  </div>
  <button type="submit">Save</button>
}

你说的“如何处理”是什么意思?是的!显然,在不破坏其他映像的情况下更新一个映像将其他映像设为null(file2、file2、file4),并在更新之前进行null检查。如果它来自于客户端为空,请不要更新Hey Shyju您能详细说明一下吗?您是如何呈现您的视图的?您的意思是“如何处理”?是的!显然,在不破坏其他映像的情况下更新一个映像将其他映像设为null(file2、file2、file4),并在更新之前进行null检查。如果它是来自客户端的null,请不要更新Hey Shyju您能详细说明一下吗?您是如何呈现您的视图的?很抱歉回复太晚,但它确实起到了作用。谢谢你所有的快速回答。很抱歉迟了回复,但它成功了。谢谢你的快速回答。