mvc 4访问变量在表单提交后与模型类中的会话一起存储

时间:2013-12-11 17:13:12

标签: c# asp.net-mvc-4

我使用会话变量将一些值存储到模型类中,并希望在表单提交后访问它们。这些值存储在ReportChooser模型中,我想访问View中的值。

报告控制器

List<ReportEntryInfo> reiList = new List<ReportEntryInfo>();
        foreach (string s in Request["ReportPeriodEntries"].ToString().Split(','))
        {
            string[] reportPeriodEntries = s.ToString().Split('_');
            string reportPeriodID = reportPeriodEntries[0];
            string entryID = reportPeriodEntries[1];

            ReportEntryInfo rei = new ReportEntryInfo();
            rei.EntryID = Convert.ToInt16(entryID);
            rei.ReportPeriodID = Convert.ToInt16(reportPeriodID);
            rei.ReportPeriodName = (from rp in db.ReportPeriods where rp.ReportPeriodID == rei.ReportPeriodID select rp.ReportPeriodName).FirstOrDefault();

            reiList.Add(rei);
        }

        rc.ReportPeriodEntries = reiList;

        List<ReportSchoolInfo> rsiList = new List<ReportSchoolInfo>();
        foreach (string s in Request["Schools"].ToString().Split(','))
        {
            ReportSchoolInfo rsi = new ReportSchoolInfo();
            rsi.SchoolID = Convert.ToInt16(s);

            List<School> sList = db.Schools.Where(sc => sc.SchoolID == rsi.SchoolID && sc.IsActive == true).ToList();

            foreach (School sl in sList)
            {
                rsi.SchoolName = sl.SchoolName;
                rsi.SchoolLevelID = sl.SchoolLevelID;
                rsi.DistID = sl.DistID;
                rsi.DistName = sl.District.DistName;
            }

            rsiList.Add(rsi);
        }

        rc.Schools = rsiList;

        rc.IndicatorID = indicatorID;

        if (form["ComparisonSideBySide"] == "School")
            rc.ComparisonSideBySide = ComparisonSideBySideValue.School;
        else
            rc.ComparisonSideBySide = ComparisonSideBySideValue.Entry;

        rc.IncludeNarrative = Convert.ToBoolean(form["IncludeNarrative"]);

        if (ModelState.IsValid)
        {
            if (indicatorID == 1)
            {
                if (rc.ComparisonSideBySide == ComparisonSideBySideValue.School)
                    return RedirectToAction("Leadership_sch");
                else
                    return RedirectToAction("Leadership_entry");

            }
            else
            {
                ViewBag.ReportChooser = rc;
                return View();
            }
        }
        else
        {
            ViewBag.ReportChooser = rc;
            return View();
        }

查看

@model IEnumerable<ImpactIndicator.Models.ReportDataModel>

@{
ViewBag.Title = "Report";
ImpactIndicator.Models.ReportChooser reportChooser = ViewBag.ReportChooser;

}
<hgroup class="title">
<h2>Report: Leadership</h2>
</hgroup>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()


}

ReportChooser模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace ImpactIndicator.Models
{
public enum ComparisonSideBySideValue { School, Entry };

public class ReportChooser
{

    public int IndicatorID
    {
        get
        {
            if (HttpContext.Current.Session["ReportChooser_IndicatorID"] != null)
                return Convert.ToInt16(HttpContext.Current.Session["ReportChoose_IndicatorID"]);
            else
                return 0;
        }
        set
        {
            HttpContext.Current.Session["ReportChooser_IndicatorID"] = value;

        }
    }

    public List<ReportSchoolInfo> Schools
    {
        get
        {
            if (HttpContext.Current.Session["ReportChooser_SchoolInfo"] != null)
            {
                List<ReportSchoolInfo> schools=(List<ReportSchoolInfo>)HttpContext.Current.Session["ReportChooser_SchoolInfo"];
                schools.OrderBy(t=>t.DistName).ThenBy(t=>t.SchoolName);
                return schools;
            }
            else
                return null;
        }
        set
        {
            HttpContext.Current.Session["ReportChooser_SchoolInfo"] = value;

        }
    }

    public int SchoolLevelID
    {
        get
        {
            if (HttpContext.Current.Session["ReportChooser_SchoolLevelID"] != null)
                return Convert.ToInt16(HttpContext.Current.Session["ReportChoose_SchoolLevelID"]);
            else
                return 0;
        }
        set
        {
            HttpContext.Current.Session["ReportChooser_SchoolLevelID"] = value;

        }
    }


    public List<ReportEntryInfo> ReportPeriodEntries
    {
        get
        {
            if (HttpContext.Current.Session["ReportChooser_ReportPeriodEntries"] != null)
                return (List<ReportEntryInfo>)HttpContext.Current.Session["ReportChooser_ReportPeriodEntries"];
            else
                return null;
        }
        set
        {
            HttpContext.Current.Session["ReportChooser_ReportPeriodEntries"] = value;

        }
    }

    public bool IncludeNarrative
    {
        get
        {
            if (HttpContext.Current.Session["ReportChooser_IncludeNarrative"] != null)
                return Convert.ToBoolean(HttpContext.Current.Session["ReportChooser_IncludeNarrative"]);
            else
                return false;
        }
        set
        {
            HttpContext.Current.Session["ReportChooser_IncludeNarrative"] = value;

        }
    }

    public ComparisonSideBySideValue ComparisonSideBySide
    {
        get
        {
            if (HttpContext.Current.Session["ReportChooser_ComparisonSideBySide"] != null)
                return (ComparisonSideBySideValue)HttpContext.Current.Session["ReportChooser_ComparisonSideBySide"];
            else
                return ComparisonSideBySideValue.Entry;
        }
        set
        {
            HttpContext.Current.Session["ReportChooser_ComparisonSideBySide"] = value;

        }
    }


    public ReportChooser()
    {

    }
}
}

ReportDataModel模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ImpactIndicator.Models
{

public class ReportDataModel
{
    public int IndicatorID { get; set; }
    public int ReportPeriodID { get; set; }
    public int EntryID { get; set; }
    public int SchoolID {get; set;}
    public bool IsSchoolRequiredToReport { get; set; }
    public int? SchoolEntryID { get; set; } 

    public int? IndDomainID {get; set;}
    public int? IndLevelID {get; set;}
    public int? IndQuestionID {get; set;}
    public string Response { get; set; }

    public ReportDataModel()
    {
    }
}
}

1 个答案:

答案 0 :(得分:1)

您可以使用此

访问存储在会话中的变量
var variable = (Cast the type of variable)HttpContext.Current.Session["the key"];