Difference between @html.TextBox and @html.TextBoxFor

Difference between @html.TextBox and @html.TextBoxFor.

My answer on Quora:

In abstract terms, the difference between the html helpers @html.TextBox() and @html.TextBoxFor() are the former is weakly typed and is independent of model and the latter is strongly typed and it should be bound to one of the model property in strongly typed view.

Consider the below piece of code,

This will be rendered as shown in the below image.

When you use, TextBoxFor, while the HTML is generated, it will automatically map the value from the model property to it, in the above case “UserName” . So when you post the values of the current page, if your controller is having your model as the parameter, the value can be retrived directly by accessing the property.

When you look at the above screenshot, the model will not hold the weekly typed “simpleTextBox”.

Hope you find it useful.

Can any one explain me the difference between @html.TextBox() and @html.TextBoxFor() in ASP.net MVC?

Model Binding and Strongly Typed Views

Model Binding and Strongly Typed Views

Answer by Sharmilli Priyadarsini:

In order to demonstrate model binding, I am making use of the default scaffold for Account registration available when you create a new web application with Visual studio.

Let us see how to create a strongly typed view using the below model,

Now, from your solution explorer, expand the controller folder and open the AccountController.

You will find something like the below action method,

Right clicking near the ActionResult and selecting go to view will navigate you to the Register.cshtml view which is strongly typed with “RegisterViewModel”. In order to make a view strongly typed, you can either do it through api or marking the model on the top of page as shown below:

Doing, so will generate you a strongly typed view as Register.cshtml bound to RegisterViewModel inside the accounts folder in views.

This is how you can create a strongly typed view. Now let us get into the detail of how to design your view by making use of the above model.When you take a close look of the above screenshot, you will see set of html helpers i.e all that follows “@html.” are said to be html helpers. Why because, they will generate the equivalent html to generate your web pages.

Let us take the below example
@html.LabelFor(m=>m.UserName, new{@class=”my-label”})
@ in the above line of code is known as razor syntax. Everything that follows the razor syntax will be rendered at the web server and are C# code in the .cshtml page and vb code in .vbhtml respectively.
html. represents that a html helper so the equivalent output will be a html code generated by the web server. we have different types of helpers like html helper, ajax helper etc.

LabelFor will generate a html label with the name user name which is strongly typed. But usually the label’s will not post the values to controllers they are just plain text. But if we have other controls like textbox, checkbox we can retrive their values in the controller.

Hope this clears your doubt.

I am getting quite confusion in binding the model data in view page, can any one explain me? How exactly it is done?

What is the practical usage of sharing service contract assemblies in WCF?

Answer by Sharmilli Priyadarsini:

Straight forward answer is, If you do not mark a class/interface as a service contract, it cannot be consumed as the service. The function of service contract is to serialize the class, in order to be consumed from other applications written in various platforms.
A practical example:
Just follow the below steps,
1) File >> New >> Project
2) Select WCF project as shown in the image below.

3) If you open the IService.cs file, you will see the service contract attribute added to your interface.
4) Now set your svc.cs as start page by right clicking and setting it as start up page. If you run your project, WCF test client will open, where you will be able to see the service contract under which the methods written inside it and test the service methods by passing the desired parameters.

Let us come to the actual problem, what happens if we do not have the service contract attribute over the interface?

5) Go to your IService.cs and remove the ServiceContract attribute.

6) While running the project it, the WCF test client throws a error saying that “Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata”.

Contracts are meant for serialization purpose. If you want to expose a class/interface, you need to attribute it as service contract.

Hope it solves your query, Happy Learning!

What is the practical usage of sharing service contract assemblies in WCF?

What websites would you recommend to me for learning a .NET MVC (no music store or movie DB) project step-by-step online?

Answer by A Quora admin:

Though you have clearly mentioned no music store, I would certainly recommend you to do music store since it gives you a step by step approach to build your MVC application and hence by the end of the tutorial, you will have a confidence that you will be able to do better with MVC, though it is not enough.

Now, my answer to your actual question is, http://asp.net/mvc is a wonderful source to begin and brush up your mvc skills than any other available online tutorials. It is one step destination to MVC where microsoft offer step by step tutorials, best practices, new features, free pluralsight videos on the recent version of mvc and on and on.

What websites would you recommend to me for learning a .NET MVC (no music store or movie DB) project step-by-step online?

How do I upload a file using asp.net?

Answer by A Quora admin:

In order to upload a file using ASP. net, you need to follow the following steps.
1) In your .aspx page where you want to have your file upload functionality drag and drop the file upload control from the tool box.

2) Choose the event upon which you need to upload the file. In my example I have chosen to upload the file on a button click.

3) Now that I want my upload code need to be written in the Upload_Click event I go to the .aspx.cs page by double clicking on the Upload_Click action from the properties window and code as shown in the below screenshot.

4) For uploading the file to the server, replace the save Location path by the application server path where the user has access to. code to get the server path is Server.MapPath("Data") .

For further reading refer : Page on microsoft.com

Hope it helps. Happy coding!

How do I upload a file using asp.net?

How to do I display a grid or a table in the drop down list for multiple columns with a header in asp.net with MVC4?

Answer by A Quora admin:

Check this fiddle, Table in dropdownlist written in kendo UI. If you are looking for something like this, you can write a custom html helper and make use of it. Make sure you use table tr td tags added to each to select and option tag. A sample of it for your purview.
ASP.NET MVC Custom dropdown from model with custom HtmlHelper

How to do I display a grid or a table in the drop down list for multiple columns with a header in asp.net with MVC4?

What are the competencies needed to build web apps with a Microsoft stack?

Answer by A Quora admin:

To develop web application using microsoft stack, you can follow either one of the below combinations
1) ASP .NET + any .net framework supporting languages preferably C#/VB/C++ & database (SQL server is native microsoft RDBMS) for dynamic web pages.
2) Sharepoint (For file management, content management, basic web forms, etc) uses SQL server by default, requires C# ,css & javascript for development and administration skills are enough for using it as a CMS that comes in handy with lot of templates.
3) ASP .NET MVC + Javascript & JQuery + JSON +HTML + CSS + C#/VB/C++ and/or LINQ and/or Entity framework + SQL Server (or other DBMS).

What are the competencies needed to build web apps with a Microsoft stack?