Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass multiple parameters in Html.BeginForm MVC4 controller action

I have something like this:

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

and in my .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" />
    }

here the value of imgid is not passing to controller action. show an error,The parameters dictionary contains a null entry for parameter 'imgid' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult ImageReplace

like image 290
neel Avatar asked Jan 06 '14 04:01

neel


2 Answers

Use this overload, which allows you to distinguish between route values and 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" />
}
like image 190
p.s.w.g Avatar answered Sep 19 '22 07:09

p.s.w.g


You can also pass imgid as a field in the form, something like:

@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" />
}
like image 22
Dimitar Dimitrov Avatar answered Sep 20 '22 07:09

Dimitar Dimitrov