Reputation: 1583
I am developing a crud app in asp.net core , From Add ActionResult after adding record when I use RedirectToAction then it does not redirect to that action.
It does redirect when request is GET, but in case of POST it does not.
Below is my code:
public IActionResult Add(AuditSchedules model)
{
try
{
int ScheduleID;
int TotalDays;
DateTime start;
List<SelectListItem> li = new List<SelectListItem>();
TempData["state"] = util.Get_State();
TempData["branch"] = li;
TempData["Catg"] = ctx.CategoryMaster.Select(x => new SelectListItem { Text = x.CategoryName, Value = x.CategoryId.ToString() }).ToList();
int count = 0;
if (HttpContext.Request.Method == "POST")
{
using (var ctx = new QuestionnaireEntities())
{
count = ctx.AuditSchedules.Where(x => (x.State == model.State) &&
(x.Branch == model.Branch) &&
(x.StaffId == model.StaffId) &&
(x.FromDate == model.FromDate)).Count();
}
if (count <= 0)
{
TotalDays = (int)(model.ToDate.Subtract(model.FromDate)).TotalDays + 1;
start = model.FromDate;
model.CreatedBy = HttpContext.Session.GetString("UserName");
model.Icon = util.GetIcon(model.CategoryId);
model.CreatedDate = DateTime.Now;
model.IsCompleted = "N";
using (var ctx = new QuestionnaireEntities())
{
ctx.AuditSchedules.Add(model);
//ctx.SaveChanges();
ScheduleID = model.ScheduleId;
for (int i = 0; i < TotalDays; i++)
{
AuditSubSchedule subModel = new AuditSubSchedule();
subModel.ScheduleId = ScheduleID;
subModel.ScheduleDate = start;
ctx.AuditSubSchedule.Add(subModel);
//ctx.SaveChanges();
start.AddDays(1);
}
}
TempData["Msg"] = "Schedule For " + model.StaffName + " Created Successfully";
return RedirectToAction("Index", "Schedules");
}
else
{
TempData["Msg"] = "Schedule AllReady Exist , Try a Different Combination <br> [ Hint : Different State , Branch , StaffID , Fromdate]";
return RedirectToAction("Index", "Schedules");
}
}
}
catch (Exception ex)
{
logger.LogError(ex);
}
finally
{
}
return View(model);
}
After Adding the record I am using return RedirectToAction("Index", "Schedules"); but it does nothing. when request is Get type than it works well , but when I post model into it than it does not redirect.
As a matter of fact it works well in MVC5 (does not matter its a GET request or POST) How can I redirect in ASP.Net Core ?
Update One Before Post Network Tab response
After Post Network Tab Response
Update two
In Network Logs Location of Add is showing not found ,
Upvotes: 0
Views: 605
Reputation: 23864
Your use of TempData
, combined with the fact that ASP.NET Core defaults to using the CookieTempDataProvider
is the cause of your issues.
Basically, you are trying to serialise large objects into cookies. The resulting cookie is too large - resulting in a 500 (which is very hard to diagnose, as you have experienced - since the return RedirectToAction("Index", "Schedules");
call does appear to execute fine).
As such, the RedirectToAction
is a red herring - it is unrelated. The multiple assignment to TempData
is the real cause - likely mainly this one:
TempData["Catg"] = ctx.CategoryMaster.Select(x => new SelectListItem { Text = x.CategoryName, Value = x.CategoryId.ToString() }).ToList();
You may wish to use a different ITempDataProvider
implementation (such as SessionStateTempDataProvider
). Or avoid TempData
altogether.
This issue won't happen with MVC5 since it doesn't use CookieTempDataProvider
.
Upvotes: 3