Я новичок в ASP.NET MVC, так что извините за ошибки.
Мне нужна страница просмотра (index.cshtml
), где я могу отображать изображение, изменять/удалять изображение, сохраняя его в столбце Varbinary(max)
в таблице SQL Server.
В таблице базы данных есть следующие столбцы:
ID int primary key Identity not null,
ImageName nvarchar(50) ,
ImagePicInBytes varbinary(MAX) null
Я использую Image.cs
с этим кодом:
public class Image
{
public int ID {get; set;}
public string ImageName {get; set;}
public byte[] ImagePicInBytes {get; set;}
}
ImageContext
класс, как показано ниже
public class ImageContext : DbContext
{
public DbSet<Image> Images { get; set; }
}
Строка подключения, как показано ниже
<connectionStrings>
<add name="ImageContext"
connectionString="server=.; database=Sample; integrated security =SSPI"
providerName="System.Data.SqlClient"/>
</connectionStrings>
ImageController
как показано ниже
public class ImageController : Controller
{
private ImageContext db = new ImageContext();
// GET: /Image/
public ActionResult Index()
{
return View(db.Images.ToList());
}
// GET: /Image/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Image image = db.Images.Find(id);
if (image == null)
{
return HttpNotFound();
}
return View(image);
}
}
Создали представления, как показано ниже
public class ImageController : Controller
{
private ImageContext db = new ImageContext();
// GET: /Image/
public ActionResult Index()
{
return View(db.Images.ToList());
}
// GET: /Image/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Image image = db.Images.Find(id);
if (image == null)
{
return HttpNotFound();
}
return View(image);
}
// GET: /Image/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Image image = db.Images.Find(id);
if (image == null)
{
return HttpNotFound();
}
return View(image);
}
// POST: /Image/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Image image = db.Images.Find(id);
db.Images.Remove(image);
db.SaveChanges();
return RedirectToAction("Index");
}
}
}
Мой create.cshtml (представление), как показано ниже
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ImageName)
</th>
<th>
@Html.DisplayNameFor(model => model.ImagePicInBytes)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ImageName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ImagePicInBytes)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
У меня есть следующие 3 вопроса
Как я могу создать новую запись в таблице данных, загрузив новое изображение из файловой системы в столбец
Varbinary
в базе данных?Как я могу иметь представление для управления FILEUPLOAD в «создать представление» и «редактировать представление»
Могу ли я использовать
HttpPostedFileBase
для достижения вышеуказанного изCreate.cshtml
? Если да: как? Любые предложения или справочные ссылки доступны?