Modelbinding Failing VS2010 asp.net mvc2

Posted by Rob Ellis on Stack Overflow See other posts from Stack Overflow or by Rob Ellis
Published on 2010-04-01T05:02:25Z Indexed on 2010/04/02 5:03 UTC
Read the original article Hit count: 354

Filed under:
|
|

The contactAddModel.Search always comes through as null - any ideas?

View declaration

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<rs30UserWeb.Models.StatusIndexModel>" %>

ViewModels

public class StatusIndexModel
{
    public ContactAddModel contactAddModel;
    public StatusMessageModel statusMessageModel;
}

public class ContactAddModel
{
    [Required(ErrorMessage="Contact search string")]
    [DisplayName("Contact Search")]
    public string Search { get; set; }
}

View content

<%  using (Html.BeginForm("AddContact", "Status")) { %>
    <div>
        <fieldset>
            <legend>Add a new Contact</legend>

            <div class="editor-label">
                <%= Html.LabelFor(m => m.contactAddModel.Search) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(m => m.contactAddModel.Search)%>
                <%= Html.ValidationMessageFor(m => m.contactAddModel.Search)%>
            </div>

            <p>
                <input type="submit" value="Add Contact" />
            </p>
        </fieldset>
    </div>
<% } %>

Controller

    [HttpPost]
    public ActionResult AddContact(Models.ContactAddModel model)
    {
        if (u != null)
        {
        }
        else
        {
            ModelState.AddModelError("contactAddModel.Search", "User not found");
        }

        return View("Index");
    }

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about mvc2