我有MVC项目,我连接到SQL Server中的现有数据库。我想在视图中使用多个模型连接到多个表并从用户获取字段或将它们显示给用户。 我该怎么办?
我尝试使用ViewModel
,但我无法将IEnumerable
列表传递给模型
第一个模型:
public partial class Amniyat
{
public int AmniyatID { get; set; }
public string NamKarbar { get; set; }
public string Ramz { get; set; }
public string KalameBazyabi { get; set; }
public int NoeKarbarID { get; set; }
public System.DateTime TarikhIjad { get; set; }
public System.DateTime TarikhEtebar { get; set; }
public bool VaziyatKarbar { get; set; }
public string Nam { get; set; }
public string Famili { get; set; }
public string Tozihat { get; set; }
}
第二种模式:
public partial class NoeKarbar
{
public int NoeKarbarID { get; set; }
public string NoeKarbar1 { get; set; }
public string NoeDastresi { get; set; }
public string Tozihat { get; set; }
}
我的ViewModel:
public class AmniyatNoeKarbar
{
public IEnumerable<Amniyat> AMN { get; set; }
public IEnumerable<NoeKarbar> NK { get; set; }
}
我想在此使用viewmodel的控制器:
private HousingAgencyEntities db = new HousingAgencyEntities();
public ActionResult Index()
{
NoeKarbar nk = db.NoeKarbars.Find(1);
Amniyat amn = db.Amniyats.Find(1);
AmniyatNoeKarbar mymodel = new AmniyatNoeKarbar { AMN = amn, NK = nk };
return View(mymodel);
}
但我在AMN = amn,NK = nk
的行中有错误答案 0 :(得分:1)
你的.Find()方法是否返回IEnumerable&lt;&gt;?我见过很多Find(int)方法,这些方法只返回无法转换为IEnumerable&lt;&gt;的基础对象。
public ActionResult Index()
{
IEnumerable<NoeKarbar> nk = new IEnumerable<NoeKarbar>.add(db.NoeKarbars.Find(1));
IEnumerable<Amniyat> amn = new IEnumerable<Amniyat>().add(db.Amniyats.Find(1));
AmniyatNoeKarbar mymodel = new AmniyatNoeKarbar { AMN = amn, NK = nk };
return View(mymodel);
}