我在asp.net mvc应用程序中遇到此问题.
在我的一个模型中有一个字段"描述".此字段的数据库列设置为NVarchar(300).
在我看来,我正在创建一个文本区域如下.
@Html.TextAreaFor(m => m.Description, new { maxlength = "300" })
Run Code Online (Sandbox Code Playgroud)
我正在使用"jquery.validate.unobtrusive.min.js"进行客户端验证.因此,当textarea中的用户类型和内容长度超过300个字符时,它会显示消息"请输入不超过300个字符".
一切正常,直到下面的情景来临.用户在文本区域中输入以下数据.
f
f
f
f
f
f
f
f
sdfa
Run Code Online (Sandbox Code Playgroud)
(此内容有8条新线)
根据"不显眼"验证,此内容的长度为300 (将每个新行"\n"计为单个字符),以便验证通过并返回页面帖子.
在我的C#代码中,由于编码,相同的内容变为长度308 (将每个新行"\ r \n"计为2个字符),这使得数据库操作失败,因为它只允许300个字符.
如果有人说我应该拥有StringLength这个特定属性,我有以下原因没有它.
如果我将此属性设置为此特定属性不会发生客户端验证,则会转到服务器,因为模型无效,所以它会返回到包含错误消息的页面.
请告诉我可能的解决方案是什么?
我试图在我们的代码中解决这种情况,我需要在运行时根据特定条件解决依赖关系,例如是否存在某些查询字符串值.
假设我有一个控制器AuthenticationController,我有两种风格的身份验证服务.
public class AuthenticationController
{
private readonly IAuthenticationService authenticationService;
public AuthenticationController(IAuthenticationService authenticationService)
{
this.authenticationService = authenticationService;
}
public ActionResult LogOn(LogOnModel model)
{
var isAuthenticated = authenticationService.AuthenticatUser(model.UserName, model.Password);
}
}
public interface IAuthenticationService
{
bool AuthenticatUser(string userName, string password);
}
public class ProviderBasedAuthenticationService : IAuthenticationService
{
public bool AuthenticatUser(string userName, string password)
{
// Authentication using provider;
return true;
}
}
public class ThirdPartyAuthenticationService : IAuthenticationService
{
public bool AuthenticatUser(string userName, string password)
{
// Authentication using third party;
return …Run Code Online (Sandbox Code Playgroud) asp.net-mvc dependency-injection castle-windsor inversion-of-control