0

Right now I just output my value(s) like

<input type="text" class="form-control" value="@Name" id="name" placeholder="Name">

But I've seen a lot about @HTML display something?

Should I use HTML display, and if so, how and why?

Per Petter
  • 71
  • 5
  • If your want to use the powerful features of MVC including 2 way model binding, and client and server side validation, then yes. –  Feb 08 '15 at 04:58
  • @Stephen, not true. You can still use 2 way model binding without using Html. It just makes it easier. Html just re-writes your Html to look like this anyways. – Scottie Feb 08 '15 at 05:01
  • @Scottie, Are you suggesting that OP write all the extra code to read the `ModelState` values when returning a view (at best all OP's code is reading the model value, not the `ModelState` value which is required for true 2 way binding) –  Feb 08 '15 at 05:03
  • @Stephen: No... the MVC binding works off of the Id field. As long as your control has an Id that matches the model, it will bind just fine. (Note: It might be the Name field... I can't remember) Take a look at the html that is generated by RAZOR. It looks almost identical to what OP has posted. If your model has a property "public string UserName" and you create your own html input tag, as long as it has Id="UserName", it will bind just fine. – Scottie Feb 08 '15 at 05:18
  • @Scottie. Firstly, I can't see any real point using MVC if you just going to create your own html and ignore features such as client side validation (i.e. all the `data-val-` attributes the helpers render). Next the helpers take care of all the encoding. And as far as the `ModelState` issue, the last part of [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) will help explain. And then of course, using the helpers is usually less code, and is strongly typed to your model. –  Feb 08 '15 at 05:23

3 Answers3

1

The Html helper class is there to encapsulate writing a lot of html code in a single call:
- the actual field
- validation rules according to your model Field Attributes
- the validation place holder
- Value binding

additionally, allows you to change the behavior and html of all your fields in case needed, in one spot.

it is, like many other things, a tool...
use it if you wish... for my opinion, it rule and should be used.

Tomer W
  • 3,395
  • 2
  • 29
  • 44
0

You can do it either way. @Html.TextBox and @Html.TextBoxFor will automatically generate the html for you, but you can do it yourself if you want.

Personally, I prefer writing my own html, but that's just me...

Scottie
  • 11,050
  • 19
  • 68
  • 109
  • Yes, I prefer writing my own HTML, it's just a habit, but I was also concerned about XSS if I just output my value like @Name – Per Petter Feb 08 '15 at 05:06
  • you can use the antiforgerykey helper to prevent xss no matter if you build your form elements via helpers or not. – Bart Calixto Feb 08 '15 at 05:42
0

The @ symbol allows you to access c# code within your cshtml file so you can mix html and c# code to generate a final html output. For more information, search for C# Razor syntax, here's a link for more info http://www.w3schools.com/aspnet/razor_syntax.asp and http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/

The benefit of using Razor syntax is the ability to access helper c# methods, global variables, view model variable object passed from the controller, view compile time errors (by default, it's off in visual studio, check http://haacked.com/archive/2011/05/09/compiling-mvc-views-in-a-build-environment.aspx/ for more info), you don't need to use javascript to populate a form or other fields dynamically ...etc

The downsides are your code needs to be compile to html so browsers can process it, a syntax error can crash your whole page, your cshtml code is not portable in case you decide to switch backend engine, ...etc

Guy
  • 2,883
  • 1
  • 32
  • 39