views:

124

answers:

1

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");
    }
A: 

You should modify the action AddContact like this

AddContact(Models.ContactAddModel contactAddModel)

just replace "model" with "contactAddModel"

jamaz
Hi Jamaz, this didn't work I'm afraid - nothing is passed into the controller still.
Rob Ellis
Actually, this did work. I needed to make sure all the names throughout were exactly the same. Thanks Jamaz!
Rob Ellis