4

Firstly, I'm new to MVC - and trying out ASP.NET MVC3.

I want to build a page that looks like this:

Start Date: [Date Box] End Date: [Date Box] [Search Button]

[Table of Results]

The user enters a start and end date (which must be validated), then they click the Search button and it comes back with the matching results.

So, how do I structure this? Here's my idea of a Model class:

public class ResultSearchModel
{
    [Required]
    [DataType((DataType.DateTime))]
    [DisplayName("Start Date")]
    public DateTime StartDate { get; set; }

    [Required]
    [DataType((DataType.DateTime))]
    [DisplayName("End Date")]
    public DateTime EndDate { get; set; }

    public List<ServiceEntry> ServiceEntries { get; set; }
}

public class ServiceEntry
{
    public DateTime Date { get; set; }
    public string Code { get; set; }
    public string Details { get; set; }
}

So, my Index controller action should contruct an instance of the ResultSearchModel and return a View with this model?

Do I do this in one view, or do I have to have a partial view for the list part?

1 Answer 1

3

I would use a display template for the results:

@Html.DisplayFor(x => x.ServiceEntries)

and then you could define the corresponding template (~/Views/Home/DisplayTemplates/ServiceEntry.cshtml) which will be rendered for each element of the ServiceEntries collection. So you would have a controller with two actions: one to render the form and one that will accept the POST from the form and handle the search. In the first action you would simply return your view model by leaving the ServiceEntries property empty so that no results are shown and in the second action you would populate this property. I would also use IEnumerable<ServiceEntry> as type.

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

2 Comments

Thanks Darin - I sort of have this working. So my ServiceEntry.cshtml template will just be a table row with a td for each property. Is there a good way to generate this (i.e. Add View...) or do I have to hand-code this?
@Craig, exactly, the ServiceEntry will contain only a single row. You could generate it with Add View in the DisplayTemplates folder and pick up ServiceEntry as model.

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.