1

Let's assume I have a simple page with some information and a form.

@using (Html.BeginForm("UpdateOrder", "OrderController", FormMethod.Post)) {
      // some inputs here
}
<p id="user_info">Some text here</p>

All input's data will be sent like model or by FormCollection to the Controller.

However, I want also to send to the controller any text\image, generally any information from the page that located outside the form. Here text with id "user_info" as an example.

I wonder if it could be implemented without jQuery, only by using default Controller's functionality.

Iskander Raimbaev
  • 1,322
  • 2
  • 17
  • 35
  • 2
    If you do not want to use jQuery/javascript, you should keep the data inside the form with an input field so that when you submit the form it will be also submitted to the server. – Shyju Sep 19 '16 at 20:20
  • http://stackoverflow.com/questions/34764359/i-cant-get-the-value-of-my-file-input-asp-net-mvc-entity-framework – Tushar Gupta Sep 19 '16 at 20:23
  • 1
    Short answer is no. A form only submits the name/value pairs its successful controls (input, textarea, select) –  Sep 19 '16 at 21:44

2 Answers2

1

Try using hidden field to send additional data form to controller.

@using (Html.BeginForm("UpdateOrder", "Order", FormMethod.Post)) {
      // some inputs here
      <input type="hidden" name="user_info" id="user_info" value="Norway">
}
//<p id="user_info">Some text here</p>

and in OrderController controller action method

public ActionResult UpdateOrder(String user_info)
{
//Hidden filed **name** will be the name of the String
//From collection can be used in similar way
}

EDIT: you can update hidden field value by jQuery/javascript and after submission you can get updated value in controller and text/image it is slightly different

Syed Mhamudul Hasan
  • 1,341
  • 2
  • 17
  • 45
1

You can do it simply

1- if you want to upload some documents or images than your form should be like bellow code:

@using (Html.BeginForm("ApplyOnline", "Applieds", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  @Html.AntiForgeryToken()
  <input type="hidden" name="JobId" id="JobId" value="@ViewBag.JobId" />

  @Html.ValidationSummary(true, "", new { @class = "text-danger" })

   <div class="form-group">
      <label class="control-label col-md-3">First Name (اسم)</label>
      <div class="col-md-8">
      @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control",@required="required" } })
      @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
   </div>
  </div>
   <input type='file' name='pmd' id='pmd' />
<input type="submit" value="Apply" class="btn btn-primary"  />
}

than in countroller in post method

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ApplyOnline([Bind(Include = "Id,JobId,FirstName")] Applied applied, HttpPostedFileBase pmd, int JobId)
{
  if (ModelState.IsValid)
  {
            //---save the data---------//
            db.MyAppliedContext.Add(applied);
            db.SaveChanges();
            //---Get inserted Id----//
            int insertedId = applied.Id;
             //--------Upload PMD-------------------//
            if (pmd != null && pmd.ContentLength > 0)
            {
                try
                {
                    var PMDFileName = "PMD-" + applied.JobId + "-" + TrimedUser + "-" + insertedId + "-" + pmd.FileName;
                    //var P11FileName = DateTime.Now.ToString();
                    string path = Path.Combine(Server.MapPath("~/App_Data/system"),
                                               Path.GetFileName(PMDFileName));
                    pmd.SaveAs(path);
                    UploadFiles MyPMDUploads = new UploadFiles();
                    MyPMDUploads.JobId = applied.JobId;
                    MyPMDUploads.ApplyId = insertedId;
                    MyPMDUploads.FileName = Path.GetFileName(PMDFileName);
                    MyPMDUploads.FilePath = path;
                    db.MyUploadFileContext.Add(MyPMDUploads);
                    db.SaveChanges();

                    ViewBag.Message = "PMD uploaded successfully";
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR PMD:" + ex.Message.ToString();
                }

            }
            else
            {
                ViewBag.Message = "You have not specified a PMD file.";
            }
   }
   return view(Model);
}

this way you can upload files and data all is included hop this help you

MJ X
  • 8,506
  • 12
  • 74
  • 99