Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering the data at the controller before it is rendered in a view

  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>
    
  6. 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!!

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; }
}
like image 332
David Williams Avatar asked Dec 17 '15 20:12

David Williams


1 Answers

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());
}

Note:

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:

  • Filter/Search using Multiple Fields - ASP.NET MVC
like image 107
Reza Aghaei Avatar answered Nov 14 '22 22:11

Reza Aghaei