window.location.href错误的脚本 [英] script for window.location.href error

查看:199
本文介绍了window.location.href错误的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是该问题的继续 http://localhost:1914/En/VoosUp/Create 重定向到 http://localhost:1914/En/events/Index/42 (例如,EG ID号)我用我写的东西得到的结果在我的Js代码中 如何将其重定向到正确的网址(事件/索引/编号) 提前致谢

this Q. is a continue of this question first qestion I have a form that I need to redirect to action with the form uploaded details (just like In stackoverflow) , but apparently because i'm using Ajax , it prevent it from redirect , I have tried to add my Ajax a redirect but I keep getting error and wrong Url . it should redirect from http://localhost:1914/En/VoosUp/Create To http://localhost:1914/En/events/Index/42 (E.G. Id number) the results i'm getting with what I wrote are in my Js Code How can I redirect it to the correct url (Events/Index/Id ) Thanks in advance 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);
                $.ajax({
                    url: 'Create',
                    type: 'POST',
                    contentType: false,
                    processData: false,
                    data: formData,
                    datatype: "html",
                    success: function(data) {
                        $('#RestoForm').html(data),
                            //   window.location.href = '@Url.Content:("~/Events/Index")' + "Id";
                            //= A potentially dangerous Request.Path value was detected from the client (:).
                            // En/VoosUp/@Url.Content:("~/Events/Index")Id
                            window.location.href = '@Url.Action("~/Events/Index")';
                        // =The resource cannot be found >
                        //En/VoosUp/@Url.Action("Events/Index 

                    }

                });

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

控制器:

 [Authorize]
    public ActionResult Create()
    {
        var viewModel = new LectureFormViewModel
        {
            Genres = _context.Genres.ToList(),

        };
        return View("Gigform", viewModel);
    }

    [Authorize, HttpPost]
    public ActionResult Create(LectureFormViewModel viewModel)
    {

        if (!ModelState.IsValid)
        {
            viewModel.Genres = _context.Genres.ToList();

            return View("Gigform", viewModel);
        }


        var lectureGig = new LectureGig
        {
           //Parameters
        };

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

        // return this.RedirectToAction( "events", (c=>c.Index(lectureGig.Id));
        return RedirectToAction("index", "Events", new { id = lectureGig.Id });
    }

和目标

 public ActionResult Index(int id)
    {
        var lecturegig = _context.LectureGigs.Include(g => g.Artist)
           .Include(g => g.Genre)
           .SingleOrDefault(g => g.Id == id);
        if (lecturegig == null)
            return HttpNotFound();
        var viewmodel = new GigDetailViewModel { LectureGig = lecturegig };

        return View("index", viewmodel);
    }

推荐答案

如果您的JavaScript位于cshtml中,则应该这样做:

This should do it if your JavaScript is in the cshtml:

   var id = 1;
   var url = '@Url.Action("Index", "Events")';
   window.location.href = url + "/" + id;

但是,根据您的描述,您似乎正在尝试从不了解MVC帮助器的.js文件中调用此文件,因此可以尝试执行以下操作:

Based on your description however it seems that you are trying to call this from a .js file which has no idea about MVC helpers so instead you can try something like this:

var id = 1; 

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

window.location.href = baseUrl + '/Events/Index' + id;

要获取正确的ID,请修改操作以返回ID,例如

To get the correct Id modify your action to return the Id e.g.

public JsonResult Create(LectureFormViewModel viewModel)
{
   return Json(new { Id=lectureGig.Id });
}

然后您可以使用JavaScript访问此ID,例如

then you can access this Id in JavaScript e.g.

 success: function(data) {
   var id = data.Id;
 }

这篇关于window.location.href错误的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆