我是初学者对于MVC,我开发了我的图像上传脚本项目,我想在网格中显示我上传的图像,有些人需要将上传的图像替换为其他图像,我的代码不是工作正常,请帮助我, 图片 的模型
public class MainGroup
{
[DataType(DataType.ImageUrl)]
public string Image { get; set; }
}
查看 - 索引
<table style="border: 1px solid black;" cellpadding="10">
<tr>
<th>
@Html.DisplayNameFor(model => model.Image)
</th>
<th>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td valign="top">
<img src="@Url.Content("~/Content/Uploaded/" + item.MainGroupID + "/" + item.Image + "")" alt="@item.MainGroupName" width="100px" height="100px" />
</td>
<td valign="top">
@Html.ActionLink("Edit", "Edit", new { id = item.MainGroupID }, new { @class = "createActor", title = "Edit Actor" })
|
@Html.ActionLink("Delete", "Delete", new { id = item.MainGroupID })
</td>
</tr>
}
</table>
创建-CHTML
@using (@Html.BeginForm("Save", "MainGroup"))
{
<div class="editor-field">
<input type="file" name="imageFile" id="imageFile" />
@Html.ValidationMessageFor(model => model.Image)
</div>
<div class="col-md-6" style="font-family:Arial, Helvetica, sans-serif; font-size:13px;"><b>Status</b></div>
<div class="col-md-6">
@Html.CheckBoxFor(a => a.StatusId)
</div>
<div class="col-md-6"></div> <div class="col-md-6"></div>
<div class="col-md-6"></div>
<div class="col-md-6"> <input type="submit" id="btn" value="Save" class="btn btn-success" /> </div>
}
</div>
}
控制器
public class MainGroupController : Controller
{
// GET: Masters/MainGroup
public ActionResult Save(MainGroup maingroup)
{
if (ModelState.IsValid)
{
var objContext = new KnittingdbContext();
maingroup.CreateId = 1;
maingroup.StatusChangeDate = System.DateTime.Now;
maingroup.CreatedDate = System.DateTime.Now;
maingroup.EditId = 1;
maingroup.EditDate = System.DateTime.Now;
objContext.MainGroups.Add(maingroup);
objContext.SaveChanges();
TempData["Success"] = "Saved Sucessfully";
return RedirectToAction("MainGroupIndex", new { A = "New" });
}
else
{
return PartialView("_MainGroupCreate", maingroup);
}
}
答案 0 :(得分:2)
您可以将文件定义为HttpFileBase属性以保存文件,然后在post方法(保存)中将文件移动到&#34;〜/ Content / Uploaded /&#34;。然后试试。如果图像文件可用,您也可以手动检查目录
public class MainGroup
{
[DataType(DataType.ImageUrl)]
public string Image { get; set; }
public HttpFileBase ImageFile { get; set; }
}
public class MainGroupController:Controller { // GET:Masters / MainGroup
public ActionResult Save(MainGroup maingroup)
{
if (ModelState.IsValid)
{
// You can add other logic also
if(maingroup.ImageFile!= null && maingroup.ImageFile.ContentLength > 0)
{
string filePath = "~/Content/Uploaded/";
string imageUrl = System.IO.Path.Combine(filePath, maingroup.ImageFile.File.FileName);
var objContext = new KnittingdbContext();
maingroup.CreateId = 1;
maingroup.Image = imageUrl;
maingroup.ImageFile.File.SaveAs(imageUrl));
maingroup.StatusChangeDate = System.DateTime.Now;
maingroup.CreatedDate = System.DateTime.Now;
maingroup.EditId = 1;
maingroup.EditDate = System.DateTime.Now;
objContext.MainGroups.Add(maingroup);
objContext.SaveChanges();
TempData["Success"] = "Saved Sucessfully";
}
return RedirectToAction("MainGroupIndex", new { A = "New" });
}
else
{
return PartialView("_MainGroupCreate", maingroup);
}
}