2016-11-09 32 views
0

我如何使用我的兩個控制器行動以某種形式使用另一個控制器[HttpPost] ActionLink的

我想不通的方式做兩個控制器動作在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"); 
    } 
+0

所以你想讓你的表單調用Create然後調用FileUpload?如果是這樣,你不能。表格只能有一個選項。更改您的控制器以在您的文件上傳操作中讀取表單數據。 – Darren

+0

我可以爲Create ActionLink添加多個參數,以便我可以從那裏引用我的圖像? –

+0

將隱藏字段添加到您的表單中並在您的操作中讀取它們。在這裏,這將有助於http://stackoverflow.com/questions/5149116/mvc-how-to-post-file-upload-and-other-form-fields-to-one-action – Darren

回答

0

我需要一個參數傳遞給Html.BeginForm() 由於[HttpPost]綁定模型來創建我的ActionLink的我只是引用我在BeginForm()圖像

我的視圖BeginForm過程:

@using (Html.BeginForm("Create", "T_Categorie", FormMethod.Post, new { enctype = "multipart/form-data" })) 

我的控制器被重定向我CreateActionLink到文件上傳過程

  [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); 
    } 
相關問題