如何将自定义登录失败通知传递给MVC4中的视图(How to pass a custom login failed notification to the view in MVC4)
我想在我编写自己的视图中传递错误登录通知,但我不知道如何。 我想把它放在
@Html.ValidationMessageFor(model => model.Password)
和@Html.ValidationMessageFor(model => model.Password)
或一个单独的标签(我是否正确使用@Html.ValidationMessage()
而不是@Html.ValidationMessageFor()
?)这是我的模特:
public class User { public int UserId { get; set; } [Required] [Display(Name = "User Name")] public string UserName { get; set; } [Required] [DataType(DataType.Password)] public string Password { get; set; } }
这里是我的控制器:
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Login(User p) { if (ModelState.IsValid) { User item = db.Authenticate(p); if (item != null) // if item is not null, the login succeeded { return RedirectToAction("Main", "Home"); } } string error = "Incorrect user name or password."; // I don't know how to pass this return View(); //login failed }
这是我的看法:
@using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) <fieldset> <legend>User</legend> <div class="editor-label"> @Html.LabelFor(model => model.UserName) </div> <div class="editor-field"> @Html.EditorFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName) </div> <div class="editor-label"> @Html.LabelFor(model => model.Password) </div> <div class="editor-field"> @Html.EditorFor(model => model.Password) @Html.ValidationMessageFor(model => model.Password) </div> <p> <input type="submit" value="Login" /> </p> </fieldset> }
I want to pass an error login notification in the view which I coded my self, but I do not know how. I want to put it in the
@Html.ValidationMessageFor(model => model.UserName)
and@Html.ValidationMessageFor(model => model.Password)
or a separate label (am I correct that I will use@Html.ValidationMessage()
instead of@Html.ValidationMessageFor()
?)here is my model:
public class User { public int UserId { get; set; } [Required] [Display(Name = "User Name")] public string UserName { get; set; } [Required] [DataType(DataType.Password)] public string Password { get; set; } }
here is my controller:
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Login(User p) { if (ModelState.IsValid) { User item = db.Authenticate(p); if (item != null) // if item is not null, the login succeeded { return RedirectToAction("Main", "Home"); } } string error = "Incorrect user name or password."; // I don't know how to pass this return View(); //login failed }
here is my view:
@using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) <fieldset> <legend>User</legend> <div class="editor-label"> @Html.LabelFor(model => model.UserName) </div> <div class="editor-field"> @Html.EditorFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName) </div> <div class="editor-label"> @Html.LabelFor(model => model.Password) </div> <div class="editor-field"> @Html.EditorFor(model => model.Password) @Html.ValidationMessageFor(model => model.Password) </div> <p> <input type="submit" value="Login" /> </p> </fieldset> }
原文:https://stackoverflow.com/questions/46851142
最满意答案
这里有一些奇怪的事情。
首先:当你
use strict
激活时,如果你使用一个变量而不声明它, 或者用完全限定名引用它,你会得到一个警告。你实际做的是在
xyzzy.pl
中用my()
声明一个本地的%cfg
,然后引用一个不同的包全局变量%main::cfg
(通过使用它的完全限定名隐式声明)。要使引用链接与您声明的
%cfg
相同,您应该声明它为our()
以使其成为package-global。 然后你可以在两个地方以$main::cfg{}
方式引用它(或者从xyzzy.pl
只需$cfg{}
),否则你可以在plugh.pm
声明它our()
,以便你可以在两个地方都使用%cfg)。奇怪的是,你有两个引用该变量,所以你不应该得到警告。 我认为这里发生的是,两个单独文件中的隐式声明被假定为单独的变量。
xyzzy.pl:
require plugh; our (%cfg); sub subOne () { my $list = `ls -1 $cfg{"abc"}`; ... }
plugh.pm:
our(%cfg); sub cfgRead () { $cfg{"abc"} = "/usr"; }
There are a few odd things here.
First: when you have
use strict
active, you will get a warning if you use a variable without declaring it, or referencing it by fully qualified name.What you have actually done is to declare a local
%cfg
withmy()
inxyzzy.pl
, and then to reference a different, package-global variable%main::cfg
(implicitly declared by using its fully qualified name).To make the reference link to the same
%cfg
that you declared, you should declare itour()
to make it package-global. Then you can either reference it as$main::cfg{}
in both places (or just$cfg{}
fromxyzzy.pl
), or else you can declare itour()
inplugh.pm
as well (so that you can use the bare %cfg in both places).The odd thing is that you do have two references to that variable, so you shouldn't get the warning. I think what has happened here is that the implicit declarations in two separate files are assumed to be separate variables.
xyzzy.pl:
require plugh; our (%cfg); sub subOne () { my $list = `ls -1 $cfg{"abc"}`; ... }
plugh.pm:
our(%cfg); sub cfgRead () { $cfg{"abc"} = "/usr"; }
相关问答
更多-
你在从另一个文件中读取你的项目名称吗? 如果是这样,你是否记得在最后删除\n (使用chomp $line或类似)? Are you reading your project names from another file? If so, did you remember to remove \n at the end (using chomp $line or similar)?
-
Perl新行分隔符问题(Perl New Line separator issue)[2022-09-13]
之前尝试一下 binmode($in); try this before while binmode($in); -
Perl和MS SQL线程问题(Perl and MS SQL thread issue)[2023-02-07]
除了安装期间使用的常用构建和测试模块之外, DBI不依赖于任何其他内容 SQL Server没有DBD驱动程序(我不清楚为什么。也许有人会启发我?)所以你需要使用DBD::ODBC驱动程序的ODBC连接 我不确定DBI在线程下的行为如何,我建议你使用fork而不是在Windows版本的Perl上模拟。 您需要设置$dbh->{AutoInactiveDestroy} = 1以防止进程在不应该自动销毁数据库和语句句柄时 DBI isn't dependent on anything other than th ... -
核心Time::Piece重载内置的本地时间函数,以便它返回一个带有strftime方法的对象,允许您根据自己的愿望格式化结果 ( localtime继续以相同的方式执行所有其他操作,所以您不会失去任何功能。) use strict; use warnings 'all'; use Time::Piece; my $rs = { lastlogin => 1467726403 }; my $epoc = $rs->{lastlogin}; my $date = localtime($epoc)->st ...
-
内嵌:: Perl5的 截至2014年底,对于大多数人来说,对于大多数人来说, Inline :: Perl5是在P6中使用P5代码的方式,反之亦然,包括在P6中use P5库。 Inline :: Perl5在MoarVM中适当地打包并包装一个常规的perl5解释器,以便P6编译器Rakudo可以与P5互操作。 该组合已经支持: 在Perl 6中use Perl 5模块,包括使用XS的模块 在P6中操纵P5对象,反之亦然 在P6中编写Perl 5类的子类 九,该模块的作者,在他创办Inline :: Pe ...
-
你应该: 清理 从任何不需要的路径清理(注释掉)你的~/.profile ,等等 从你的$HOME清理任何新的perl安装(确保移到安全的地方) 总之,请尝试将您的环境恢复到之前的工作状态 重新登录,(注销,登录) 修复你的系统Perl。 这意味着, 阅读@Sam Varshavchik的回答 使用你的包管理器(5.10)从你的发行版重新安装它。 这一步应该覆盖你造成的混乱。 测试它! 直到你确定之后才能继续,一切正常。 学到的教训: 永远不要覆盖你的系统Perl 学习 阅读通过perlbrew.pl 重复 ...
-
这里有一些奇怪的事情。 首先:当你use strict激活时,如果你使用一个变量而不声明它, 或者用完全限定名引用它,你会得到一个警告。 你实际做的是在xyzzy.pl中用my()声明一个本地的%cfg ,然后引用一个不同的包全局变量%main::cfg (通过使用它的完全限定名隐式声明)。 要使引用链接与您声明的%cfg相同,您应该声明它为our()以使其成为package-global。 然后你可以在两个地方以$main::cfg{}方式引用它(或者从xyzzy.pl只需$cfg{} ),否则你可以在p ...
-
Perl Regex问题(Perl Regex issue)[2022-04-21]
有人可以解释为什么数组有4个元素,而不是2个? 你在拆分时也捕获分隔符,所以有4个元素而不是2个。 来自http://perldoc.perl.org/functions/split.html 如果PATTERN包含捕获组,则对于每个分隔符,将为组捕获的每个子字符串生成一个附加字段... Can someone explain why the array has 4 elements, instead of 2? You're capturing delimiters as well when splitt ... -
我认为sub_b是由包含sub_a的包导入的? 您正在更改名称package_of_sub_b::sub_b引用的package_of_sub_b::sub_b 。 您没有更改名称package_of_sub_a::sub_b引用的package_of_sub_a::sub_b 。 如果您正在解析第二个名称,那么您需要覆盖包含sub_a的包中的sub_a 。 override_sub( 'package_of_sub_a::sub_b', ... ); I presume sub_b is importe ...
-
问题与Perl Regex(Issue with Perl Regex)[2024-01-29]
一堆| 你得到的,是从一开始就不合格的评论。 因此,解决方案是忽略所有“不合适”的行。 所以,而不是 $line =~ s/.*\s+//; 使用 next unless $line =~ s/^127.*\s+//; 所以你会忽略除了从127开始的每一行。 The bunch of | you get, is from the unfitting comment-lines at the beginning. So the solution is to ignore all "unfitting" l ...