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

4 Answers 4

27

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" />
}
Sign up to request clarification or add additional context in comments.

1 Comment

I think you mean this overload: BeginForm(HtmlHelper, String, String, RouteValueDictionary, FormMethod, IDictionary<String,Object>) learn.microsoft.com/en-us/dotnet/api/…
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" />
}

Comments

3

Use this:

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

Comments

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 );
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.