How to create a form with parameters and a list in MVC3

First, I'm new to MVC - and testing ASP.NET MVC3.

I want to create a page that looks like this:

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

[Results Table]

The user enters a start and end date (which should be checked), then they click the Search button and return with the corresponding results.

So how do I structure this? Here is my idea of ​​the 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 action with the index controller is to construct an instance of ResultSearchModel and return a view with this model?

Am I doing this in one view, or do I need to have a partial view for part of the list?

+4
source share
1 answer

I would use a display template for the results:

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

and then you can define the corresponding template ( ~/Views/Home/DisplayTemplates/ServiceEntry.cshtml ) that will be displayed for each element of the ServiceEntries collection. Thus, you will have a controller with two actions: one for visualizing the form and one that will take POST from the form and handle the search. In the first step, you simply return your view model, leaving the ServiceEntries property empty so that the results do not show, and in the second step, you populate this property. I would also use IEnumerable<ServiceEntry> as a type.

+3
source

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


All Articles