@Html. validationfor ()EditorFor (x =比;x.Name typeof(字符串).Fu
本文关键字:Name typeof Fu 字符串 validationfor EditorFor @Html | 更新日期: 2023-09-27 18:12:41
提前感谢您的帮助。
我有一个摇摇晃晃的旧MVC客户端作为我的VS解决方案的一部分,我们将其称为apicclient(后端客户端)。根模型(我们称之为User)拥有所有验证需求和消息。但是,请查看相关视图中的代码…
<<p> 视图/strong>@using Cms.Utils
@using Utils.Strings
@using Utils.Web
@using Utils.DateTimes
@using System.Web.Mvc
@using Models
@model Cms.Models.Users.AddModel
@SectionBreadcrumb.Add(Html, "Manage Users", "Add")
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<div class="col-xs-12">
<div class="row">
<h2>
Add Person
</h2>
</div>
<form method="POST">
<div class="row">
@*@Html.LabelFor(x => x.User.Username, new { @class = "control-label col-md-2" })*@
<div>
@Html.EditorFor(x => x.User.Username, typeof(string).FullName, new { @class = "form-control" })
</div>
<div class="row">
@Html.ValidationMessageFor(x => x.User.Username, "", new { @class = "text-danger" })
</div>
</div>
<<p> 视图的模型/strong> namespace Cms.Models.Users
{
public class AddModel
{
public User User { get; set; }
public List<Role> Roles { get; set; }
}
}
这里我必须提到User是解决方案中的子模块(另一个项目)中的模型。它也继承自CoreUser(也在同一子模块中)
用户模型
namespace Models
{
public class User : CoreUser
{
public bool IsFollowing { get; set; }
public bool IsFollowingMe { get; set; }
}
}
<<p> CoreUser模型/strong> namespace Sqor.Models
{
public class CoreUser : IEntity, IAttributeContainer
{
public int Id { get; set; }
[StringLength(256), RegularExpression(@"[^'s@]+@[^'s@]+'.[^'s@]+", ErrorMessage = "Invalid Email Address")]
public string Email { get; set; }
public string Phone { get; set; }
public bool IsPhoneVerified { get; set; }
[StringLength(20, ErrorMessage = "Usernames cannot exceed 20 characters."), RegularExpression("^[a-zA-Z](_[a-zA-Z0-9]|[a-zA-Z0-9])+$", ErrorMessage = "Letters, numbers, and underscores only. Start with a letter. No spaces. Max length of 20 characters.")]
public string Username { get; set; }
[MaxLength(200, ErrorMessage = "Snippet cannot exceed 200 characters.")]
public string Snippet { get; set; }
[MaxLength]
public string Bio { get; set; }
public bool IsTeam { get; set; }
public bool IsAmbassador { get; set; }
public DateTime Created { get; set; }
public string TimeZone { get; set; }
public int? ContractTypeId { get; set; }
public bool IsMvp { get; set; }
public string FacebookPageId { get; set; }
public string TwitterHandle { get; set; }
public string WikipediaUrl { get; set; }
public int NumberOfPosts { get; set; }
public string College { get; set; }
public bool? AdFree { get; set; }
public bool MarketingEmails { get; set; }
[StringLength(45, ErrorMessage = "First names cannot exceed 45 characters.")]
[Obsolete("Use FirstName property instead.")]
public string Firstname { get; set; }
[StringLength(45, ErrorMessage = "Last names cannot exceed 45 characters.")]
[Obsolete("Use LastName property instead.")]
public string Lastname { get; set; }
/// <summary>
/// Use this property moving forward.
/// </summary>
///
[StringLength(45, ErrorMessage = "First names cannot exceed 45 characters.")]
public string FirstName
{
#pragma warning disable 618
get { return this.Firstname; }
set { this.Firstname = value; }
#pragma warning restore 618
}
/// <summary>
/// Use this property moving forward.
/// </summary>
///
[StringLength(45, ErrorMessage = "Last names cannot exceed 45 characters.")]
public string LastName
{
#pragma warning disable 618
get { return this.Lastname; }
set { this.Lastname = value; }
#pragma warning restore 618
}
[RegularExpression(@"^([0]'d|[1][0-2])'/([0-2]'d|[3][0-1])'/([2][01]|[1][6-9])'d{2}('s([0-1]'d|[2][0-3])(':[0-5]'d){1,2})?$", ErrorMessage = "Please use a valid date. MM/DD/YYYY")]
public DateTime? BirthDate { get; set; }
[StringLength(45, ErrorMessage = "Locations cannot exceed 45 characters.")]
public string Location { get; set; }
[StringLength(45, ErrorMessage = "Gender cannot exceed 45 characters.")]
public string Gender { get; set; }
public string Avatar { get; set; }
public string ProfileHeader { get; set; }
public bool IsPlayer { get; set; }
public bool IsUser { get; set; }
public List<PlayerLeague> Leagues { get; set; }
public List<AthleteContract> Contracts { get; set; }
public List<Organization> Organizations { get; set; }
public int? AccountManagerId { get; set; }
[JsonIgnore]
public string FullName
{
get { return FirstName + " " + LastName; }
}
EntityType IEntity.Type
{
get { return EntityType.User; }
}
string IEntity.Name
{
get { return FullName; }
}
string IEntity.Team
{
get { return null; }
}
string IEntity.League
{
get { return null; }
}
int IEntity.LeaguePrecedence
{
get { return 0; }
}
string IEntity.JerseyNumber
{
get { return null; }
}
string IEntity.Position
{
get { return null; }
}
[JsonIgnore]
public Dictionary<string, object> Attributes
{
get { return AttributeTokens != null ? AttributeTokens.ToDictionary(x => x.Key, x => x.Value == null ? null : x.Value.ToObject<object>()) : new Dictionary<string, object>(); }
set { AttributeTokens = value.ToDictionary(x => x.Key, x => x.Value == null ? null : JToken.FromObject(x.Value)); }
}
[JsonExtensionData]
public Dictionary<string, JToken> AttributeTokens { get; set; }
[JsonIgnore]
public IEnumerable<AttributeValue> AttributeValues
{
get { return Attributes.Select(x => new AttributeValue { Name = x.Key, Value = x.Value }); }
set { Attributes = value.ToDictionary(x => x.Name, x => x.Value); }
}
public override string ToString()
{
return FullName + " (" + Id + ")";
}
}
}
我的问题是,在我的观点上面的每一个下面…@Html.EditorFor(x.User.Username)有一个重载类型typeof(string). fullname。 @Html.EditorFor (x.User。用户名、typeof(字符串)。FullName, new {placeholder = "blah"})
基本上改变了我的编辑器的名称,因此每个客户端@Html.ValidationMessageFor(x.User.Username)代码不会激活。
同样,如果我删除typeof(string)。FullName从@Html。例如,我的客户端验证开始工作,但是在保存时,它会将空值传递给相关的控制器,因此创建了一个包含所有空白信息的User。
- 为什么以前的开发人员使用这种类型的(字符串)。FullName的东西,它到底是什么?
- 如何摆脱这个typeof(string)。FullName和它的工作与我的模型(即。提交到控制器时不发送空值),以便客户端验证工作?
- 我可以让客户端验证工作的类型(字符串)。FullName之类的东西?
非常感谢你的任何帮助!!
解决方案资源管理器解决方案资源管理器屏幕剪辑@42shadow42视图/共享/EditorTemplates
UPDATE - UsersController.cs
[Route("person/add"), HttpPost]
public new async Task<ActionResult> Add(string email, string username, string firstName, string lastName, string snippet, string bio, bool isAmbassador = false, bool isTeam = false, Gender gender = Gender.None, string location = null, DateTime? birthdate = null,
string twitterHandle = null, string facebookPageId = null, string wikipediaUrl = null, bool marketingEmails = false, string[] roles = null)
-
typeof(字符串)。FullName指示MVC使用名为System.String.cshtml的编辑器模板
-
可能,见下文
-
是的,见下文2,这可能会或可能不会修复你的模型绑定。
3
解尝试将此用于您的System.String。cshtml,如果我是正确的,它应该呈现相同的html样式和格式,内置支持验证属性,你正在使用。
<div class="row">
@Html.LabelFor(model => model , new { @class = "col-xs-2" })
@Html.TextBoxFor(model => model , new { @class = "col-xs-10" })
</div>
2
解决方案1如果此时它没有建模绑定,您可能需要更改主视图的一部分:
替换:
<div>
@Html.EditorFor(x => x.User.Username, typeof(string).FullName, new { @class = "form-control" })
</div>
<div class="row">
@Html.ValidationMessageFor(x => x.User.Username, "", new { @class = "text-danger" })
</div>
<div>
@Html.EditorFor(x => x.User.Username, typeof(string).FullName, "Username", new { @class = "form-control" })
</div>
<div class="row">
@Html.ValidationMessageFor(x => x.User.Username, "", new { @class = "text-danger" })
</div>
2
解决方案#2或者你可以直接更新你的控制器动作方法签名,使用参数名"User"。这样的:
public ActionResult Edit(User User){
}