Custom ViewModel with MVC 2 Strongly Typed HTML Helpers return null object on Create ?

Posted by Barbaros Alp on Stack Overflow See other posts from Stack Overflow or by Barbaros Alp
Published on 2010-03-22T18:59:31Z Indexed on 2010/03/22 19:11 UTC
Read the original article Hit count: 676

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

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about asp.net-mvc2