在视图中呈现数据之前在控制器上过滤数据 [英] Filtering the data at the controller before it is rendered in a view

查看:68
本文介绍了在视图中呈现数据之前在控制器上过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 您好,我是MVC5,Razor和EF的新手",而且我已经寻找了两天,但仍然找不到解决我问题的方法.
  2. 我想做的是查看用户输入年份,季度和部门的视图.在提交时,我希望另一个视图的控制器可以看到这些参数并在呈现视图之前过滤数据.目前,我有5种不同的划分,并且在渲染视图时我只想过滤一个划分.
  3. 我浏览了很多论坛,网站等,试图弄清楚这一点,但我没有任何运气.我很高兴至少能指出正确的方向.我试图通过跳入火中并自己弄清楚这一点来学习这一点,但是我现在需要帮助.
  4. 我完全了解MVC的工作原理,我在使用DB时没有任何问题,并且在学习脚手架和ViewModels的工作原理方面取得了成功.我现在正在尝试学习如何在控制器和视图中操作数据.任何帮助将不胜感激.
  5. 查看1-只需输入参数

  1. Hello, I am very new to MVC5, Razor, and EF and I have been looking for two days and still can't figure out a solution to my problem.
  2. What I want to do is have a view where users enter a year, the quarter, and division. On submit, I want a controller for another view to see these parameters and filter the data before the view is rendered. Currently I have 5 different division and I want to filter only one division when the view is rendered.
  3. I have looked at a lot of forums, websites, etc. trying to figure this out and I haven't had any luck. I would be glad to at least get pointed in the right direction. I am trying to learn this by jumping into the fire and figuring it out myself but I need help now.
  4. I have the whole idea down behind how MVC works, I have no problems working with the DB, and I have been successful on learning how scaffolding works and also ViewModels. I am now trying to learn how to manipulate the data within the controller and views. Any help would be appreciated.
  5. View 1 - Just to enter parameters

<p> Enter Year: @Html.TextBox("Year")</p>
<p> Enter Quarter: @Html.TextBox("Qtr")</p> 
<p> Enter Division: @Html.TextBox("Div")</p>
<p><input id="Submit" type="button" value="button" /></p>

  • 用于View 2的控制器

  • Controller for View 2

    namespace BSIntranet.Controllers
    {
        public class DivisionIncomeController : Controller
        {
            private ProjectionsEntities db = new ProjectionsEntities();
    
            // GET: DivisionIncome
            public ActionResult Index()
            {
                return View(db.JobRecaps.ToList());
            }
        }
    }
    

  • 我不知道从这里开始什么或如何开始.谢谢您的帮助!

    I don't know what or how to get started here. Thanks for your help!!

    编辑 使用系统; 使用System.Collections.Generic;

    EDIT using System; using System.Collections.Generic;

    public partial class JobRecap
    {
        public int ID { get; set; }
        public string Job_ID { get; set; }
        public int Year { get; set; }
        public int Qtr { get; set; }
        public string Div { get; set; }
        public string PreparedBy { get; set; }
        public string ReviewedBy { get; set; }
        public Nullable<System.DateTime> Date { get; set; }
        public Nullable<System.DateTime> ProjStart { get; set; }
        public Nullable<System.DateTime> ProjComp { get; set; }
        public string SvgsSplit { get; set; }
        public Nullable<int> OwnerSplit { get; set; }
        public Nullable<int> BSSplit { get; set; }
        public string JointVent { get; set; }
        public Nullable<int> BSPct { get; set; }
        public string ContractType { get; set; }
        public string ContractWritten { get; set; }
        public Nullable<decimal> CurContrAmt { get; set; }
        public string FeeBasis { get; set; }
        public Nullable<decimal> EstTotFeePct { get; set; }
        public Nullable<decimal> EstTotFeeAmt { get; set; }
        public string PreconFeeBasis { get; set; }
    }
    

    推荐答案

    为简单起见,您可以在Index操作中添加int? Year, int? Qtr, string Div参数并使用它们进行搜索:

    To keep things simple you can simply add int? Year, int? Qtr, string Div parameters to your Index action and search using them:

    public ActionResult Index(int? Year, int? Qtr, string Div)
    {
        var data= db.JobRecaps.AsQueryable();
        if(Year.HasValue)
            data= data.Where(x=>x.Year == Year);
        if(Qtr.HasValue)
            data= data.Where(x=>x.Qtr == Qtr );
        if(!string.IsNullOrEmpty(Div))
            data= data.Where(x=>x.Div == Div );   
    
        return View(data.ToList());
    }
    

    注意:

    此外,您可以分离问题并创建一个包含那些搜索参数的JobRecapSearchModel并将其用作操作参数,还可以创建一个包含List<JobRecap> Search(JobRecapSearchModel searchMode)方法的JobRecapBusinessLogic类,该类与我上面使用的业务有关.这样,您将拥有一个更加灵活和美观的控制器.

    Also you can separate concerns and create a JobRecapSearchModel containing those search parameters and use it as parameter of action and also create a JobRecapBusinessLogic class containing a List<JobRecap> Search(JobRecapSearchModel searchMode) method with the business that I used above. This way you will have a more flexible and beautiful controller.

    要了解有关如何使用这种方法以及其好处的更多信息,请查看以下问题:

    To learn more about how to use such method and the benefits you can take a look at this question:

    这篇关于在视图中呈现数据之前在控制器上过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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