2013-05-30 89 views
1

我需要在我的視圖中添加文件上傳。我在我的主Html.BeginForm中添加了Html.BeginForm,以便文件上傳不會觸及Controller方法。一旦我把我的文件上傳出來,它的工作。但我需要以我的第一種形式上傳文件。在asp.net中上傳文件MVC

不工作:

@using (Html.BeginForm()) 
{ 
    @Html.DropDownListFor(m => m.StateModel, new SelectList(Model.StateModel, "Id", "StateName"), new { @id = "ddlstate", @style = "width:200px;", @onchange = "javascript:GetCity(this.value);" }) 
    <br /> 
    <br /> 
    <select id="ddlcity" name="ddlcity" style="width: 200px"> 

    </select> 

    <br /><br /> 

    using (Html.BeginForm("Upload", "Test", FormMethod.Post, new { enctype = "multipart/form-data" })) 
    { 
     <input type="file" name="file" /> 
     <input type="submit" value="Upload" /> 
    }    
} 

工作:

@using (Html.BeginForm("Upload", "Test", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <input type="file" name="file" /> 
    <input type="submit" value="Upload" /> 
} 

@using (Html.BeginForm()) 
{ 
    @Html.DropDownListFor(m => m.StateModel, new SelectList(Model.StateModel, "Id", "StateName"), new { @id = "ddlstate", @style = "width:200px;", @onchange = "javascript:GetCity(this.value);" }) 
    <br /> 
    <br /> 
    <select id="ddlcity" name="ddlcity" style="width: 200px"> 

    </select>    
} 
+0

你能不能給控制器呢?你爲什麼需要另一個表格?看起來像是外面的那個,那不是指向正確的行動,是問題! – Fals

回答

1

您不能在MVC中嵌套表單,您將需要使用工作選項。 或 如果要執行沒有表單其餘部分中的數據的文件上載,則需要使用標準輸入類型=按鈕(不提交),並使用jQuery/javascript連接此按鈕的單擊事件。

@*current rows *@ 
<div id="CurrentDetails"> 
    @Html.Partial("_DetailsGet") 
</div> 

@*newrow*@ 

    <table> 
    <tbody> 
     <tr> 
      <td>Description</td> 
      <td>Owner</td> 
     </tr> 
    <tr> 
    <td> 
    @Html.TextBoxFor(m => m.newDescription) 
     </td> 
    <td> 
    @Html.TextBoxFor(m => m.newOwner) 
     @Html.HiddenFor(m => m.ModelObject.Id) 
      </td> 
    <td> 
    <input id="adddetails" type="button" value="+" name='btnSubmit'/> 
     </td> 
    </tr> 
    </tbody> 
    </table> 

<script> 
$().ready(function() { 

     $("#adddetails").click(function() { 
      var description = $("#newDescription").val(); 
      var owner = $("#newPropertyDetailsOwner").val(); 
      var id = $("#ModelObject_Id").val(); 
      $.ajax({ 
       type: "POST", 
       url: "/MyRoute/DetailsAdd", 
       data: { 
        id: id, 
        description: description, 
        owner: owner 
       }, 
       success: function (data) { 
        $("#CurrentDetails").html(data); 
       } 

      }); 
     }); 
</script> 
4

的問題是你的外在形式不映射到正確的操作和控制,這就是爲什麼不工作。還請解釋爲什麼你需要兩個表單標籤?

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <label for="file">Upload Image:</label> 
    <input type="file" name="file" id="file"/> 
    <input type="submit" value="Upload Image" /> 
} 

試試這個。這裏的家是你的控制器,索引是你的行爲結果方法。

您可以在下面的鏈接中看到整個示例。 http://www.dotnetjalps.com/2012/04/file-upload-in-aspnet-mvc3.html

+0

這就是我上面所說的!做得好! :) – Fals

+0

你好!我需要兩個表單標籤,因爲我需要在提交主表單之前上傳文件。你是正確的,我的外表沒有被映射到正確的動作。我需要將一個動作映射到文件上傳,另一個動作映射到主窗體submit.hope很明顯 – chamara

+0

然後,您可以使用jquery異步上傳文件 - 請參閱http://weblogs.asp.net/bryansampica/archive/2013/01 /15/AsyncMVCFileUpload.aspx –