I'm having a problem with an action in ASP.NET MVC 5.
I submit a form to an URL (/Tickets/ManageChangeTicketStatus/{Id}) responsible for handling the POST and after processing the data I create a redirect because on the end of that POST action there is no view.
But after processing the data and hitting that RedirectToAction my browser still end up on /Tickets/ManageChangeTicketStatus/{Id} instead of the redirect.
The code of the HTML and the actions in the controller will be attached below.
HTML file:
@using UniversityTicketing.Models
<div class="col-md-offset-1 col-md-10">
@using (Html.BeginForm("ManageChangeTicketStatus", "Tickets", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Schimba statusul ticketului</h4>
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Status, new { @class = "col-md-1 control-label" })
<div class="col-md-3">
@Html.EnumDropDownListFor(m=>m.Status, "Selecteaza statusul", new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input type="submit" class="btn btn-primary" formaction="/Tickets/ManageChangeTicketStatus/@ViewData["modelId"]" value="Schimba status" />
</div>
</div>
}
</div>
Controller code:
// GET: Tickets/ManageSingleTicket/{Id}
public ActionResult ManageSingleTicket(int Id)
{
var ticket = DbContext.Tickets
.Include("Department")
.Include("CreatedBy")
.Include("TicketComments.SentBy")
.Where(t => t.Id == Id)
.FirstOrDefault();
var mapper = ClassMapper.Create();
var userTicketVM = mapper.Map<UserTicketViewModel>(ticket);
ticket.LastOpenedByEmployee = DateTime.Now;
DbContext.SaveChanges();
return View(userTicketVM);
}
//
// POST: /Tickets/ManageChangeTicketStatus/{Id}
[HttpPost]
public ActionResult ManageChangeTicketStatus(int Id, UserTicketViewModel model)
{
var ticket = DbContext.Tickets
.Include("Department")
.Include("CreatedBy")
.Include("TicketComments.SentBy")
.Where(t => t.Id == Id)
.FirstOrDefault();
var mapper = ClassMapper.Create();
var employeeTicketVM = mapper.Map<UserTicketViewModel>(ticket);
if (ModelState.IsValid)
{
ticket.Status = model.Status;
ticket.ModifiedTime = DateTime.Now;
ticket.LastOpenedByEmployee = DateTime.Now;
DbContext.SaveChanges();
employeeTicketVM = mapper.Map<UserTicketViewModel>(ticket);
return RedirectToAction("ManageSingleTicket", Id);
}
return View("ManageSingleTicket", employeeTicketVM);
}```
return RedirectToAction("ManageSingleTicket", new {Id=Id});
– Mohammed Sajid