[英]How to create EDIT method and EDIT Razor View for Image which i stored in database in VARBINARY datatype in ASP.net MVC?
这是我的创建方法
/* [HttpPost]
public ActionResult Create(Image newRecipe)
{
try
{
using (var db = new MitishaKitchenContext())
{
db.Images.Add(newRecipe);
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch { return View(); }
}*/
[HttpPost]
public ActionResult Create(Image IG)
{
// Apply Validation Here
if (IG == null)
{
return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
}
if (IG != null && IG.File.ContentLength > (2 * 1024 * 1024))
{
ModelState.AddModelError("CustomError", "File size must be less than 2 MB");
return View();
}
if (!(IG.File.ContentType == "image/jpeg" || IG.File.ContentType == "image/gif"))
{
ModelState.AddModelError("CustomError", "File type allowed : jpeg and gif");
return View();
}
// IG.FileName = IG.File.FileName;
// IG.ImageSize = IG.File.ContentLength;
byte[] data = new byte[IG.File.ContentLength];
IG.File.InputStream.Read(data, 0, IG.File.ContentLength);
IG.ImageData = data;
using (MitishaKitchenContext dc = new MitishaKitchenContext())
{
dc.Images.Add(IG);
dc.SaveChanges();
}
return RedirectToAction("Index");
}
}
如何创建我在ASP.net MVC中以VARBINARY数据类型存储在数据库中的图像的EDIT方法和EDIT Razor View?
这是我的创建视图:
{
@using (Html.BeginForm("Create","Recipes",null, FormMethod.Post,new {enctype="multipart/form-data"}))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Image</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ImageName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ImageName)
@Html.ValidationMessageFor(model => model.ImageName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Recipe.RecipeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Recipe.RecipeName)
@Html.ValidationMessageFor(model => model.Recipe.RecipeName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Recipe.RecipeDescriptions)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Recipe.RecipeDescriptions,new {style = "width:90%;height:400px"})
@Html.ValidationMessageFor(model => model.Recipe.RecipeDescriptions)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Recipe.RecipeIngredients)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Recipe.RecipeIngredients)
@Html.ValidationMessageFor(model => model.Recipe.RecipeIngredients)
</div>
<br />
<table>
<tr>
<td>Select File : </td>
<td>
@Html.TextBoxFor(Model=> Model.File, new{type="file"})
@Html.ValidationMessage("CustomError")
</td>
<td>
<input type="submit" value="Upload" />
</td>
</tr>
</table>
enter code here
</fieldset>
}
}
您将需要在数据库表中包含以下组件:RecordId,ImageName,RecipeName,RecipeDescriptions,RecipeDescriptions,ImageData。
然后,在编辑中您可以执行以下操作:
[HttpGet]
public ActionResult Edit(int recordId)
{
using (var db = new MitishaKitchenContext())
{
// fetch data for the image by Id
var yourmodel = db.Images.Where(x => x.RecordId == recordId).FirstOrDefault();
if(yourmodel != null)
{
return View("Create", yourmodel);
}
}
return RedirectToAction("Index");
}
您可以使用与“创建”使用的视图相同的视图,因为模型将填充您的字段。 然后,您应该搜索recordId是否存在并向上插入(如果已经存在,则插入或更新)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.