2014-01-06 64 views
3

我有這樣的事情:通在Html.BeginForm MVC4控制器動作的多個參數

public ActionResult ImageReplace(int imgid,HttpPostedFileBase file) 
    { 
     string keyword = imgid.ToString(); 
     ....... 
    } 

,並在我的.cshtml:

@model Models.MemberData 
    @using (Html.BeginForm("ImageReplace", "Member", FormMethod.Post, 
      new { imgid = @Model.Id, enctype = "multipart/form-data" })) 
     { 
    <input type="file" name="file" id="file" value="Choose Photo" /> 
    <input type="submit" name="submit" value="Submit" /> 
    } 

這裏imgid的值不會傳遞給控制器​​動作。顯示一個錯誤,參數字典包含一個null項,用於方法'System.Web.Mvc.ActionResult的非空值類型'System.Int32'的參數'imgid'的空項。替換

回答

12

使用this overload,它允許您區分路由值和HTML attribtues:

@using (Html.BeginForm(
     "ImageReplace", "Member", 
     new { imgid = @Model.Id }, 
     FormMethod.Post, 
     new { enctype = "multipart/form-data" })) 
{ 
    <input type="file" name="file" id="file" value="Choose Photo" /> 
    <input type="submit" name="submit" value="Submit" /> 
} 
3

您也可以通過imgid作爲形式的領域,像這樣:

@model Models.MemberData 
@using (Html.BeginForm("ImageReplace", "Member", FormMethod.Post, 
     new { enctype = "multipart/form-data" })) 
{ 
    @Html.HiddenFor(x => x.Id) 
    <input type="file" name="file" id="file" value="Choose Photo" /> 
    <input type="submit" name="submit" value="Submit" /> 
} 
2

使用此:

 @using (Html.BeginForm("ImageReplace", "Member", 
     new { imgid = @Model.Id }, FormMethod.Post, 
    new { enctype = "multipart/form-data" })) 
相關問題