首页 文章

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

提问于
浏览
3

我有这样的事情:

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的值没有传递给控制器动作 . 显示错误,参数字典包含参数'imgid'的非可空类型'System.Int32'的空条目,用于方法'System.Web.Mvc.ActionResult ImageReplace

3 回答

  • 17

    使用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" />
    }
    
  • 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" />
    }
    
  • 3

    用这个:

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

相关问题