Asp.net mvc Ajax不允许视图渲染
根据页面上的交互流,我在ajax上有几个变体。但改变的只是变量。以下是其中之一:Asp.net mvc Ajax不允许视图渲染,asp.net-mvc,jquery,redirect,Asp.net Mvc,Jquery,Redirect,根据页面上的交互流,我在ajax上有几个变体。但改变的只是变量。以下是其中之一: $('#btn_skickaEnkel').bind('click', function () { $.ajax({ type: 'POST', url: '/Contact/IntresseAnmälan/', dataType: 'json', data: { Namn
$('#btn_skickaEnkel').bind('click', function () {
$.ajax({
type: 'POST',
url: '/Contact/IntresseAnmälan/',
dataType: 'json',
data: {
Namn: $('#namn').val(),
Mail: $('#mail').val(),
Info: $('#meddelande').val(),
Nivå: $('#nivå').find(":selected").text(),
IsEnkel: true,
Telefon: $('#nr').val(),
ID: function () {
var url = window.location.pathname;
var id = url.substring(url.lastIndexOf('/') + 1);
return id;
}
},
traditional: true
});
});
在我的控制器中,我无法重定向或返回不同的视图。此时,来自JSON的数据不再相关,因为它已经保存到DB中
我的控制器:
public ActionResult IntresseAnmälan(BokningContainer bokning)
{
db = new DbContext();
//Saving some data to database(removed)
//Just determening the state of container obj.
if (bokning.IsEnkel)
{
//Geting som information from db (removed)
//Creating a mail (removed)
email.Send(bokning.Namn, bokning.Mail, body);
}
else
{
}
//db.SaveChanges();
//This part is not working, I think it's beacuase of the Ajax
return View("IntresseAnmälan");
}
视图没有呈现,我认为它与ajax有关。视图只是没有渲染。有没有办法强制返回它而忽略ajax?正如我所说,不再需要数据,因为内容已经保存到数据库中 您不能在ajax调用时呈现视图,只需使用form post方法,或者在ajax调用成功时将其重定向到所需的操作,如下所示:
$('#btn_skickaEnkel').bind('click', function () {
$.ajax({
type: 'POST',
url: '/Contact/IntresseAnmälan/',
dataType: 'json',
data: {
Namn: $('#namn').val(),
Mail: $('#mail').val(),
Info: $('#meddelande').val(),
Nivå: $('#nivå').find(":selected").text(),
IsEnkel: true,
Telefon: $('#nr').val(),
ID: function () {
var url = window.location.pathname;
var id = url.substring(url.lastIndexOf('/') + 1);
return id;
}
},
traditional: true,
success: function(result) {
window.location.href = '@Url.Action("action", "Controller")';
}
});
});
当我发现这个虫子时,我简直不敢相信自己的眼睛。问题是,在某个时候,我将提交更改为一个按钮。因此,表格从未提交。至少我学到了一些视图和Ajax
抱歉耽误了你的时间 我试过你的解决方案,但它不能解决我的问题。即使没有->成功:实现,我仍然可以访问控制器。我甚至渲染了_Layout.cshtml,但当我在_Layout.cshtml中看到@RenderBody部分时,它根本就没有渲染。