使用另一个控制器 [HttpPost] ActionLink 在表单中上传文件
Upload a file within a form using another Controller [HttpPost] ActionLink
如何以一种形式使用我的两个控制器 Action?
我想不出在 BeginForm()
中执行两个控制器操作的方法。
我对我的项目有这样的看法:
@using (Html.BeginForm()){
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>T_Categorie</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CatName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CatName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CatName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CatDesc, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CatDesc, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CatDesc, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<label class="control-label col-md-2">Upload an image</label>
<input type="file" name="image" runat="server" style="width: 100%;" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>}
这是我的类别创建 ActionLink,它位于 T_CategorieController
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CatName,CatDesc")] T_Categorie t_Categorie)
{
if (ModelState.IsValid)
{
db.T_Categorie.Add(t_Categorie);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(t_Categorie);
}
这是我的文件上传器,它位于 T_Images 控制器
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase image, T_Categorie CurrentCat)
{
if (image != null)
{
if (image.ContentLength > 0)
{
byte[] imageData = null;
using (var binaryReader = new BinaryReader(image.InputStream))
{
imageData = binaryReader.ReadBytes(image.ContentLength);
}
try
{
//Upload to database
T_Images newImage = new T_Images { ImgData = imageData };
db.T_Images.Add(newImage);
db.SaveChanges();
//Change the currentCat fk_ImgId
var CurrentImageDB = db.T_Images.OrderByDescending(t => t.ImgId).First();
var currentRefCat = db.T_Categorie.Find(CurrentCat.CatId);
//
currentRefCat.fk_ImgID = CurrentImageDB.ImgId;
db.SaveChanges();
}
catch (System.Data.SqlClient.SqlException)
{
throw new FileLoadException();
}
}
}
return RedirectToAction("Index", "Home");
}
我需要将参数传递给 Html.BeginForm()
由于 [HttpPost] 将模型绑定到我创建的 ActionLink,我只需要在 BeginForm()
中引用我的图像
我的视图BeginForm程序:
@using (Html.BeginForm("Create", "T_Categorie", FormMethod.Post, new { enctype = "multipart/form-data" }))
我的控制器正在将我的 CreateActionLink 重定向到 FileUpload 过程
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(HttpPostedFileBase IMG, [Bind(Include = "CatName,CatDesc")] T_Categorie t_Categorie)
{
if (ModelState.IsValid)
{
db.T_Categorie.Add(t_Categorie);
db.SaveChanges();
var addedCategory = db.T_Categorie.Find(t_Categorie.CatName);
return RedirectToAction("FileUpload", "T_Images", new { image = IMG, CurrentCat = addedCategory });
}
return View(t_Categorie);
}
如何以一种形式使用我的两个控制器 Action?
我想不出在 BeginForm()
中执行两个控制器操作的方法。
我对我的项目有这样的看法:
@using (Html.BeginForm()){
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>T_Categorie</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CatName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CatName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CatName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CatDesc, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CatDesc, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CatDesc, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<label class="control-label col-md-2">Upload an image</label>
<input type="file" name="image" runat="server" style="width: 100%;" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>}
这是我的类别创建 ActionLink,它位于 T_CategorieController
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CatName,CatDesc")] T_Categorie t_Categorie)
{
if (ModelState.IsValid)
{
db.T_Categorie.Add(t_Categorie);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(t_Categorie);
}
这是我的文件上传器,它位于 T_Images 控制器
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase image, T_Categorie CurrentCat)
{
if (image != null)
{
if (image.ContentLength > 0)
{
byte[] imageData = null;
using (var binaryReader = new BinaryReader(image.InputStream))
{
imageData = binaryReader.ReadBytes(image.ContentLength);
}
try
{
//Upload to database
T_Images newImage = new T_Images { ImgData = imageData };
db.T_Images.Add(newImage);
db.SaveChanges();
//Change the currentCat fk_ImgId
var CurrentImageDB = db.T_Images.OrderByDescending(t => t.ImgId).First();
var currentRefCat = db.T_Categorie.Find(CurrentCat.CatId);
//
currentRefCat.fk_ImgID = CurrentImageDB.ImgId;
db.SaveChanges();
}
catch (System.Data.SqlClient.SqlException)
{
throw new FileLoadException();
}
}
}
return RedirectToAction("Index", "Home");
}
我需要将参数传递给 Html.BeginForm() 由于 [HttpPost] 将模型绑定到我创建的 ActionLink,我只需要在 BeginForm()
中引用我的图像我的视图BeginForm程序:
@using (Html.BeginForm("Create", "T_Categorie", FormMethod.Post, new { enctype = "multipart/form-data" }))
我的控制器正在将我的 CreateActionLink 重定向到 FileUpload 过程
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(HttpPostedFileBase IMG, [Bind(Include = "CatName,CatDesc")] T_Categorie t_Categorie)
{
if (ModelState.IsValid)
{
db.T_Categorie.Add(t_Categorie);
db.SaveChanges();
var addedCategory = db.T_Categorie.Find(t_Categorie.CatName);
return RedirectToAction("FileUpload", "T_Images", new { image = IMG, CurrentCat = addedCategory });
}
return View(t_Categorie);
}