Custom ViewModel with MVC 2 Strongly Typed HTML Helpers return null object on Create ?
- by Barbaros Alp
Hi,
I am having a trouble while trying to create an entity with a custom view modeled create form. Below is my custom view model for Category Creation form.
public class CategoryFormViewModel
{
    public CategoryFormViewModel(Category category, string actionTitle)
    {
        Category = category;
        ActionTitle = actionTitle;
    }
    public Category Category { get; private set; }
    public string ActionTitle { get; private set; }
}
and this is my user control where the UI is
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CategoryFormViewModel>" %>
        <h2>
            <span><%= Html.Encode(Model.ActionTitle) %></span>
        </h2>
        <%=Html.ValidationSummary() %>
        <% using (Html.BeginForm()) {%>
        <p>
            <span class="bold block">Baslik:</span>
            <%=Html.TextBoxFor(model => Model.Category.Title, new { @class = "width80 txt-base" })%>
        </p>
        <p>
            <span class="bold block">Sira Numarasi:</span>
            <%=Html.TextBoxFor(model => Model.Category.OrderNo, new { @class = "width10 txt-base" })%>
        </p>        
        <p>
            <input type="submit" class="btn-admin cursorPointer" value="Save" />
        </p>
        <% } %>
When i click on save button, it doesnt bind the category for me because of i am using custom view model and strongly typed html helpers like that
<%=Html.TextBoxFor(model => Model.Category.OrderNo) %>
How can i fix this ?
Thanks in advance