如何在mvc3中从控制器返回两个变量到视图
本文关键字:两个 变量 视图 返回 mvc3 控制器 | 更新日期: 2023-09-27 17:53:46
嗨,我是MVC3的新手,我在返回两个变量时有问题,这里是我的控制器代码
public ActionResult Create()
{
using (var context = new material_managementEntities())
{
var blogs = (from cs in context.GROUP_MASTER
where cs.GROUP_NAME == "MIC"
select cs.ID).FirstOrDefault();
var droplist = new TRANSACTION();
droplist.GROUP_MASTER_ID = blogs;
droplist.GROUP_NAME = "MIC";
var directory_master = db.DIRECTORY_MASTER.Include(d => d.CATEGORY_MASTER).Include(d => d.REPRESENTATIVE_MASTER);
droplist.dir_data = directory_master.ToList();
return View(droplist);
}
}
这里事务是一个表,DIRECTORY_MASTER是另一个,GROUP_MASTER_ID是事务表的外键,我想要的是toList数据将显示在一个模态弹出框中,所以我需要传递来自两个表的可用数据
这里是模型
namespace Material.Models
{
using System;
using System.Collections.Generic;
using System.Web.Mvc;
public partial class TRANSACTION
{
public int ID { get; set; }
public int GROUP_MASTER_ID { get; set; }
public string GROUP_NAME { get; set; }
public string dir_data { get; set; }
public string NAME { get; set; }
public string ADDRESS_DETAILS { get; set; }
public string CONTACT_PERSON { get; set; }
public Nullable<int> TEL_NO { get; set; }
public Nullable<int> CELL { get; set; }
public Nullable<int> FAX { get; set; }
public string EMAIL_ID { get; set; }
public Nullable<int> OPENING_BALANCE { get; set; }
public Nullable<System.DateTime> OPENING_BALANCE_DATE { get; set; }
public string VERIFIED { get; set; }
public virtual GROUP_MASTER GROUP_MASTER { get; set; }
}
}
你可以使用
viewbag.directory_master = (db.DIRECTORY_MASTER.Include(d => d.CATEGORY_MASTER).Include(d => d.REPRESENTATIVE_MASTER)).ToList();
,然后从你的模型中迭代你的viewbag.directory_master
,并显示所需的。
或者您可以创建包含directory_master
和TRANSACTION
的自定义视图模型,如
public class DashboardModel
{
public TRANSACTION transation{get;set;}
public List<directory_master>directoryMaster{get;set;}
}
所以你的动作会像这样
public ActionResult Create()
{
using (var context = new material_managementEntities())
{
var blogId = (from cs in context.GROUP_MASTER
where cs.GROUP_NAME == "MIC"
select cs.ID).FirstOrDefault();
DashboardModel dashboardModel= new DashboardModel();
dashboardModel.transation.GROUP_MASTER_ID = blogId;
dashboardModel.transation.GROUP_NAME = "MIC";
dashboardModel.directoryMaster = (db.DIRECTORY_MASTER.Include(d => d.CATEGORY_MASTER).Include(d => d.REPRESENTATIVE_MASTER)).ToList();
return View(dashboardModel);
}
}
我强烈建议你的MVC视图采用你需要的类型的模型,并通过控制器的动作方法的返回函数中的参数将该模型传递给视图。