在Html.BeginForm MVC4控制器动作中传递多个参数

时间:2014-01-06 04:21:36

标签: c# asp.net-mvc asp.net-mvc-4

我有这样的事情:

   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的值没有传递给控制器​​动作。显示错误,参数字典包含方法'System.Web.Mvc.ActionResult ImageReplace

的非可空类型'System.Int32'参数'imgid'的空条目

4 个答案:

答案 0 :(得分:18)

使用this overload,可以区分路由值和HTML属性:

@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" />
}

答案 1 :(得分: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 :(得分:3)

使用此:

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

答案 3 :(得分:0)

    @using (Html.BeginForm("Search", "Orders", FormMethod.Post, htmlAttributes: new { id = "example-form", @class = "app-search" }))
       {

           <input type="text" class="form-control" name="OrderNo" style="max-width:100% !important" placeholder="Search OrderNo"> 
            <a class="srh-btn">
             <i class="ti-close"></i>
            </a>
           </input>

       }

  public ActionResult Search(FormCollection form)
        {
            string Orderno = form["OrderNo"];
            int orderno = Convert.ToInt32(Orderno);

            return View(orderno );
        }