1

我必须从视图将多个值传递给控制器​​。我希望发送到视图的两个值是 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();



    }
4

1 回答 1

0

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

例如(非常简化的示例)

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

return View(myModel);

...然后在您的视图中引用模型

@model OverViewModel
于 2013-05-09T10:20:01.507 回答