Asp.net 如何从多个选择中仅上载第一个图像?

Asp.net 如何从多个选择中仅上载第一个图像?,asp.net,asp.net-mvc,asp.net-mvc-5,Asp.net,Asp.net Mvc,Asp.net Mvc 5,我有这个ActionResult它做什么它上传多个图像到数据库。但我需要另一个操作,将只上传第一张图片到另一个表 [HttpPost] public ActionResult Add(Ads ads, IEnumerable<HttpPostedFileBase> images) { //Ensure model state is valid if (ModelState.IsValid) {

我有这个ActionResult它做什么它上传多个图像到数据库。但我需要另一个操作,将只上传第一张图片到另一个表

[HttpPost]
    public ActionResult Add(Ads ads, IEnumerable<HttpPostedFileBase> images)
    {

        //Ensure model state is valid  
        if (ModelState.IsValid)
        {
          if(images != null) { 
                var imageList = new List<AdsImage>();
                foreach (var image in images)
                {
                    using (var br = new BinaryReader(image.InputStream))
                    {
                        var data = br.ReadBytes(image.ContentLength);
                        var img = new AdsImage { Id = ads.Id };
                        img.ImageData = data;
                    }
                }
                ads.AdsImage = imageList;
          }
[HttpPost]
公共行动结果添加(广告、IEnumerable图像)
{
//确保模型状态有效
if(ModelState.IsValid)
{
如果(图像!=null){
var imageList=新列表();
foreach(图像中的var图像)
{
使用(var br=新的二进制读取器(image.InputStream))
{
var data=br.ReadBytes(image.ContentLength);
var img=新的AdsImage{Id=ads.Id};
img.ImageData=数据;
}
}
ads.AdsImage=imageList;
}
例如,如果我选择照片1和照片2,只有1个会上传到数据库


感谢您的帮助。谢谢。

使用以下代码,只上传第一张图片

[HttpPost]
public ActionResult Add(Ads ads, IEnumerable<HttpPostedFileBase> images)
    {

        //Ensure model state is valid  
        if (ModelState.IsValid)
        {
            if (images != null)
            {
                var imageList = new List<AdsImage>();
                var image = images.First();


                    using (var br = new BinaryReader(image.InputStream))
                    {
                        var data = br.ReadBytes(image.ContentLength);
                        var img = new AdsImage { Id = ads.Id };
                        img.ImageData = data;
                    }
                
                ads.AdsImage = imageList;
            }
        }   
    }
[HttpPost]
公共行动结果添加(广告、IEnumerable图像)
{
//确保模型状态有效
if(ModelState.IsValid)
{
如果(图像!=null)
{
var imageList=新列表();
var image=images.First();
使用(var br=新的二进制读取器(image.InputStream))
{
var data=br.ReadBytes(image.ContentLength);
var img=新的AdsImage{Id=ads.Id};
img.ImageData=数据;
}
ads.AdsImage=imageList;
}
}   
}

使用以下代码,只上传第一张图像

[HttpPost]
public ActionResult Add(Ads ads, IEnumerable<HttpPostedFileBase> images)
    {

        //Ensure model state is valid  
        if (ModelState.IsValid)
        {
            if (images != null)
            {
                var imageList = new List<AdsImage>();
                var image = images.First();


                    using (var br = new BinaryReader(image.InputStream))
                    {
                        var data = br.ReadBytes(image.ContentLength);
                        var img = new AdsImage { Id = ads.Id };
                        img.ImageData = data;
                    }
                
                ads.AdsImage = imageList;
            }
        }   
    }
[HttpPost]
公共行动结果添加(广告、IEnumerable图像)
{
//确保模型状态有效
if(ModelState.IsValid)
{
如果(图像!=null)
{
var imageList=新列表();
var image=images.First();
使用(var br=新的二进制读取器(image.InputStream))
{
var data=br.ReadBytes(image.ContentLength);
var img=新的AdsImage{Id=ads.Id};
img.ImageData=数据;
}
ads.AdsImage=imageList;
}
}   
}