2012-02-18 66 views
0

我的第一個.Net/MVC項目,我生成了一個視圖,允許我在數據庫中列出,編輯和創建項目,並且我想添加一個文件上傳控件到創建頁面,只上傳一個文件。我知道在[HttpPost]我需要「公共ActionResult索引(HttpPostedFileBase文件)」,但我目前的[HttpPost]是這樣的:「公共ActionResult創建(lm_pics lm_pics)」。在創建視圖中上傳文件和文本

+0

你能展示你當前的視圖是怎樣的嗎? – 2012-02-19 08:57:00

回答

1

爲什麼你有2個文件輸入?你爲什麼有兩種形式?是不是第一種形式足夠了(假設當然你的enctype="multipart/form-data"屬性添加到它,你不能沒有它上傳文件)?:

@model PictureUploader_MVC.Models.lm_pics 
@{ 
    ViewBag.Title = "Create"; 
} 
<h2>Upload Picture</h2> 

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>lmit_pics</legend> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.brand) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.brand) 
      @Html.ValidationMessageFor(model => model.brand) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.enabled) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.enabled) 
      @Html.ValidationMessageFor(model => model.enabled) 
     </div> 
     <div> 
      <label for="file">Filename:</label> 
      <input type="file" name="file" id="file" /> 
     </div> 
    </fieldset> 

    <button type="submit">Create</button> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

和控制器的行動,將處理表單提交:

[HttpPost] 
public ActionResult Create(lm_pics lm_pics, HttpPostedFileBase file) 
{ 
    ...  
} 
+0

我無意中發佈了兩個文件輸入的代碼 - 我嘗試了很多東西。特別是我被上述鏈接拋出,表示將文件輸入另一種形式。我將在上面編輯我的帖子以刪除任何錯誤信息。 – Dave 2012-02-20 15:15:37

+0

完全作品!我不知道可以將多個參數傳遞給Create。謝謝。 – Dave 2012-02-20 15:15:47