简体   繁体   English

从ViewModel到Controller ViewBag的LINQ查询显示在View中的SelectList中

[英]LINQ query from ViewModel to Controller ViewBag to display in SelectList in View

I am in the process of learning MVC, and it is proving to be quite an admirable foe seeing as how I'm used to web forms.我正在学习 MVC,事实证明这是一个非常令人钦佩的敌人,因为我已经习惯了 Web 表单。

There are so many things I can do in web forms that I can't seem to replicate in MVC, and if I can I'm so lost when it comes to actually implementing it.我可以在 Web 表单中做很多事情,我似乎无法在 MVC 中复制,如果可以的话,我在实际实现它时会很迷茫。 But my question is as follow;但我的问题如下;

I have a table我有一张桌子

public partial class Patient
{
    public Patient()
    {
        this.PatientHousings = new HashSet<PatientHousing>();
    }

    public Guid ID {get;set;}
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public bool Incarcerated {get;set;}
    public bool Released {get;set;}
    public bool Deceased {get;set;}

    public virtual ICollection<PatientHousing> PatientHousings {get;set;}
}

And this is a other table这是另一张桌子

public partial class PatientHousing
{
    public Guid ID {get;set;}
    public Guid PatientID {get;set;}
    public bool CurrentPrevious {get;set;}
    public string Building {get;set;}

    public virtual Patient Patient {get;set;}
}

Okay, so in web forms I would just bind a dropdownlist to a datasource with the proper query.好的,所以在 Web 表单中,我只需将下拉列表绑定到具有正确查询的数据源。 Well I can't do that with MVC, and I have to use the controller which I'm still bashing my head against the wall with.好吧,我不能用 MVC 做到这一点,而且我必须使用控制器,我仍然用它来撞墙。

I've read that I need to create a ViewModel to help me with this, so I've created the following我读过我需要创建一个 ViewModel 来帮助我解决这个问题,所以我创建了以下内容

public class PatientViewModel
{
    public List<Patient> patients {get;set;}

    public PatientViewModel(){
        DATAEntities db = new DATAEntities();
        patients = db.Patients.Include(r => r.PatientHousings.Where(h => h.CurrentPrevious == true)).Where(x => x.Incarcerated == false && x.Released == false && x.Deceased == false && !string.IsNullOrEmpty(x.LastName)).ToList();
    }
}

Now I'm getting this error现在我收到这个错误

The Include path expression must refer to a navigation property defined on the type. Include 路径表达式必须引用类型上定义的导航属性。 Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.对参考导航属性使用虚线路径,对集合导航属性使用 Select 运算符。

Someone please help.有人请帮忙。 I'm so lost right now it isn't even funny.我现在很失落,这甚至都不好笑。

To clarify: What I need to do is, create a select list in a view that lists all patients that are not incarcerated, released, or deceased.澄清一下我需要做的是,在一个视图中创建一个选择列表,其中列出了所有未被监禁、释放或死亡的患者。

I think you cannot use a 'where' inside Include as it isn't one of allowed navigation properties.我认为您不能在 Include 中使用“where”,因为它不是允许的导航属性之一。

https://msdn.microsoft.com/en-us/library/gg671236(v=vs.103).aspx https://msdn.microsoft.com/en-us/library/gg671236(v=vs.103).aspx

Is there a way to defined .Where inside .Include 有没有办法定义 .Where 里面 .Include

You can try using Select or an expression without it and then filter out further using Where.您可以尝试使用 Select 或没有它的表达式,然后使用 Where 进一步过滤掉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有SelectList的ViewModel和具有单个reocrd的LINQ查询结果 - ViewModel with a SelectList and a LINQ Query result with a single reocrd 如何在控制器中显示从Viewbag传递的数据以进行查看 - How do i display data passed from viewbag in controller to view 将控制器方法viewbag中的列表显示到视图中的列表 - Display a list from controller method viewbag to a list in view MVC在没有Viewbag / Viewdata的控制器上显示视图 - MVC display records on view from controller without Viewbag/Viewdata 无法显示从控制器到视图的LINQ查询输出值 - Unable to display LINQ query output values from controller to view 在View中的BeginForm Post方法中将ViewBag ViewModel返回给Controller - Return a ViewBag ViewModel back to Controller in BeginForm Post method in the View 从模型到控制器的DateTime选择列表 - DateTime Selectlist from model to view to controller 将viewbag从动作控制器传递到局部视图 - Pass viewbag to partial view from action controller 将列表从 Controller 传递到使用 ViewBag 进行查看 - Pass List From Controller to View Using ViewBag 在 ASP.NET Core 视图中显示来自控制器的视图模型数据 - Display viewmodel data from controller in view in ASP.NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM