Asp.net mvc 如何在ActionResult asp.net mvc中上载图像?

Asp.net mvc 如何在ActionResult asp.net mvc中上载图像?,asp.net-mvc,Asp.net Mvc,我想将图像设置为我的帖子,但我不能。所有模型字段都是填充的,但图像字段为空 我的模型课: public class Game { public Game() { Platforms = new List<Platform>(); } [Key] public int GameID { get; set; } public string Name { get; set; } public string I

我想将图像设置为我的帖子,但我不能。所有模型字段都是填充的,但图像字段为空

我的模型课:

     public class Game
{
    public Game()
    {
        Platforms = new List<Platform>();
    }
    [Key]
    public int GameID { get; set; }
    public string Name { get; set; }
    public string Image { get; set; }
    [AllowHtml]
    public string Description { get; set; }
    public DateTime PublishDate { get; set; }
    public virtual ICollection<Platform> Platforms { get; set; }

}
公共类游戏
{
公共游戏()
{
平台=新列表();
}
[关键]
public int GameID{get;set;}
公共字符串名称{get;set;}
公共字符串图像{get;set;}
[allowtml]
公共字符串说明{get;set;}
public DateTime PublishDate{get;set;}
公共虚拟ICollection平台{get;set;}
}
我的控制器类:

public ActionResult Create_post(Game model,int[] platformsIds)
    {
        if (ModelState.IsValid)
        {
            foreach (var pId in platformsIds)
            {
                var platform = _repository.Get<Platform>(pId);
                model.Platforms.Add(platform);
            }

            _repository.Add<Game>(model);
            return RedirectToAction("Index");
        }
        ViewBag.Platforms = _repository.GetAll<Platform>().ToList();
        ViewBag.HtmlContent = model.Description;
        return View(model);
    }
public ActionResult创建_post(游戏模型,int[]平台SID)
{
if(ModelState.IsValid)
{
foreach(平台SID中的var pId)
{
var platform=\u repository.Get(pId);
model.Platforms.Add(平台);
}
_添加(模型);
返回操作(“索引”);
}
ViewBag.Platforms=_repository.GetAll().ToList();
ViewBag.HtmlContent=模型.描述;
返回视图(模型);
}

谢谢

您只需在保存模型之前检查图像url即可

在您看来,您必须拥有此代码

 <div class="form-group">
        @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-1" })
        <div class="col-md-4">
                <div class="col-md-6 col-md-push-0">
                    <input type="file" class="form-control" name="ImageUrl" id="ImageUrl" />
                </div>
                @Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" })                
        </div>
    </div>

@LabelFor(model=>model.Image,htmlAttributes:new{@class=“controllabel col-md-1”})
@Html.ValidationMessageFor(model=>model.Image,“,new{@class=“text danger”})
在控制器中,必须更改代码

public ActionResult Create_post(Game model,int[] platformsIds,HttpPostedFileBase ImageUrl)
    {
        if (ModelState.IsValid)
        {
            foreach (var pId in platformsIds)
            {
                var platform = _repository.Get<Platform>(pId);
                model.Platforms.Add(platform);
            }
            if (ImageUrl != null && ImageUrl.ContentLength > 0)
            {
                var uploadDir = "~/images/Games/"; // your location for save images
                string image = Guid.NewGuid().ToString() + ImageUrl.FileName;
                var imagePath = Path.Combine(Server.MapPath(uploadDir),image);
                ImageUrl.SaveAs(imagePath);
                model.Image = image;
            }

            _repository.Add<Game>(model);
            return RedirectToAction("Index");
        }
        ViewBag.Platforms = _repository.GetAll<Platform>().ToList();
        ViewBag.HtmlContent = model.Description;
        return View(model);
    }
public ActionResult Create_post(游戏模型,int[]platformsIds,HttpPostedFileBase ImageUrl)
{
if(ModelState.IsValid)
{
foreach(平台SID中的var pId)
{
var platform=\u repository.Get(pId);
model.Platforms.Add(平台);
}
if(ImageUrl!=null&&ImageUrl.ContentLength>0)
{
var uploadDir=“~/images/Games/”;//保存图像的位置
字符串image=Guid.NewGuid().ToString()+ImageUrl.FileName;
var imagePath=Path.Combine(Server.MapPath(uploadDir),image);
ImageUrl.SaveAs(imagePath);
模型。图像=图像;
}
_添加(模型);
返回操作(“索引”);
}
ViewBag.Platforms=_repository.GetAll().ToList();
ViewBag.HtmlContent=模型.描述;
返回视图(模型);
}

您只需在保存模型之前检查图像url即可

在您看来,您必须拥有此代码

 <div class="form-group">
        @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-1" })
        <div class="col-md-4">
                <div class="col-md-6 col-md-push-0">
                    <input type="file" class="form-control" name="ImageUrl" id="ImageUrl" />
                </div>
                @Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" })                
        </div>
    </div>

@LabelFor(model=>model.Image,htmlAttributes:new{@class=“controllabel col-md-1”})
@Html.ValidationMessageFor(model=>model.Image,“,new{@class=“text danger”})
在控制器中,必须更改代码

public ActionResult Create_post(Game model,int[] platformsIds,HttpPostedFileBase ImageUrl)
    {
        if (ModelState.IsValid)
        {
            foreach (var pId in platformsIds)
            {
                var platform = _repository.Get<Platform>(pId);
                model.Platforms.Add(platform);
            }
            if (ImageUrl != null && ImageUrl.ContentLength > 0)
            {
                var uploadDir = "~/images/Games/"; // your location for save images
                string image = Guid.NewGuid().ToString() + ImageUrl.FileName;
                var imagePath = Path.Combine(Server.MapPath(uploadDir),image);
                ImageUrl.SaveAs(imagePath);
                model.Image = image;
            }

            _repository.Add<Game>(model);
            return RedirectToAction("Index");
        }
        ViewBag.Platforms = _repository.GetAll<Platform>().ToList();
        ViewBag.HtmlContent = model.Description;
        return View(model);
    }
public ActionResult Create_post(游戏模型,int[]platformsIds,HttpPostedFileBase ImageUrl)
{
if(ModelState.IsValid)
{
foreach(平台SID中的var pId)
{
var platform=\u repository.Get(pId);
model.Platforms.Add(平台);
}
if(ImageUrl!=null&&ImageUrl.ContentLength>0)
{
var uploadDir=“~/images/Games/”;//保存图像的位置
字符串image=Guid.NewGuid().ToString()+ImageUrl.FileName;
var imagePath=Path.Combine(Server.MapPath(uploadDir),image);
ImageUrl.SaveAs(imagePath);
模型。图像=图像;
}
_添加(模型);
返回操作(“索引”);
}
ViewBag.Platforms=_repository.GetAll().ToList();
ViewBag.HtmlContent=模型.描述;
返回视图(模型);
}

什么“图像”?你的查看代码是什么?你是说上传文件吗?我在这里看不到任何可以上传文件的东西。但是已经有很多教程和示例展示了如何在MVC中工作。只要在网上搜索,你就不需要我们再发明轮子了。什么“图像”?你的查看代码是什么?你是说上传文件吗?我在这里看不到任何可以上传文件的东西。但是已经有很多教程和示例展示了如何在MVC中工作。只要在线搜索,你就不需要我们再次发明轮子了。