如何在两个不同的控制器中从一个动作切换到另一个动作 [英] How to rediect from one action to another action within two diferent ccontroller
本文介绍了如何在两个不同的控制器中从一个动作切换到另一个动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码,我想将一些数据(这里的日历日期)发布到home控制器的索引操作,并希望将这些数据保存在数据库中,同时我想重定向到另一个另一个控制器的动作,即Home控制器的Index动作
这里是我的jquery代码,
This is my code where I want to post some data(date of calendar here) to "index" action of "home" controller and want to save this data in the database and at the same time I want to redirect to another action of another controller i.e "Index" action of "Home" controller
here is my jquery code below,
function getDate() {
$('.cal_daybox').on('click', function () {
var timestamp = parseInt($(this).attr('id'));
var day = new Date(timestamp);
alert("You clicked " + day.toDateString());
var url = '@Url.Content("~/Home/Index")';
var date = day.toDateString();
$.ajax({
type: "POST",
url: url,
data: { date: day.toDateString() },
dataType:"json"
});
return false;
});
EventsController.cs
EventsController.cs
public ActionResult Index()
{
return View(db.Events.ToList());
}
HomeController.cs
HomeController.cs
[HttpPost]
public ActionResult Index(DateTime date)
{
Date dt = new Date();
dt.StartDate = date;
db.Dates.Add(dt);
db.SaveChanges();
return RedirectToAction("Index", "Events", new { id = dt.DateID });
}
推荐答案
(' .cal_daybox')。on(' click', function (){
var timestamp = parseInt (
('.cal_daybox').on('click', function () { var timestamp = parseInt(
( this )。attr(' id'));
var day = new 日期(时间戳);
alert( 您单击 + day.toDateString());
var url = ' @ Url.Content(〜/ Home / Index)';
var date = day.toDateString();
(this).attr('id')); var day = new Date(timestamp); alert("You clicked " + day.toDateString()); var url = '@Url.Content("~/Home/Index")'; var date = day.toDateString();
.ajax({
type : POST,
url:url,
data:{date: day.toDateString()},
dataType: json
}) ;
return false ;
});
.ajax({ type: "POST", url: url, data: { date: day.toDateString() }, dataType:"json" }); return false; });
EventsController.cs
EventsController.cs
public ActionResult Index()
{
return View(db.Events.ToList());
}
HomeController.cs
HomeController.cs
[HttpPost]
public ActionResult Index(DateTime date)
{
Date dt = new Date();
dt.StartDate = date;
db.Dates.Add(dt);
db.SaveChanges();
return RedirectToAction("Index", "Events", new { id = dt.DateID });
}
这篇关于如何在两个不同的控制器中从一个动作切换到另一个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文