在mvc应用程序中添加其他字段时显示错误

时间:2014-08-08 10:35:52

标签: c# asp.net-mvc

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

<asp:Content ID="registerTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Register
</asp:Content>

<asp:Content ID="registerContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Create a New Account</h2>
    <p>
        Use the form below to create a new account. 
    </p>
    <p>
        Passwords are required to be a minimum of <%: ViewData["PasswordLength"] %> characters in length.
    </p>

<% using (Html.BeginForm()) { %>
    <%: Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.") %>
    <div>
        <fieldset>
            <legend>Account Information</legend>

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

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

            <div class="editor-label">
                <%: Html.LabelFor(m => m.Password) %>
            </div>
            <div class="editor-field">
                <%: Html.PasswordFor(m => m.Password) %>
                <%: Html.ValidationMessageFor(m => m.Password) %>
            </div>

            **<div class="editor-label">
                <%: Html.LabelFor(m => m.FirstName) %>
            </div>
            <div class="editor-field">
                <%: Html.TextboxFor(m => m.FirstName)%>
                <%: Html.ValidationMessageFor(m => m.FirstName)%>
            </div>**

            <div class="editor-label">
                <%: Html.LabelFor(m => m.ConfirmPassword) %>
            </div>
            <div class="editor-field">
                <%: Html.PasswordFor(m => m.ConfirmPassword) %>
                <%: Html.ValidationMessageFor(m => m.ConfirmPassword) %>
            </div>

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

在此代码中,我添加了FirstName名称中的新字段,但收到错误:

  

'MvcApplication1.Models.RegisterModel'不包含'FirstName'的定义,并且没有扩展方法'FirstName'接受类型为'MvcApplication1.Models.RegisterModel'的第一个参数'(您是否缺少using指令或汇编参考?)

1 个答案:

答案 0 :(得分:0)

向视图中添加新属性是不够的。

public string FirstName{ get; set; }添加到您的RegisterModel课程。

应该如下所示。

public class RegisterModel
{
    // other properties

    [Display(Name = "First name")]
    public string FirstName { get; set; }
}