0

im posting a form that contain file and html data plus a function to get geocode ,but the controller only get the data from the ajax post but not the file, i assume that it have something to do with the fact the button posting but i'm not sure ,

    @using (Html.BeginForm("Create", "lecture", FormMethod.Post, new { enctype = "multipart/form-data", id = "RestoForm" }))
{
    //values has remove for clear
    <div class="form-group">
        @Html.LabelFor(f => f.RImage) <i class="glyphicon glyphicon-folder-open"></i>
        <input type="file" name="RImage" class="btn btn-default btn-sm btn-google btn-group-justified hvr-shadow " />
    </div>

    <input type="button" class="btn btn-primary btn-lg" onclick="GetLocation()" value="Finish" />
}

@section scripts
{

    <script type="text/javascript">
        function GetLocation() {
            var geocoder = new window.google.maps.Geocoder();

            var Street = document.getElementById('txtAddress').value;
            var City = document.getElementById('txtCity').value;
            var address = City + Street;
            console.log(address);
            var labelLat = document.getElementById('Latitude');
            var labellong = document.getElementById('longitude');

            geocoder.geocode({ 'address': address },
                function (results, status) {
                    if (status == window.google.maps.GeocoderStatus.OK) {
                        var latitude = results[0].geometry.location.lat();
                        var longitude = results[0].geometry.location.lng();
                 console.log("Latitude: " + latitude + "\nLongitude: " + longitude); //Ok.

                        labelLat.value = latitude; //Ok.
                        labellong.value = longitude;
                        $.ajax({
                            url: 'Create',
                            type: 'POST',
                            data: $('#RestoForm').serialize(),
                            datetype: "html",
                            success: function (data) {
                                $('#RestoForm').html(data);
                            }
                        });

                        error: function e(xhr, ajaxOptions, thrownError) {
                            alert(xhr.status);
                            alert(thrownError);
                        }
                    }
                });
        };
    </script>

controlle `

[Authorize, HttpPost, ValidateAntiForgeryToken]
        public ActionResult Create(LectureFormViewModel viewModel) // RImage =Null
        {`
11
  • datetype typo mistake ? it should be datatype. and use FormData not only serialize Commented Nov 22, 2016 at 12:20
  • Uncaught TypeError: $(...).FormData is not a function(…) data: $('#RestoForm').FormData(), dattype: "html", Commented Nov 22, 2016 at 12:32
  • Can you show LectureFormViewModel ? Commented Nov 22, 2016 at 12:32
  • 3
    Refer stackoverflow.com/questions/21044798/… Commented Nov 22, 2016 at 12:35
  • 1
    RImage should be of type HttpPostedFIleBase ? Commented Nov 22, 2016 at 12:41

1 Answer 1

1

You can use FormData to send files to action using AJAX.

var fm = new FormData();
fm.append("Name", $("#Name").val());
fm.append("RImage", $("#RImage").val());

$.ajax({
    url: 'Create',
    type: 'POST',
    data: fm,
    datatype: "html",
    contentType: false, // Not to set any content header
    processData: false, // Not to process data
    success: function (data) {
        $('#RestoForm').html(data);
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

that gave me 500 error on jquery :xhr.send( options.hasContent && options.data || null );
500 is an HTTP error. It has nothing to do with JavaScript. The server responding to the AJAX errored out. You need to look at your server-side code to determine why.
You must need to append all the properties of model to the formdata. Pleas e check that.
@ChrisPratt Yep, my bed , found it.
Thanks you all , get the answer

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.