2

I see a lot of codes like this:

@Html.LabelFor(x => x.username)
@Html.TextBoxFor(x => x.password)

Those are just examples. Could anyone please explain to me what does that x => x.username part mean? I still use html tags <input type="text"> like this in my asp.net c# mvc application because I don't get a hang of @Html.TextBox style.

There's also new parameter like this:

@Html.TextBoxFor(x => x.username, new {placeholder = @Html.DisplayNameFor(x => x.username})

I don't understand what all these mean. Could anyone please explain this?

Thanks in advance.

2 Answers 2

1

Most of your questions can be solved by doing research. It appears you're unable to determine, how to find the answer to your questions. This should help you now and in the future determine how to find the answer to most questions like this.

Could anyone please explain to me what does that x => x.username part mean?

Well lets do some research. The first step is, what does the method signature look like. A simple search for LabelFor on google and our first link reveals..

https://msdn.microsoft.com/en-us/library/system.web.mvc.html.labelextensions.labelfor(v=vs.118).aspx

With a bunch of signatures, I'll choose the first one because it matches your first code example..

@Html.LabelFor(x => x.username)

matches:

public static MvcHtmlString LabelFor<TModel, TValue>(
  this HtmlHelper<TModel> html,
  Expression<Func<TModel, TValue>> expression
)

I see the first parameter begins with this, which denotes a Extension Method which is beyond the scope of your original question so I'll ignore it.

The second paramters is a Expression<Func<TModel, TValue>> expression. Google searching Expression Func yields quite a number of useful results..

What does this mean: Expression<Func<TModel, TValue>>

or more specifically:

Expression<TDelegate>

Etc etc. Hopefully now you have the tools to do the research.

Suggested reading: C# Delegates, Anonymous Methods, and Lambda Expressions – O My!

0

x => x.username is a lambda expression. You should read more about anonymous methods and delegates. But those strongly typed html helpers accepts a generic delegate called Func<in T, out TResult>( can't remember the exact signature ) which represents a function that takes a T and returns a TResult. The T is your @Model.

@Html.TextBoxFor(x => x.UserName) = <input type="text" name="UserName" id="UserName"/>

@Html.TextBox("UserName") same as above. But that red string is bad.

Try to keep your view strongly typed so that you can rename properties and not worry about all the views you have to manually update aswell.

new {placeholder = @Html.DisplayNameFor(x => x.username} is an anonymous type. Take a closer look at the intellisense and see what type of things the methods accept. In this case an object. Which an anonymous type is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.