@Html.EditorFor(m => m) lambda syntax in MVC

前端 未结 4 1431
野趣味
野趣味 2021-02-05 16:08

I\'m just learning C# and MVC, and trying to understand some examples.

@Html.EditorFor(m => m)

Eventually I figured out that \'=>\' is the l

4条回答
  •  鱼传尺愫
    2021-02-05 16:27

    1.

    • @Html.EditorFor(m => m) - display editor for whole model
    • @Html.EditorFor(m => m.propertyName) - display editor for specific property of model

    2.

    @Html.EditorFor(m => m) is equal to @Html.EditorFor(t => t) or @Html.EditorFor(randomName => randomName). Name doesn't matter, it is just parameter's name. The type for this parameter is type of view model.

    You have to pass function, because it is not only value, that counts. Reflections are used to get attributes, that describe how to display property. Look at this example

    public class ResetPasswordModel
    {
        public string Username { get; set; }
    
        [DataType(DataType.Password)]
        public string NewPassword { get; set; }
        [DataType(DataType.Password)]
        public string PasswordConfirmed { get; set; }
    }
    

    Attributes describe, that NewPassword should be password field, not regular input. If we passed value, that would not be possible.

    In our example @Html.EditorFor(m => m) will show for containing one input for user name and two password inputs for passwords. @Html.EditorFor(m => m.NewPassword) will show input, which has type of password.

    3.

    http://msdn.microsoft.com/en-us/library/ee402949.aspx

    public static MvcHtmlString EditorFor(
        this HtmlHelper html,
        Expression> expression
    )
    

    This is extension method for HtmlHelper class. this HtmlHelper html is not a parameter, it is type of class, that function extends.

提交回复
热议问题