10

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

neel
  • 5,123
  • 12
  • 47
  • 67

4 Answers4

26

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" />
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • I think you mean this overload: **BeginForm(HtmlHelper, String, String, RouteValueDictionary, FormMethod, IDictionary)** https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.html.formextensions.beginform?view=aspnet-mvc-5.2#system-web-mvc-html-formextensions-beginform(system-web-mvc-htmlhelper-system-string-system-string-system-web-routing-routevaluedictionary-system-web-mvc-formmethod-system-collections-generic-idictionary((system-string-system-object))) – sergiol Jul 06 '23 at 11:36
4

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" />
}
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
3

Use this:

      @using (Html.BeginForm("ImageReplace", "Member",   
      new { imgid = @Model.Id },   FormMethod.Post,
  new { enctype = "multipart/form-data" }))
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 );
        }