开发者

Modelbinding Failing VS2010 asp.net mvc2

开发者 https://www.devze.com 2022-12-25 13:41 出处:网络
The contactAddModel.Search always comes through as null - any ideas? View declaration <%@ Page 开发者_运维问答Title=\"\" Language=\"C#\" MasterPageFile=\"~/Views/Shared/Site.Master\" Inherits=\"S

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


You should modify the action AddContact like this

AddContact(Models.ContactAddModel contactAddModel)

just replace "model" with "contactAddModel"

0

精彩评论

暂无评论...
验证码 换一张
取 消