在MVC 4 /实体框架的自定义查询? [英] Custom queries in MVC 4 / Entity Framework?

查看:101
本文介绍了在MVC 4 /实体框架的自定义查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前使用ASP.net MVC 4和实体框架建立了一个工作的CRUD应用程序。我使用一个EDMX型号为表,这是一个活的服务器上部署了一个数据库。

I currently have a working CRUD application built using ASP.net MVC 4 and the Entity Framework. I use an EDMX model for the tables, which are deployed in a database on a live server.

我想知道如何查询表在我的控制器返回包含每个表(连接)列视图,一个表的基础上,从查询字符串返回的列标题

I want to know how to query the tables in my controllers to return a view which contains columns from each table (join), as one table, based on a column header returned from a query string

这是我的表,我对他们的getter和setter在各自的车型
该模型是Model1.edmx

These are my tables, I have getters and setters for them in their respective models and the model is "Model1.edmx"

AccCompany                             AccControl
    ID                                     ID
    Code                                   ControlCode
    CompanyID                              Nominal
    AccountsCompany                        CostCentre
    Company                                Department

我只是不知道如何加入使用自定义方法表,作为MVC框架。 EF似乎尽一切本身 - 在实际查询方面...

I just don't understand how to join the tables using custom methods, as the MVC framework. EF seemed to do everything itself - in terms of the actual query...

推荐答案

有我喜欢的两种方法。

首先是直线前进,利用实体框架的导航方法:

The first is straight forward, using navigation methods from entity framework:

控制器:

public ActionResult Details(short id = 0)
{
    AccCompany accComp = db.AccCompany.Find(id);
    if (accComp == null)
    {
        return HttpNotFound();
    }
    return View(accComp);
}

查看:

@model Some.Entities.AccCompany

<div class="displayLabel">
     @Html.DisplayNameFor(model => model.Company)
</div>
<div class="displayField">
    @Html.DisplayFor(model => model.Company)
</div>

<div class="displayLabel">
     @Html.DisplayNameFor(model => model.AccControl.CostCentre)
</div>
<div class="displayField">
    @Html.DisplayFor(model => model.AccControl.CostCentre)
</div>

第二个涉及到针对特定视图中创建一个自定义的视图模型,并将它作为在视图模型,用于验证IMO更好的:

The second one involves creating a custom "View Model" for the specific view and using that as your model in the view, better for validations imo:

SomeViewModel.cs:

SomeViewModel.cs:

public class SomeViewModel
{
    [Required]
    public string Company { get; set; }

    [Required]
    [Display(Name = "Cost Centre")]
    public string CostCentre { get; set; }
}

然后填充,在你的控制器:

Then populate that in your controller:

public ActionResult Details(short id = 0)
{
    AccCompany accComp = db.AccCompany.Find(id);

if (accComp == null)
    {
        return HttpNotFound();
    }

SomeViewModel vm = new SomeViewModel();

vm.Company = accComp.Comany;
vm.CostCentre = accComp.AccControl.CostCentre;

    return View(vm);
}

然后在视图:

@model Some.SomeViewModel

<div class="displayLabel">
     @Html.DisplayNameFor(model => model.Company)
</div>
<div class="displayField">
    @Html.DisplayFor(model => model.Company)
</div>

<div class="displayLabel">
     @Html.DisplayNameFor(model => model.CostCentre)
</div>
<div class="displayField">
    @Html.DisplayFor(model => model.CostCentre)
</div>

希望这有助于

这篇关于在MVC 4 /实体框架的自定义查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆