user107986
user107986

Reputation: 1541

What sort of syntax is this?

public class MyViewModelClass
{
   public string Name { set;get;}
} 

public ActionResult MyAction()
{
  return View(new MyViewModelClass());
}

@model MyViewModelClass
@using(Html.BeginForm())
{
  MyAction Name : @Html.TextBoxFor(s=>s.Name)
  <input type="submit" value="Generate report" />
}

Could anyone tell me what the following line do?:

MyAction Name : @Html.TextBoxFor(s=>s.Name)

OK, the first word is just specifying the action to perform, but I don't get why someone needed to put 'Name' in this expression and follow it by a colon. Yes, I know what TextBoxFor does.

I've never seen syntax like this before in any ASP.NET tutorial, maybe you can give me some links where I can find more about it. Thanks!

Upvotes: 1

Views: 118

Answers (2)

trashr0x
trashr0x

Reputation: 6565

What you are seeing is the Razor syntax which allows you to add C# (or VB, depending on what you're using) code in your HTML using the @ character. It also comes with a bunch of helpful classes such as the HtmlHelper class you asked about (more on this below).

As far as this line is concerned:

MyAction Name : @Html.TextBoxFor(s => s.Name)

The MyAction Name : part will simply print "MyAction Name :" in your HTML. The @Html.TextBoxFor(s => s.Name) part utilises the TextBoxFor method of the HtmlHelper class from the System.Web.Mvc namespace to render a TextBox for the Name property of your MyViewModelClass (which is declared as the model of your view using the @model directive) in your HTML markup.

I've added some links which will help make things clear for you and get you up to speed in no time.

Upvotes: 4

Sirwan Afifi
Sirwan Afifi

Reputation: 10824

That's a simple string:

MyAction Name : @Html.TextBoxFor(s=> s.Name)

So first you see MyAction Name : in the result then @Html.TextBoxFor(s=> s.Name) generates a text box after that.

Upvotes: 1

Related Questions