在mvc应用程序中添加额外字段时显示错误
本文关键字:字段 显示 错误 mvc 应用程序 添加 | 更新日期: 2023-09-27 18:17:00
<%@ 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'接受类型为' mvcapapplication1 . models '的第一个参数。可以找到RegisterModel'(您是否缺少using指令或程序集引用?)
为视图添加新属性是不够的。
将public string FirstName{ get; set; }
添加到RegisterModel
类中。
应该像下面这样
public class RegisterModel
{
// other properties
[Display(Name = "First name")]
public string FirstName { get; set; }
}