(m => m.Email)的含义(ASP.NET)以及如何获取用户的用户名 [英] (m => m.Email) meaning (ASP.NET) and how to get a user's username

查看:201
本文介绍了(m => m.Email)的含义(ASP.NET)以及如何获取用户的用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是网络开发的新手。我有什么小的经验是在C#Unity和Java中。下面的代码是从屏幕上获取的,然后是Google认证/登录屏幕。我正在尝试将用户的Google姓名设置为网站上的用户名。我想知道是否有人可以帮助我,也许可以解释(m => m.Email)的含义。



非常感谢!

  @model Start.Models。 ExternalLoginConfirmationViewModel 
@using(Html.BeginForm(ExternalLoginConfirmation,Account,new {ReturnUrl = ViewBag.ReturnUrl},FormMethod.Post,new {@class =form-horizo​​ntal,role =form} )){

@ Html.AntiForgeryToken()

< hr />

< div class =form-group>
< div class =col-md-10>
@ Html.TextBoxFor(m => m.Email)
< / div>
< / div>
< input type =submit/>
}
@section脚本{
@ Scripts.Render(〜/ bundles / jqueryval)
}


解决方案

这里稍微有些技术性的解释。 => 运算符表示一个lambda表达式,它可以被认为是一个返回其值的函数。左边的东西( m )是函数的参数,右边的东西是从函数返回的东西。鉴于此, m => m.Email 实质上意味着返回我在这里传入的电子邮件属性。



然而,当谈到像HtmlHelpers这样的事情时,作为第一个参数传递的信息实际上是 Expression< Func< TModel,TProperty>> ,所以让我们解开这个。 Func< TModel,TProperty> 是我们刚才谈到的lambda的实际类型。 Func 是一个泛型类型,带有两个(或更多)类型参数。在这里,第一个类型参数是 TModel ,并且对应于lamda的左侧的类型,即我们传入的东西的类型。 TProperty type参数是lambda右侧的类型,也就是我们要返回的东西。



表达式是一个应用于像 Func 之类的东西的包装器,并且构建了所谓的表达式树拉姆达。除此之外,这个表达式树允许检查lambda内部的内容,因此可以确定返回的属性名称等内容。然后,HtmlHelpers使用这些信息来执行诸如生成 id name 属性的东西,这显然需要匹配属性名称。这实际上是一个很重要的事情,因为许多新开发人员感到困惑,并认为他们可以将任何事情传递给HtmlHelper作为表达式。情况并非如此,因为你没有处理值,而是表示值的表达式。例如,以下内容根本不起作用:

  @ Html.TextBoxFor(m => m.Email.ToLower( ))

因为它不是一个有效的表达式,即使它适用于lambda。 / p>

I am brand new to web development. What little experience I have is in C# Unity and Java. The code below was taken from the screen followed by the Google authentication/login screen. I am trying to set the user's Google name to the username on the website. I was wondering if someone could help me out and maybe explain what (m => m.Email) means.

Thank you so much!

@model Start.Models.ExternalLoginConfirmationViewModel
@using (Html.BeginForm("ExternalLoginConfirmation", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" })) {

    @Html.AntiForgeryToken()

    <hr />

    <div class="form-group">
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Email)
        </div>
    </div>
    <input type="submit" />
}
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

解决方案

Here's the slightly more technical explanation. The => operator denotes a lambda, which can be thought of as a function that returns its value in place. The stuff on the left side (m) is the parameter(s) to the "function", and the stuff on the right side is what will be returned from the "function". Given that, m => m.Email means, essentially, "return the Email property of what I pass in here".

However, when it comes to things like HtmlHelpers, what you're passing as the first parameter there is actually Expression<Func<TModel, TProperty>>, so let's unwrap that. Func<TModel, TProperty> is the actual type of that lambda we just talked about. Func is a generic type, with two (or more) type parameters. Here, the first type parameter is TModel, and corresponds to the type of the "left side" of the lamda, i.e. the type of the thing we're passing in. The TProperty type parameter is the type of the "right side" of the lambda, i.e. the thing we're returning.

Expression is a wrapper applied to things like Func, and builds what's called an "expression tree" out of the lambda. This expression tree, among other things, allows the inspection of what's inside that lambda, so you can determine things like the name of the property that's being returned. The HtmlHelpers, then, use this information to do things like generating id and name attributes, which obviously need to match the property name. This is actually an important thing to recognize as a lot of new developers get confused and think they can pass in just about anything to an HtmlHelper as the expression. That's not the case, because you're not dealing with a value, but rather an expression representing a value. For example, the following would not work at all:

@Html.TextBoxFor(m => m.Email.ToLower())

Because it's not a valid expression, even though it works for the purpose of the lambda.

这篇关于(m => m.Email)的含义(ASP.NET)以及如何获取用户的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆