0
がどのように私は1つの形で私の2つのコントローラのアクションを使用することができますActionLinkの[HttpPost]別のコントローラを使用して、フォーム内のファイルをアップロードBeginForm()で2つのコントローラのアクションを行うために、私は方法を理解することはできません</p> <p>
私は私のプロジェクトで、この見解を持っている:
@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);
}
です
ここアップローダーウィッヒは、私がHtml.BeginForm(にパラメータを渡すために必要な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");
}
フォームでCreateを呼び出し、FileUploadを呼び出すようにしますか?もしそうなら、あなたはできません。フォームには1つのオプションしか指定できません。ファイルアップロードアクションでフォームデータを読み取るようにコントローラーを変更します。 – Darren
アクションリンクの作成に複数のパラメータを設定して、そこから画像を参照できますか? –
フォームに非表示のフィールドを追加し、アクションでそれらのフィールドを読み込みます。ここでは、これは役立ちますhttp://stackoverflow.com/questions/5149116/mvc-how-to-post-file-upload-and-other-form-fields-to-one-action – Darren