RedirectToAction上忽略了HttpGet属性?

时间:2011-08-31 15:50:57

标签: asp.net-mvc asp.net-mvc-3

我有一个带有动作评论的控制器,如下所示:

    [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调用此方法:

    [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

3 个答案:

答案 0 :(得分:1)

您需要创建一个ReviewModel实例并将其传递给重定向。

答案 1 :(得分:0)

您正在将上传模型传递给期望ReviewModel的方法。您需要传递ReviewModel。

答案 2 :(得分:0)

看看这个问题,你可以做的一个选择是使用TempData,保持模型或下次调用时你需要的任何东西

MVC - Passing Data with RedirectToAction()