HttpGet Attribute ignored on RedirectToAction?
I have a controller with an action Review like so:
[HttpPost, HttpGet]
[RoleRequired("Payroll Tech")]
public ActionResult Review(ReviewModel model)
{
PayrollManagement.Importer importer = new PayrollManagement.Importer(
ConfigurationManager.ConnectionStrings["ImportDatabase"].ConnectionString,
model);
model.Discrepancies = importer.GetDiscrepancyReport();
return View(model);
}
[RoleRequired("Payroll Tech")]
public ActionResult Review()
{
ReviewModel model = new ReviewModel();
model.Initialize(); //initializes the dropdown collections
model.Discrepancies = new List<DiscrepancyReportItem>();
return View(model);
}
I am trying to invoke this method on a RedirectToAction call from my Upload method:
[HttpPost]
[RoleRequired("Payroll Tech")]
public ActionResult Upload(UploadModel model)
{
string tmpFile = Path.GetTempFileName();
model.File.SaveAs(tmpFile);
PayrollManagement.Importer importer = new PayrollManagement.Importer(
ConfigurationManager.ConnectionStrings["ImportDatabase"].ConnectionString,
model);
importer.UploadExcelDocument(tmpFile);
return RedirectToAction("Review", (IImporterFileInfo)model);
}
This seems to work appropriately, in that when the Upload event is called, the redirect adds the model elements to the URL, which I would expect to be mapped to the ReviewModel object.
Instead, the generic Review (not HttpPost or HttpGet) is invoked, which is not what I would expe开发者_运维知识库ct. Why is it not mapping to the model and persisting the data? What can I do to make the core data in the IImporterFileInfo interface carry on to the next screen, wizard-like?
I will include the model structure as well, so ppl can see what I'm trying to do.
public class BaseModel : IImporterFileInfo
{
[Display(Name = "Financial System:")]
public string FinancialSystem { get; set; }
[Display(Name = "Fiscal Year:")]
public int FiscalYear { get; set; }
[Display(Name = "Fiscal Week:")]
public int FiscalWeek { get; set; }
[Display(Name = "Accrual File?")]
public bool IsAccrual { get; set; }
[Display(Name="Accrual Month:")]
public int AccrualMonth { get; set; }
public void Initialize()
{
this.FinancialSystem = "Legacy";
this.FiscalWeek = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
this.FiscalYear = DateTime.Now.Year;
this.AccrualMonth = DateTime.Now.Month;
}
//the readonly lists
private IList<string> _FinancialSystems;
public IList<string> FinancialSystems
{
get
{
if (_FinancialSystems == null)
{
IList<string> choices = new List<string>();
choices.Add("Legacy");
choices.Add("Morris");
_FinancialSystems = choices;
//_FinancialSystems = new SelectList(choices, this.FinancialSystem ?? "Legacy");
}
return _FinancialSystems;
}
}
private IList<int> _FiscalWeeks;
public IList<int> FiscalWeeks
{
get
{
if (_FiscalWeeks == null)
{
IList<int> choices = new List<int>();
for (int i = 1; i <= 53; i++)
{
choices.Add(i);
}
_FiscalWeeks = choices;
}
return _FiscalWeeks;
}
}
private IList<int> _FiscalYears;
public IList<int> FiscalYears
{
get
{
if (_FiscalYears == null)
{
IList<int> choices = new List<int>();
for (int i = DateTime.Now.Year - 7; i <= DateTime.Now.Year + 1; i++)
{
choices.Add(i);
}
_FiscalYears = choices;
}
return _FiscalYears;
}
}
private IList<int> _AccrualMonths;
public IList<int> AccrualMonths
{
get
{
if (_AccrualMonths == null)
{
IList<int> choices = new List<int>();
for (int i = 1; i <= 12; i++)
{
choices.Add(i);
}
_AccrualMonths = choices;
}
return _AccrualMonths;
}
}
}
public class UploadModel : BaseModel
{
[Display(Name = "File Name:")]
public HttpPostedFileBase File { get; set; }
}
public class ReviewModel : BaseModel
{
public IList<DiscrepancyReportItem> Discrepancies { get; set; }
}
I think this should work pretty well. Dunno why it doesn't. The query string looks like:
https://localhost:44301/Import/Review?File=System.Web.HttpPostedFileWrapper&FinancialSystem=Legacy&FiscalYear=2011&FiscalWeek=33&IsAccrual=True&AccrualMonth=8&FinancialSystems=System.Collections.Generic.List`1%5BSystem.String%5D&FiscalWeeks=System.Collections.Generic.List`1%5BSystem.Int32%5D&FiscalYears=System.Collections.Generic.List`1%5BSystem.Int32%5D&AccrualMonths=System.Collections.Generic.List`1%5BSystem.Int32%5D
You will need to create a ReviewModel instance and pass it on the redirect.
take a look at this question, one option you could do is use TempData, hold the model or whatever you will need in the next call
MVC - Passing Data with RedirectToAction()
You are passing upload model to a method which is expecting a ReviewModel. You need to pass a ReviewModel.
精彩评论