user4229770
user4229770

Reputation:

How with FormData pass files and parameters to mvc controller action

i am trying to pass files and parameters to MVC controller action. I can get my model, files in controller but cannot get my other variables in controllers such as string CommunId, string CommunContext, i know it might be because of these false parameters, but if i do not set it to false my controller action is not even invoked. Here is my code:

function submitNewCommunication(s, e) {
     // var data = new FormData($(this).serialize()); //tried this one also

        $.ajax({
            url: '@Url.Action("CreateCommunication", "Communications")',
            type: 'POST',
            data: new FormData($('.createCommunicationForm').get(0)),
            processData: false,
            contentType: false,
            success: function (result) {
                alert('test'); 
            }
        });

}

My View

@using (Html.BeginForm("CreateCommunication", "Communications", new { CommunContext = ViewBag.CommContext, CommunId = ViewBag.CommId }, FormMethod.Post, new { @class = "createCommunicationForm"}))
{
//some code
}

My MVC controller:

        public ActionResult CreateCommunication(string CommunId, string CommunContext, string[] PartyTypes, [ModelBinder(typeof(DevExpress.Web.Mvc.DevExpressEditorsBinder))]CommunicationModel item)
    {
        var files = Request.Files;
    }

So files and model are here, but not these other variables, maybe is there a simple solution how can I obtain them ? Thanks for any help and Your time.

Upvotes: 0

Views: 1440

Answers (1)

James Dev
James Dev

Reputation: 3009

You can pass the values you require through hidden fields in the form:

@using (Html.BeginForm("CreateCommunication", "Communications", FormMethod.Post, new { @class = "createCommunicationForm"}))
{
    <input type="hidden" name="CommunContext" value="@ViewBag.CommContext">
    <input type="hidden" name="CommunId" value="@ViewBag.CommId">
}

Upvotes: 1

Related Questions