将字典对象与其他值一起发送到Controller到View

时间:2013-05-09 06:57:41

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

我必须从视图中将多个值传递给控制器​​。我希望发送到视图的两个值是Dictionary对象和数据库中多个表的值。请帮助。

    public ActionResult Overview(int id)
    { 
      //Feb runs
        ScriptRun run1 = new ScriptRun("IE","Tech2",DateTime.Now.AddMonths(-1));
        ScriptRun run2 = new ScriptRun("IE","Tech2",DateTime.Now.AddMonths(-1));

        //Jan runs
        ScriptRun run3 = new ScriptRun("IE","Tech2",DateTime.Now.AddMonths(-2));
        ScriptRun run4 = new ScriptRun("IE","Tech2",DateTime.Now.AddMonths(-2));

        //March runs
        ScriptRun run5 = new ScriptRun("IE","Tech2",DateTime.Now.AddMonths(0));
        ScriptRun run6 = new ScriptRun("IE","Tech2",DateTime.Now.AddMonths(0));

        //April runs
        ScriptRun run7 = new ScriptRun("IE","Tech2",DateTime.Now.AddMonths(+1));
        ScriptRun run8 = new ScriptRun("IE","Tech2",DateTime.Now.AddMonths(+1));


        List<ScriptRun> janRuns = new List<ScriptRun>();
        janRuns.Add(run3);
        janRuns.Add(run4);

         List<ScriptRun> febRuns = new List<ScriptRun>();
        febRuns.Add(run1);
        febRuns.Add(run2);

        List<ScriptRun> marRuns = new List<ScriptRun>();
        marRuns.Add(run5);
        marRuns.Add(run6);

        List<ScriptRun> aprRuns = new List<ScriptRun>();
        aprRuns.Add(run7);
        aprRuns.Add(run8);
        aprRuns.Add(run7);
        aprRuns.Add(run8);

        Dictionary<string,List<ScriptRun>> runs = new Dictionary<string,List<ScriptRun>>();
        runs.Add("January",janRuns);
        runs.Add("February", febRuns);
        runs.Add("March", marRuns);
        runs.Add("April", aprRuns);

        var script_tbl = db.ScriptDb.Include(x => x.ScriptRuns);
        var inreturn1= script_tbl.ToList().Find(p => p.scriptId == id);

        //Wish to return inreturn1 and  runs

        return View();



    }

1 个答案:

答案 0 :(得分:0)

在这种情况下,最好的方法是发回强类型模型。

e.g。 (非常简化的例子)

var myModel = new OverViewModel(); //Your new model type
myModel.Runs = runs;
myModel.Return1 = inreturn1;

return View(myModel);

...然后参考视图中的模型

@model OverViewModel