Javascript Post后将id从控制器传递到脚本

Javascript Post后将id从控制器传递到脚本,javascript,jquery,ajax,asp.net-mvc,Javascript,Jquery,Ajax,Asp.net Mvc,这将在Q之前继续 我试图弄清楚如何将Id传递给link(E>G)。 有人建议我传递它查看json结果,但我不能将它传递回脚本文件。我需要的是在用户提交后将Id传递给url并显示新发布的项目。(例如MyDomain/GolblizionCode/Controller/View/Id)。根据我目前得到的信息,我没有定义if-Id。() 代码:GetAddres.Js` function GetLocation() { var geocoder = new window.google.maps

这将在Q之前继续

我试图弄清楚如何将Id传递给link(E>G)。 有人建议我传递它查看json结果,但我不能将它传递回脚本文件。我需要的是在用户提交后将Id传递给url并显示新发布的项目。(例如MyDomain/GolblizionCode/Controller/View/Id)。根据我目前得到的信息,我没有定义if-Id。() 代码: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

尝试在Json响应中添加允许get

return Json(new {Id = lectureGig.Id}, JsonRequestBehavior.AllowGet); 
希望这有帮助

return Json(new {Id = lectureGig.Id}, JsonRequestBehavior.AllowGet);
或者您可以使用ViewBag

C#

剃刀


“如何通过a是链接”对不起,你是什么意思?请始终确保其他人能够理解您。这并不是唯一一个几乎是胡言乱语的部分…@PeterB,对不起,我编辑了我的Q。它可能更适用于使用API控制器并返回IHttpResponse而不是ActionResult。@不平衡的是,它看起来像,但我更愿意保持原样,除非我别无选择,我也不认为Api可以重定向到Action,我假设您想要这样做,因为您不想在多个类中定义创建函数(这是一个好主意),但是您在这里违反了规则。明智的做法是将创建逻辑移动到外部函数(helper/manager/etc),然后让控制器和API控制器都使用该函数来执行操作,只有它们的请求和响应不同。控制器用于返回视图,而不是数据。
ViewBag.MyIdBag = lectureGig.Id;
@ViewBag.MyIdBag