0

i don't like auto-generated code so i made a decision to never use Asp.net MVC's Html Helpers, but i face a problem when it comes to save the value of an input after validation failed, so the value is saved when i use

@Html.TextBoxFor(m=>m.Name)

but it doesn't save the value when i write

<input type="text" name="Name"/>

so how can i have the same thing without using TextBoxFor Helper, and what does this helper do, that i haven't

1 Answer 1

2

i don't like auto-generated code so i made a decision to never use Asp.net MVC's Html Helpers

You are going to have a hell hard time doing ASP.NET MVC development if you do not use helpers and I am afraid that you will not get very far. You will struggle not only with things like setting values for input fields but also emitting HTML5 data-* attributes on your input fields for things like unobtrusive client side validation, all things that are automatically handled by HTML helpers.

You could always perform the following pornography in your view if you find that more readable:

<input type="text" name="Name" value="@Html.Raw(HttpUtility.HtmlAttributeEncode(Model != null ? Model.Name : ""))" />

But I don't know why I kinda prefer writing @Html.TextBoxFor(m => m.Name). I don't know, maybe it's just a matter of personal preference.

But to answer your question, you can do ASP.NET MVC without helpers. It's just that you might find that a pretty disagreeable experience.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you, but one more question, why it doesn't work when i write manually the same thing generated by @Html.TextBoxFor(m => m.Name)
Because the HTML helper already takes the model into account. The helper is designed exactly for that purpose. There's such logic built into the helper. It will check whether your model property has a value and emit the corresponding value attribute on the input tag. It will also take care into properly encoding this value so that it doesn't break your markup and so on.
how can i read the helper template, to know exactly what it does (it's psychic i'am always wasting time learning how infrastructures works while others learn how to use it)
An HTML helper is an extension method. There's no template. You will have to look at the corresponding C# code. Since ASP.NET MVC source code is available you could always look at it: aspnetwebstack.codeplex.com/SourceControl/changeset/view/… Or simply use a tool such as Reflector to directly browse it from the System.Web.Mvc assembly.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.