5
  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; }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • It would help to see the code of your model. Could you add that? – ScoobyDrew18 Dec 17 '15 at 20:33
  • You may find this post helpful [Filter/Search using Multiple Fields - ASP.NET MVC](http://stackoverflow.com/a/33154580/3110834) – Reza Aghaei Dec 17 '15 at 20:36
  • Yes I have seen this method but I would like to filter the view before it is rendered, not filtering it after the page is loaded. Thank you for the suggestion though!! – David Williams Dec 17 '15 at 20:43
  • It filters the data before the page renders. – Reza Aghaei Dec 17 '15 at 20:45
  • Have you considered using a separate view model class that would contain only the information you want from your model and pass that to the view engine? Creating that would effectively filter your content. – ScoobyDrew18 Dec 17 '15 at 20:47
  • Okay, I see it now, Reza...this might be a good solution for another idea of mine for another view I am building later so that does help. I still would like to see if what I have presented above is possible. Thank you!! – David Williams Dec 17 '15 at 20:48
  • Yes, ScoobyDrew, I have but I am trying to keep from having 5 different views for 5 different divisions. I also need the create, edit and delete views which require different layouts and formatting. That would be a lot of coding but if necessary I would do it. – David Williams Dec 17 '15 at 20:51
  • @DavidWilliams By the way, when you accept a question, it would be great if you also kindly vote for answer by click on up arrow near the answer. Also you can vote for as many answers as you find helpful, not only answers to your question, even answers to other questions that you find helpful. It's not compulsory at all but it's reasonable, [recommended](http://meta.stackexchange.com/a/5235/308647) and your kindness :) – Reza Aghaei Dec 17 '15 at 21:44
  • Gotcha! Like I said, Im new to all of this and I'm sure I will be asking more questions in the future..I definitely need help. I am grateful on how fast I got a response too!! – David Williams Dec 17 '15 at 21:47

1 Answers1

7

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:

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 2
    Not that it technically matters, but `AsQueryable` is unnecessary. `db.JobRecaps` returns an `IQueryable`. – Chris Pratt Dec 17 '15 at 20:52
  • Thank you Reza....it took a little bit to figure it out but I see what you are saying. I'm very new to this! – David Williams Dec 17 '15 at 21:16
  • 1
    @ChrisPratt Thank you for your comment, It's important for the OP to know we can use either `var data=db.JobRecaps.AsQueryable()` or `IQueryable data=db.JobRecaps;` but using `var data = db.JobRecaps;` then `data=data.Where(...)` cause a compile time error because type of data would be `DbSet` and this way we can not assign `data.Where(...)` to `data` – Reza Aghaei Dec 17 '15 at 21:33