【发布时间】:2017-05-18 12:56:28
【问题描述】:
这个问题是这个问题的继续first qestion 我有一个表单,我需要使用表单上传的详细信息重定向到操作(就像在 stackoverflow 中一样),但显然因为我使用的是 Ajax,它阻止了它从重定向,我试图添加我的 Ajax 重定向,但我不断收到错误和错误的 Url。它应该从 http://localhost:1914/En/VoosUp/Create 重定向到 http://localhost:1914/En/events/Index/42 (例如 ID 号)我用我写的内容得到的结果在我的 Js 代码中 如何将其重定向到正确的 url (Events/Index/Id) 提前致谢 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);
}
【问题讨论】:
-
'@Url.Action("Index", "Events")'; -
@Satpal localhost:1914/En/VoosUp/… > window.location.href= '@Url.Action("Index", "Events")';也不适合我
-
试试这个 - window.location.href= @String.Concat("'", Url.Action("Index", "Events"), "'");
-
@majita 它在某处缺少引用&我找不到 >Url 未定义的位置
-
只是一个愚蠢的问题:Ajax 旨在避免重定向。您想要做的事情可以通过简单的表单提交来完成。为什么需要模拟纯表单提交(用Ajax发帖,然后重定向)?
标签: javascript jquery ajax asp.net-mvc redirect