0

this is continue for Q before Orginal q

i'm trying to figure out how to pass Id to link (E>G http://localhost:1914/en/Events/Index/22) . i was suggested to pass it view json results buti cantpass it back to my script file what i need is after user submit to pass the Id to a url and display the new posted item. (EG MyDomain/GolblizionCode/Controller/View/Id) .with what i got so far i have undefined instead if Id .(http://localhost:1914/EN/Events/Indexundefined ) Code: 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);
                }
            }
        });
};`  

controller:`

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
Community
  • 1
  • 1
Assaf Our
  • 611
  • 3
  • 9
  • 25
  • "how to pass a is to link" sorry, what do you mean? Please always make sure that others can understand you. And that isn't the only part that is almost gibberish... – Peter B Jan 04 '17 at 15:39
  • @PeterB ,Sorry , i edit My Q. – Assaf Our Jan 04 '17 at 15:42
  • It may be more applicable to use an API controller and return an IHttpResponse instead of an ActionResult. – Ross Brasseaux Jan 04 '17 at 15:51
  • @Lopsided Yes it look like it but i would prefer to keep it as is unless i will have no other choice , I also don't think an Api can redirectToAction – Assaf Our Jan 04 '17 at 15:55
  • I'm assuming you want to do this because you don't want to define the creation function in multiple classes (which is a good idea), but you are going against the grain here. It would be smart to move your creation logic to an outside function (helper/manager/etc), then have both the Controller and API Controller use that function to perform the action—only their requests and responses would differ. Controllers are meant to return views, not data. – Ross Brasseaux Jan 04 '17 at 16:07
  • @Lopsided , strong point,So youthink if i would refactor it to repository will change anything ? as far as my limited knowledge it should be the same ,just more easy to maintain . – Assaf Our Jan 04 '17 at 16:11
  • Not sure if its the case, but `contentType: false,` does not look fine to me. Shouldn't that be `application/json`? What do you get from the ajax response. Does the code actually jump to the `success` function? can you do `console.log(data)`? Agree that this redirection should happen from the mvc side, not from the jquery side. – David Espino Jan 04 '17 at 17:29
  • @DavidEspino without the contentType:false the form doesn't submit. Yes the data is pass(success) to controller and save .I couldn't agree more that it should happen on Mvc side .Just don't know how. – Assaf Our Jan 05 '17 at 09:18
  • So what do you get on the `data` object?. Apparently if you are using Ajax, the best way to do it is to serve the `redirect url` as part of your response object. Here is a link that gives you the idea: http://stackoverflow.com/questions/28325249/web-api-redirect-to-view-with-model – David Espino Jan 05 '17 at 19:24

2 Answers2

0

Try to add the allow get on the Json Response

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

Hope this helps

David Espino
  • 2,177
  • 14
  • 21
0
return Json(new {Id = lectureGig.Id}, JsonRequestBehavior.AllowGet);

or you can use ViewBag

C#

ViewBag.MyIdBag = lectureGig.Id;

Razor

@ViewBag.MyIdBag