0

contactAddModel.Search 总是以 null 的形式出现 - 有什么想法吗?

查看声明

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

视图模型

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

1 回答 1

0

您应该像这样修改操作 AddContact

AddContact(Models.ContactAddModel contactAddModel)

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

于 2010-04-13T04:55:09.977 回答