У меня есть контроллер с обзором действий, например:
[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);
}
Я пытаюсь вызвать этот метод при вызове RedirectToAction из моего метода Upload:
[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);
}
Кажется, это работает должным образом, в том случае, когда при вызове события Upload, редирект добавляет элементы модели в URL-адрес, который, как я ожидаю, будет сопоставлен с объектом ReviewModel.
Вместо этого вызывается универсальный Review (не HttpPost или HttpGet), а это не то, чего я ожидал. Почему это не сопоставление с моделью и сохранение данных? Что я могу сделать, чтобы основные данные в интерфейсе IImporterFileInfo перешли на следующий экран, как в мастере?
Я также включу структуру модели, чтобы ppl мог видеть, что я пытаюсь сделать.
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; }
}
Я думаю, это должно работать очень хорошо. Не знаю, почему это не так. Строка запроса выглядит так:
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