4

I upload image using Dropzone. I need to send some other values via that(Some text values).In my scenario i need to send Product Name to the controller(pls see some comment in code)

Image successfully comes to the Controller's side,when button click.

HTML Code :

<input type="text" name="text" id="txtprod" class="form-control" />

<div id="dropzonffe" style="width: 55%; margin-left: 25%">
    <form action="~/Admin/SaveProducts" class="dropzone" id="dropzoneJsForm"></form>
</div>

jQuery Code :

<script type="text/javascript">
Dropzone.options.dropzoneJsForm = {

    autoProcessQueue: false,
    init: function () {
        var submitButton = document.querySelector("#btnSubmit");
        var myDropzone = this;

        submitButton.addEventListener("click", function () {
            var ProductName = $("#txtprod").val();//<-- I want to send this Productname to the controller also.
            myDropzone.processQueue();
        });
    }
};
</script>

My Controller :

public ActionResult SaveProducts () {
    bool isSavedSuccessfully = false;

    foreach (string fileName in Request.Files) {
        HttpPostedFileBase file = Request.Files[fileName];
        isSavedSuccessfully = true;
    }
    return Json (new { isSavedSuccessfully, JsonRequestBehavior.AllowGet });
}

I need to pass the Product Name to the controller. How can I do it ?

1
  • Have you tried adding the formcollection? public ActionResult SaveProducts(FormCollection fc) {... }
    – mrpotocnik
    Commented Apr 9, 2015 at 19:41

3 Answers 3

10

I found the solution to my problem.Here i have paste it for future help.There's a event in DropZone called sending.you can pass additional parameters via append to form data.

 Dropzone.options.dropzoneJsForm = {
    autoProcessQueue: false,
    init: function () {
        var submitButton = document.querySelector("#btnSubmit");
        var myDropzone = this;

        this.on("sending", function (file, xhr, formData) {
            formData.append("ProductName", $("#txtprod").val());


        });

        submitButton.addEventListener("click", function () {
        myDropzone.processQueue();

        });

    }
};

Access it in Server Side

 public ActionResult SaveProducts(string ProductName)
    {

     }
1
  • how ablout multiple upload. It will have many ProductName parameter? Commented Aug 5, 2019 at 1:10
2

You can create a model

public class ProductModel {
    public string Name {get;set;}
}

Add inside the form the Name

<div id="dropzonffe" style="width: 55%; margin-left: 25%">
    <form action="~/Admin/SaveProducts" class="dropzone" id="dropzoneJsForm">
        @Html.EditorFor(m => m.Name);
    </form>
</div>
<button id="btnSubmit">Submit</button>

And pass in Action the Model, the value from inputs will be sent alongside with files.

public ActionResult SaveProducts(ProductModel product)
{
     // rest of the logic
3
  • But in here you use Html helper control ( @Html ) know ? i use <input> textbox.How can i do that .. pls see updated OP
    – Alex
    Commented Apr 19, 2015 at 1:55
  • @Alex in your case will be <input type="text" name="ProductName" id="txtprod" class="form-control" />
    – adricadar
    Commented Apr 19, 2015 at 6:00
  • I struggled to get this work for ages until I finally got the configuration right. I'm not sure what exactly I was doing wrong but now it works a charm. Commented Nov 1, 2018 at 23:30
1

Add the values as a hidden field to your form and they will be sent to your controller

<form action="~/Admin/SaveProducts" class="dropzone" id="dropzoneJsForm">
    <input type="hidden" value="SomeVal" />

and then

public ActionResult SaveProducts(string SomeVal) 
{

   ..controller body
}

or the FormCollection object would work as well

public ActionResult SaveProducts(FormCollection fc) 
{
     //use fc["SomeVal"]
   ..controller body
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.