这个Q.是这个问题的继续first qestion我有一个表单需要重定向到表单上传的详细信息(就像在stackoverflow中一样),但显然是因为我正在使用Ajax,它防止它重定向,我试图添加我的Ajax重定向但我不断收到错误和错误的网址。它应该从http://localhost:1914/En/VoosUp/Create重定向到http://localhost:1914/En/events/Index/42(E.G. Id号码)我得到的结果是我在Js代码中得到的结果 如何将其重定向到正确的URL(事件/索引/ 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);
}
答案 0 :(得分:1)
如果您的JavaScript位于cshtml中,则应该这样做:
var id = 1;
var url = '@Url.Action("Index", "Events")';
window.location.href = url + "/" + id;
根据你的描述,似乎你试图从一个不了解MVC助手的.js文件中调用它,所以你可以尝试这样的事情:
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,例如
public JsonResult Create(LectureFormViewModel viewModel)
{
return Json(new { Id=lectureGig.Id });
}
然后您可以在JavaScript中访问此ID,例如
success: function(data) {
var id = data.Id;
}
答案 1 :(得分:0)
如果它在cshtml文件中,您可以使用代码
window.location.href = '@Url.Action("Index", "Events")'+ "?id=1";
如果您在js文件中
window.location.href='/Events/Index?id=1'