2

Although the problem I'll be detailing in this question is quite specific, I guess it should help some other beginner programmers like me. I have this following situation: in the system I'm designing, the user has the option to set the values for his search in the database (instead of just routing the parameter of the controller action to the id of a object in the database, as I've done before), with the following fields:

  • Power Plant (Drop down list of all of the existing Power Plants)
  • Generating Units (List box with multiple selection that displays the generating units of the selected Power Plant)
  • Period (A drop down list with the available time spans, like "Last 7 days" and "Last Month")
  • Beginning and End Time (If instead of selecting a predefined time, the user wants to define the specific time span)

How can I put this elements on a page and search for the specified values in the database? I mean, how do I make this fields an object that I can send to the POST action, so it can compare to the database and get the values? What I've tried to do included create an editor template for this (but I couldn't understand what I was doing), and also I tried creating a controller to deal with this queries, so I could render just the query in the other pages (but I ended up with a page inside a page, in the best of "yo dawg I haerd u liek" style).

Can you guys give me some help, so I don't have to give up being a programmer and become a rapper?

1
  • Id stay you better stick to rapping... Sorry just kidding. Could you elaborate on where exactly you are having a hard time implementing this? Is it the view, the viewmodel, the controller? I've done something very similar to what you are attempting, but I am not quite sure what exactly to explain. Commented Jan 30, 2011 at 11:07

2 Answers 2

1

It is a good example:

http://weblogs.asp.net/rajbk/archive/2010/05/08/asp-net-mvc-paging-sorting-filtering-using-the-mvccontrib-grid-and-pager.aspx

I think it is written in MVC 2, but can easily be converted to 3.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't quite understand from your question what entity you are filtering in your view, so i'll just call it "Entity". in your controller you should have an entity repository (if you are unfamilier with the repository pattern you should look it up) if you got that set up, filtering shouldn't be too difficult:

Create a view model class as followes:

public class EntityFilterViewModel()
{
    string PowerPlanet {get;set;}
    string GeneratingUnits {get;set;}
    string Period{get;set;} // Simplification, you should use timespan or something.
    DateTime BeginTime {get;set;}
    DateTime EndTime {get;set;}
}

Next, Have a controller method like this:

public ActionResult Filter(EntityFilterViewModel model) 
{
     var result = from e in _entityRepository.Entites
                  where e.PowerPlanet.Equals(model.PowerPlanet) &&
                        e.GeneratingUnits.Equals(model.GeneratingUnits) &&
                        e.Periond.Equals(model.Period)
                        // other filters you would want...

     return View("List", result); // use the overload which takes a view name and a viewmodel object
}

You should remember though to account for situations in which the user hasn't filled all the fields in the filtering form

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.