So, this is how I usually solve this problem. My notes are purely my opinion (religious?) On the class names in the MVC project in order to maintain its purpose.
A couple of interfaces to keep it extensible:
Couple of classes
// I like suffixing classes that I only use for MVC with Model public PersonSearchCriteriaModel { public string Name {get; set;} public string Company {get; set;} public string DateStart {get; set;} public string DateEnd {get; set;} } // I like suffixing classes that I used passed to a View/Partial View // with ViewModel public class PersonSearchViewModel : IPersonSearchResultsView, IPersonSearchCriteriaView { public IEnumerable<EFPerson> PersonSearchResults { get; set; } public PersonSearchCriteriaModel PersonSearchModel { get; set; } }
Now for your controllers, I will configure them so that you can also do Ajax in the future.
public PersonController : Controller { public ActionResult Search() { var model = new PersonSearchViewModel();
Create some partial views of your views.
/Views/Person/Partial-SearchCriteria.cshtml
@model IPersonSearchCriteriaView // the new part is for htmlAttributes, used by Ajax later @using (Html.BeginForm(..., new { id="searchCriteria" })) { // Here is were the magic is, if you use the @Html.*For(m=>) // Methods, they will create names that match the model // and you can back back to the same model on Get/Post <label>Name:</label> @Html.TextBoxFor(m => Model.PersonSearchModel.Name) // or let mvc create a working label automagically @Html.EditorFor(m => Model.PersonSearchModel.Name) // or let mvc create the entire form.. @Html.EditorFor(m => Model.PersonSearchModel) }
/Views/Person/Partial-SearchResults.cshtml
@model IPersonSearchResultsView @foreach (var person in Model.PersonSearchResults ) { <tr> <td class="list-field"> @Html.DisplayFor(modelItem => person.Name) </td>
And finally, the view:
/Views/Person/Search.cshtml
@model PersonSearchViewModel @Html.Partial("Partial-SearchCriteria", Model) // easily change the order of these <div id="searchResults"> @Html.Partial("Partial-SearchResults", Model); </div>
Now the inclusion of Ajax is pretty crazy (simplified and not entirely accurate):
$.Ajax({ url: '/Person/Results', data: $('#searchCriteria').serialize(), success: function(jsonResult) { $('#searchResults').innerHtml(jsonResult); });