Filtering data on the controller before it is displayed in the view

  • Hi, I am very new to MVC5, Razor and EF, and I searched for two days and still can not find a solution to my problem.
  • What I want to do is a view in which users enter the year, quarter, and section. On submit, I want the controller for another view to see these parameters and filter out the data before rendering the view. Currently, I have 5 different sections, and I want to filter out only one unit when rendering the view.
  • I went through a lot of forums, websites, etc., trying to figure it out, and I was out of luck. I would be glad to at least point in the right direction. I try to find out by jumping into the fire and figuring it out myself, but now I need help.
  • I have an idea about how MVC works, I have no problems with working with the database, and I was able to find out how scaffolding works, as well as ViewModels. Now I'm trying to learn how to manipulate data in the controller and views. Any help would be appreciated.
  • View 1 - Just enter the 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> 
  • Controller to 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 do not know what and how to start here. Thank you for your help!

CHANGE system usage; 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; } } 
+5
source share
1 answer

To keep things simple, can you just add int? Year, int? Qtr, string Div parameters int? Year, int? Qtr, string Div int? Year, int? Qtr, string Div int? Year, int? Qtr, string Div 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:

You can also separate the problems and create a JobRecapSearchModel containing these search parameters, and use it as an action parameter, and create a JobRecapBusinessLogic class containing the List<JobRecap> Search(JobRecapSearchModel searchMode) with the business that I used above. Thus, you will have a more flexible and beautiful controller.

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

+5
source

Source: https://habr.com/ru/post/1238570/


All Articles