0

My form

@using (Html.BeginForm("ViewProjectAssignment", "AssignmentOfProject", FormMethod.Post, new { enctype = "multipart/form-data", @id="formAssignment" }))
{
    @Html.AntiForgeryToken()
    <table class="headertable">            
        <tbody>
            <tr>
                <td style="padding-left:50px; width:120px;">
                    <b>Project Name</b>
                </td>
                <td>
                    @Html.DropDownListFor(mod => mod.ProjectName, Model.ProjectNameList, new { @class = "textBoxDisplay", @id = "ddlProjectName", style = "width:300px;" })
                    @Html.ValidationMessageFor(x => x.ProjectName)
                </td>
            </tr>
            <tr>
                <td style="padding-left:50px; width:120px;">
                    <b>CEQR Number</b>
                </td>
                <td>
                    @Html.DropDownListFor(mod => mod.CEQRNumber, Model.CEQRNumberList, new { @class = "textBoxDisplay", @id = "ddlCEQRNumber" })
                    @Html.ValidationMessageFor(x => x.CEQRNumber)
                </td>
            </tr>
            <tr>
                <td style="padding-left:50px; width:120px;">
                    <b>Upload File</b>
                </td>
                <td>
                    @Html.TextBoxFor(mod => mod.File, new { @class = "textBoxFileDisplay", style = "width:600px;", type = "file", multiple = "true", @id = "txtFiles" })
                    @Html.ValidationMessageFor(x => x.File)
                </td>
            </tr>
            <tr>
                <td style="padding-left:50px; width:120px;"></td>
                <td>
                    <button name="button" class="button" value="UploadFile" id="btnUploadFiles">
                        Upload File
                    </button>                        
                    &nbsp;
                    <button name="button" value="create" class="button" id="btnClearSeach">
                        Clear
                    </button>
                </td>
            </tr>
        </tbody>
    </table>
}

Jquery

// AntiForgeryToken 

   var token = $('[name=__RequestVerificationToken]').val();
    var headers = {};
    headers["__RequestVerificationToken"] = token;

// button click event

$('#btnUploadFiles').click(function (event) {
        fnBlockUI();
        event.preventDefault();
        $.ajax({
            url: '@Url.Action("ViewProjectAssignment", "AssignmentOfProject", new { Area = "PrivateCEQRApplication" })',
            type: 'POST',
            dataType: 'json',
            cache: false,
            headers: headers,
            data: {
                ProjectName: $("#ddlProjectName").val(),
                CEQRNumber: $("#ddlCEQRNumber").val(),
                File: $("#txtFiles").val()
            },
            beforeSend: function (xhr, settings) { xhr.setRequestHeader('__RequestVerificationToken', token); },
            success: function (result) {
                $.unblockUI();
                $('body').empty().append(result);
            },
            error: function (xhr, textStatus, errorThrown) {
                $.unblockUI();
                alert("An error occurred while processing your request. Please try again. If the error persists, please email the account administrator .");
            }
        });
    });

Controller Method

[HttpPost]
public ActionResult ViewProjectAssignment(ProjectUploadFiles uploadFiles)
{
    // Upload code here
}

Model

public class ProjectUploadFiles
{
    public string CEQRNumber { get; set; }

    public string ProjectName { get; set; }

    public IEnumerable<HttpPostedFileBase> File { get; set; }
}

Issue

  1. My application allows user to upload multiple files against CEQRNumber and corresponding ProjectName. btnUploadFiles click event makes ajax post, passing selected CEQRNumber, ProjectName and the Files to the coltroller. Now the problem is that the parameter the Action Method gets from ajax call return null for file as appose to IEnumerable(public ActionResult ViewProjectAssignment(ProjectUploadFiles uploadFiles))
DotNetBeginner
  • 412
  • 2
  • 15
  • 34
  • 1
    where is your @Html.AntiForgeryToken() in your form? – dansasu11 Sep 02 '15 at 17:55
  • OMG thanks u, that was foolish foolish foolish and foolish of me. Any idea about issue 1 – DotNetBeginner Sep 02 '15 at 19:55
  • 1
    You can use `FormData` post form values and files. Refer [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) –  Sep 02 '15 at 22:57
  • 1
    Try setting processData: false and try again , you can add this after cache: false . According to the Jquery site, By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false. – dansasu11 Sep 03 '15 at 10:54

0 Answers0