首页 \ 问答 \ 如何将自定义登录失败通知传递给MVC4中的视图(How to pass a custom login failed notification to the view in MVC4)

如何将自定义登录失败通知传递给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
更新时间:2022-10-22 17:10

最满意答案

这里有一些奇怪的事情。

首先:当你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 with my() in xyzzy.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 it our() to make it package-global. Then you can either reference it as $main::cfg{} in both places (or just $cfg{} from xyzzy.pl), or else you can declare it our() in plugh.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)?
  • 之前尝试一下 binmode($in); try this before while binmode($in);
  • 除了安装期间使用的常用构建和测试模块之外, 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 ...
  • 有人可以解释为什么数组有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 ...
  • 一堆| 你得到的,是从一开始就不合格的评论。 因此,解决方案是忽略所有“不合适”的行。 所以,而不是 $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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。