UpLoad de multiplas imagens
Boa Tarde,
Estou desenvolvendo uma aplicação web - asp.net mvc 4 razor, na qual preciso fazer:
1 - upload de 1 ou várias imagens;
2 - Grava no bd (id, descrição, referencia, nome da imagem e internet)
3 - Após o upload, apresentar estas imagens e editar os seguintes campos (descrição e internet);
4 - Gravar as alterações no bd.
na minha aplicação atual consegui fazer até o item 3 - não estou conseguindo pegar o model editado para gravar
alguém pode me ajudar??
Home Controller:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UpLoad.Models;
namespace UpLoad.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
var imagens = new List<Imagem>();
var path = "";
var i = 0;
var referencia = 100;
var temObjeto = new Imagem()
{
id = i,
referencia = referencia,
caminho = path
};
imagens.Add(temObjeto);
return View(imagens);
}
[HttpPost]
public ActionResult Index(Picture picture, string salvarFotos, FormCollection form)
{
//string query = form["txtBusca"];
if (!string.IsNullOrEmpty(salvarFotos))
{
ViewBag.Title = "Atualiza Fotos";
}
var imagens = new List<Imagem>();
var i = 0;
var descricao = "Descrição ";
var referencia = 100;
foreach (var file in picture.Files)
{
i++;
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Content/images"), fileName);
file.SaveAs(path);
var temObjeto = new Imagem()
{
id = i,
referencia = referencia,
descricao = descricao,
caminho = fileName
};
imagens.Add(temObjeto);
}
}
//return RedirectToAction("Index");
return View(imagens);
}
[HttpPost]
public ActionResult Salvar(Imagem image)
{
if (ModelState.IsValid)
{
//ViewBag.Title = "Atualiza Fotos" + image.caminho;
}
return View(image);
}
}
}
Classe Imagem:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UpLoad.Models
{
public class Imagem
{
public int id { get; set; }
public int referencia { get; set; }
public string descricao { get; set; }
public string caminho { get; set; }
}
}
Classe Picture:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UpLoad.Models
{
public class Picture
{
public IEnumerable<HttpPostedFileBase> Files {get; set; }
}
}
View:
@model IEnumerable<UpLoad.Models.Imagem>
@{
ViewBag.Title = "Index";
var salvarFotos = "SIM";
@*<h1><img src="~/Content/images/119.JPG" /></h1>*@
}
<h2>Up Load File</h2>@using (Html.BeginForm(null, null, new {model = Model}, FormMethod.Post))
{
<fieldset>
<legend>Imagens</legend>
@foreach (var item in Model)
{
<h1>
@Html.EditorFor(a => item.caminho)
@Html.EditorFor(a => item.descricao)
<img src="~/Content/images/@item.caminho" alt="@item.caminho" height="82" width="82" />
</h1>
}
<p>
<input type="submit" value="Salvar" />
</p>
</fieldset>
}@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
<tr>
<td>File:</td>
<td>
<input type="file" name="Files" id="Files" multiple /></td>
</tr>
<tr>
<td>&npsp</td>
<td>
<input type="submit" name="submit" value="UpLoad" /></td>
</tr>
</table>
}Discussão (0)
Carregando comentários...