I’m playing with an ASP.NET MVC application and I’ve run into a bit of a problem. I am pretty new to ASP.NET MVC and just barely understand the basics to get things to work at this point.
I have a PersonModel, a PersonController, and a bunch of views that let a user add a new person, edit a person and search for people.
I am not using a DataBase in the back end. Everything I’m doing depends on an external DLL that returns “person” structures (that I turn into PersonModels).
In order to search for people, I have to provide a person-structure that acts as search criteria to a method in the external DLL. The method returns a collection of person-structures that match the search criteria. If I want to retrieve all of the people in the system I supply an empty person-structure to the method.
So, I have the “retrieve all people” function working…..but I’d like to provide an advanced search.
My Search View is bound to a class that contains 2 properties:
Public Class PersonSearchModel
Private _searchCriteria As PersonModel
Private _searchResults As List(Of PersonModel)
Public Property SearchCriteria As PersonModel
Get
return _searchCriteria
End Get
Set(ByVal value As PersonModel)
_searchCriteria = value
End Set
End Property
Public Property SearchResults As List(Of PersonModel)
Get
return _searchResults
End Get
Set(ByVal value As List(Of PersonModel))
_searchResults = value
End Set
End Property
End Class
Now the Search View binds to this PersonSearchModel and I have 2 sections…a section where the user can provide search criteria and a section that displays the search results.
I am having a problem binding the PersonSearchModel.SearchCriteria to the controls used to display/gather the Person search criteria.
I cannot retrieve the search criteria.
This what I have in my view for the search criteria:
<fieldset>
<legend>Search Criteria</legend>
<%
With Model.SearchCriteria
%>
<div style="float:left">
<p>
<label for="FirstName">
FirstName:</label>
<%=Html.TextBox("FirstName", Html.Encode(.FirstName))%>
<%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
<label for="LastName">
LastName:</label>
<%=Html.TextBox("LastName", Html.Encode(.LastName))%>
<%= Html.ValidationMessage("LastName", "*") %>
</p>
<!-- More controls -->
</div>
<% End With%>
</fieldset>
<%=Html.ActionLink("Search", "Search",Model.SearchCriteria)%>
<!-- The Search Results Section-->
The PersonModel passed into the Search method is a new/empty PersonModel Object.
It does not contain the data that the user entered.
What am I doing wrong here?
********** Edit **********
I have tried changing the View to bind differently. I removed the VB “With”:
<fieldset>
<legend>Search Criteria</legend>
<div style="float:left">
<p>
<label for="FirstName">
FirstName:</label>
<%=Html.TextBox("FirstName", Html.Encode(.FirstName))%>
<%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
<label for="LastName">
LastName:</label>
<%=Html.TextBox("LastName", Html.Encode(.LastName))%>
<%= Html.ValidationMessage("LastName", "*") %>
</p>
<!-- More controls -->
</div>
</fieldset>
<%=Html.ActionLink("Search", "Search",Model.SearchCriteria)%>
<!-- The Search Results Section-->
But this didn’t help.
I also tried:
<fieldset>
<legend>Search Criteria</legend>
<div style="float:left">
<p>
<label for="FirstName">
FirstName:</label>
<%=Html.TextBox("Model.SearchCriteria.FirstName", Html.Encode(Model.SearchCriteria.FirstName))%>
<%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
<label for="LastName">
LastName:</label>
<%=Html.TextBox("Model.SearchCriteria.LastName", Html.Encode(Model.SearchCriteria.LastName))%>
<%= Html.ValidationMessage("LastName", "*") %>
</p>
<!-- More controls -->
</div>
</fieldset>
<%=Html.ActionLink("Search", "Search",Model.SearchCriteria)%>
<!-- The Search Results Section-->
And:
<fieldset>
<legend>Search Criteria</legend>
<div style="float:left">
<p>
<label for="FirstName">
FirstName:</label>
<%=Html.TextBox("SearchCriteria.FirstName")%>
<%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
<label for="LastName">
LastName:</label>
<%=Html.TextBox(".SearchCriteria.LastName")%>
<%= Html.ValidationMessage("LastName", "*") %>
</p>
<!-- More controls -->
</div>
</fieldset>
<%=Html.ActionLink("Search", "Search",Model.SearchCriteria)%>
<!-- The Search Results Section-->
However, I am still getting an empty/new PersonModel passed into the Search method in the controller. I’ve also checked the PersonSearchModel.SearchCriteria to see if maybe that contained the values entered, but this also has a new/empty PersonModel.
-Frinny
Using reflection is pretty much what the MVC model binders are setup to do, my guess is that you weren’t naming your fields correctly so when they posted back to your action they didn’t map up to your parameters. Try doing something like:
Then your fields (HTML) should be named like so: