在Post之后将id从控制器传递给脚本

时间:2017-01-04 15:30:27

标签: javascript jquery ajax asp.net-mvc

这是Orginal q

之前的Q继续

我正试图弄清楚如何将Id传递给链接(E> G http://localhost:1914/en/Events/Index/22)。 我被建议通过它查看json结果buti cantpass回到我的脚本文件我需要的是用户提交后将Id传递给url并显示新发布的项目。 (EG MyDomain / GolblizionCode / Controller / View / Id)。到目前为止,我得到了未定义的if(。http://localhost:1914/EN/Events/Indexundefined) 代码:GetAddres.Js`

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;
                var form = $('#RestoForm')[0];
                var formData = new FormData(form);

                var getUrl = window.location;
                var baseUrl = getUrl.protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];

                $.ajax({
                    url: 'Create',
                    type: 'POST',
                    contentType: false,
                    processData: false,
                    data: formData,
                    datatype: "JSON",

                    success: function(data) {
                        $('#RestoForm').html(data);

                        var id = data.id;
                        console.log(id);
                        window.location.href = baseUrl + '/Events/Index' + id;
                        console.log(baseUrl + '/Events/Index' + id);
                    }
                    });

                error: function e(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            }
        });
};`  

控制器:`

public ActionResult Create()
        {
            var viewModel = new LectureFormViewModel
            {
                Genres = _context.Genres.ToList(),
            };
            return View("Gigform", viewModel);
        }

        [Authorize, HttpPost]
        public JsonResult Create(LectureFormViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                viewModel.Genres = _context.Genres.ToList();
                //return View("Gigform", viewModel);
            }

            var lectureGig = new LectureGig
            {
                //Parmeters
            };

            _context.LectureGigs.Add(lectureGig);
            _context.SaveChanges();

            return Json(new {Id = lectureGig.Id});
        }
    }`
 Thanks in advance

2 个答案:

答案 0 :(得分:0)

尝试添加允许获取Json响应

return Json(new {Id = lectureGig.Id}, JsonRequestBehavior.AllowGet); 

希望这有帮助

答案 1 :(得分:0)

return Json(new {Id = lectureGig.Id}, JsonRequestBehavior.AllowGet);

或者您可以使用ViewBag

C#

ViewBag.MyIdBag = lectureGig.Id;

剃刀

@ViewBag.MyIdBag