Asp.net mvc 3 ASP.NET MVC 3标签不显示
我有一门课的成绩如下:Asp.net mvc 3 ASP.NET MVC 3标签不显示,asp.net-mvc-3,Asp.net Mvc 3,我有一门课的成绩如下: public class MyClass{ [Display(Name="First Name")] public string FirstName{get;set;} } 在Razor视图中,我这样访问它,其中MyClass是模型上的一个属性: @Html.Label("MyClass.FirstName") 但是,“显示”属性中定义的值不会显示。如果我写: @Html.LabelFor(model => model.MyClass.Firs
public class MyClass{
[Display(Name="First Name")]
public string FirstName{get;set;}
}
在Razor视图中,我这样访问它,其中MyClass是模型上的一个属性:
@Html.Label("MyClass.FirstName")
但是,“显示”属性中定义的值不会显示。如果我写:
@Html.LabelFor(model => model.MyClass.FirstName)
这很好,但是对于我正在研究的解决方案,我必须使用第一种方法。第一种方法我错过了什么
更新
感谢您查看此问题,此问题是由在调用局部视图之前更改模型引起的。这意味着被评估的模型不是我期望的模型
问题现在解决了
Here is an example from one of my projects.
首先是视图模型类
public class UsersRegisterViewModel
{
[Display(Name = "E-Mail Address")]
[Required(ErrorMessage = "E-Mail address is required")]
[Email(ErrorMessage = "Not a valid e-mail address")]
[Remote("UserNameIsAvailable", "Validation", ErrorMessage = "Username is not available")]
public string UserName { get; set; }
[Display(Name = "Password")]
[Required(ErrorMessage = "Please enter a password")]
public string Password { get; set; }
[Display(Name = "Verify Password")]
[Required(ErrorMessage = "Please confirm your password")]
[Compare("Password", ErrorMessage = "Passwords don't match")]
public string VerifyPassword { get; set; }
[Required(ErrorMessage = "Please enter a display name")]
[Remote("DisplayNameIsAvailable", "Validation", ErrorMessage = "Display name is not avalable")]
public string DisplayName { get; set; }
}
Now the View (Ignore the AJAX goo)
@model UsersRegisterViewModel
<div id="user-register" class="site-contol">
<h2>Account Registration</h2>
<p></p>
@using (Ajax.BeginForm("Register", "Users", null, new AjaxOptions
{
HttpMethod = "post",
UpdateTargetId = "user-registration",
InsertionMode = InsertionMode.Replace,
OnSuccess = "registrationCallBacks.onSuccess",
OnFailure = "registrationCallBacks.onError"
}, new {@id = "frm-sign-in"}))
{
<ul>
<li>@Html.LabelFor(m => m.UserName)</li>
<li>@Html.TextBoxFor(m => m.UserName) @Html.ValidationMessageFor(m => m.UserName)</li>
<li>@Html.LabelFor(m => m.Password)</li>
<li>@Html.PasswordFor(m => m.Password) @Html.ValidationMessageFor(m => m.Password)</li>
<li>@Html.LabelFor(m => m.VerifyPassword)</li>
<li>@Html.PasswordFor(m => m.VerifyPassword) @Html.ValidationMessageFor(m => m.VerifyPassword)</li>
<li>@Html.LabelFor(m => m.DisplayName)</li>
<li>@Html.TextBoxFor(m => m.DisplayName) @Html.ValidationMessageFor(m => m.DisplayName)</li>
<li>
<ul>
<li><input type="submit" name="sb-register" value="Create Account"/></li>
</ul>
</li>
</ul>
}
</div>
public类用户注册视图模型
{
[显示(Name=“电子邮件地址”)]
[必需(ErrorMessage=“需要电子邮件地址”)]
[电子邮件(ErrorMessage=“不是有效的电子邮件地址”)]
[远程(“usernameisavaailable”,“Validation”,ErrorMessage=“Username不可用”)]
公共字符串用户名{get;set;}
[显示(Name=“密码”)]
[必需(ErrorMessage=“请输入密码”)]
公共字符串密码{get;set;}
[显示(Name=“验证密码”)]
[必需(ErrorMessage=“请确认您的密码”)]
[比较(“密码”,ErrorMessage=“密码不匹配”)]
公共字符串验证密码{get;set;}
[必需(ErrorMessage=“请输入显示名称”)]
[远程(“DisplayNameIsAvailable”、“Validation”、ErrorMessage=“显示名称不可用”)]
公共字符串DisplayName{get;set;}
}
现在是视图(忽略AJAX goo)
@模型用户注册视图模型
帐户注册
@使用(Ajax.BeginForm(“Register”,“Users”),null,新的AjaxOptions
{
HttpMethod=“post”,
UpdateTargetId=“用户注册”,
InsertionMode=InsertionMode.Replace,
OnSuccess=“registrationCallBacks.OnSuccess”,
OnFailure=“registrationCallBacks.onError”
},新的{@id=“frm登录”})
{
- @LabelFor(m=>m.UserName)
- @Html.TextBoxFor(m=>m.UserName)@Html.ValidationMessageFor(m=>m.UserName)
- @LabelFor(m=>m.Password)
- @Html.PasswordFor(m=>m.Password)@Html.ValidationMessageFor(m=>m.Password)
- @LabelFor(m=>m.VerifyPassword)
- @Html.PasswordFor(m=>m.VerifyPassword)@Html.ValidationMessageFor(m=>m.VerifyPassword)
- @LabelFor(m=>m.DisplayName)
- @Html.TextBoxFor(m=>m.DisplayName)@Html.ValidationMessageFor(m=>m.DisplayName)
-
}
如果您正在使用MyClass
模型大力键入视图,请尝试
@Html.LabelFor(model => model.FirstName)
请发布整个视图的代码。我看不出@model include在哪里?您是否尝试过
@Html.Label(“model.MyClass.FirstName”)
?您在上面的问题中是否输入了双引号中第一个方法中的参数?它不应该是model.MyClass.FirstName,周围没有任何引号吗?它应该是@Html.LabelFor(x=>x.PropertyOfModel),还要确保模型的命名空间在范围内