Modelbinding失败的VS2010 asp.net mvc2

时间:2010-04-01 05:02:25

标签: asp.net-mvc-2 model-binding

contactAddModel.Search总是以null形式出现 - 任何想法?

查看声明

<%@ 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; }
}

查看内容

<%  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>
<% } %>

控制器

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

        return View("Index");
    }

1 个答案:

答案 0 :(得分:0)

您应该像这样修改AddContact动作

AddContact(Models.ContactAddModel contactAddModel)

只需将“model”替换为“contactAddModel”

即可