My model inherits from an interface:
public interface IGrid
{
ISearchExpression Search { get; set; }
.
.
}
public interface ISearchExpression
{
IRelationPredicateBucket Get();
}
The model:
public class Project : IGrid
{
public ISearchExpression Search { get; set; }
public Project()
{
this.Search = new ProjectSearch();
}
}
The ProjectSearch:
public class ProjectSearch: ISearchExpression
{
public string Name { get; set; }
public string Number { get; set; }
public IRelationPredicateBucket Get()
{...}
}
And the strong typed partialview in the main view:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ProjectSearch>" %>
<%= Html.TextBoxFor(x=>x.Name)%>
<%= Html.TextBoxFor(x => x.Number)%>
....
When I submit the form, the Search property don’t get bound properly. Everything is empty.The action takes an argument of ProjectSearch type.
Why the Search don’t get bound as supposed ?
EDIT
The action
public virtual ActionResult List(Project gridModel)
{..}
You need to specify the correct prefix in order to bind sub types. For example if you want to bind to
Nameproperty of theSearchproperty of the Model your textbox must be named:Search.Name. When you useHtml.TextBoxFor(x=>x.Name)your textbox is namedNameand the model binder doesn’t work. One workaround is to explicitly specify the name:or use editor templates which is a new feature in ASP.NET MVC 2.0
UPDATE:
Based on the additional details provided in the comments section here’s a sample that should work:
Model:
Controller:
View – ~/Views/Home/Index.aspx:
View – ~/Views/Home/SearchTemplate.ascx: