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?

What should I choose, Angular.js or .NET MVC?

Angularjs vs ASP.NET MVC

Answer by A Quora admin:

Angularjs is a javascript framework written in MVVM pattern, But works almost in a similar fashion to MVC. .NET MVC is a server side technology written on the top of .NET framework  that converts your HTML helpers into the html tags, apply roles and styling and works alongside with the JQuery as a fully functional web pages. Where as angularjs is a javascript framework that does the similar action on the client side.

Precisely the answer is, You can very well use angularjs along with MVC. However, whenever you are dealing with complex views or when your model is bound to multiple DOM objects, I will recommend you to choose MVVM over MVC.

What should I choose, Angular.js or .NET MVC? Which language has better career prospects?

ASP.NET MVC vs ASP.NET?

Both ASP.NET webforms and MVC  are microsoft technologies available for building dynamic web pages.  The following are the reasons why one should you prefer one technology over the other.

Why MVC ?
1) Lightweight and fast : The  ASP.NET follows the event driven approach that strongly binds your UI with the server side event handelling. Whereas in case of MVC, the UI and the server side logic are decoupled and makes them independent entities. Also, MVC got rid of one of the state management techniques called viewstate that makes the asp.net pages fat and takes time to load the controls.
2) Highly customisable due to rich support of HTML, CSS, Javascript, AJAX and JQuery : If you look at your design files of MVC, they are plain HTML controls along with server side HTML helpers that will again present the web pages with clean DOM unlike  ASP.NET webforms that generate complex html tags and attribute values while presenting the webpages to the user.
3) Highly modular with clean url support: Clean URL is something very native to MVC pattern, because the route engine process the URLs directly in case of mvc (default format is domain_name/controller_name/action_name/additional_parameters) whereas in webforms the request will try to look for the equivalent aspx page and the url will look too long and unorganized in most of the cases.
4) SEO: As I told you before the mvc webpages will present a very straight forward HTML webpage with clean url that make sense not only to the users but also for the search engines to index your websites. So obviously your websites will be prefered by google or other search engines whenever some one looks for information relevant to your business, that will result in more hits and more business.

Why ASP.NET (Webforms)?
1) Legacy : Being a pioneer technology, we have a lot of applications built in ASP.NET already and it is practically difficult to invest a lot of money for rewriting those applications in mvc. So the enhancements to the existing system will preferably be written in the same software stack, instead writing them on something new.
2) Availability of skill pool: We have a huge skill pool of  ASP.NET developers as it is there for quiet a long time and MVC is comparitively very young technology. So if your project has minimal time to train the resources, going with a familiar software stack is preferable.
3) Not so many dependent technical skills: If you say that you are good in MVC, you are expected to be equally good in HTML5,javascript,JQuery and CSS along with C#.
4) Continuous support from microsoft: Microsoft is providing dedicated support for ASP.NET and continuously investing in R&D to make it better. One of the few significant steps of it is vNext that is aimed at making it lightweight.

What is the difference between ASP.NET MVC and ASP.NET?

What are 3-tier and 2-tier architectures in .net?

Answer by Sharmilli Priyadarsini:

3-tier architecture and 2-tier architecture is not something restricted to .NET. These are architectural designs meant for sharing the load of the servers by hosting the tiers in multiple servers.

In case of 2-tier architecture, there will be an application tier and a data tier. The conventional window application that has the client application installed in your local computers and communicate with a centralised database located in some other machine is a good example for two tier architecture.

In case of 3-tier architecture, we usually have an application tier, business tier and a data tier. The application/presentation tier will have the UI related components like web pages and client interfaces. The Business tier will have all the business logics and the Data tier will have the database.

The communication between the tiers will happen in such a way that a reference to the tier will be added to another tier in order to create the objects of classes in other layer and access the members. In case of 3 tier architecture, presentation and data tier will not be able to bypass the intermediate business tier.

What are 3-tier and 2-tier architectures in .net?

When would I use = vs == in programming?

Answering a programming beginner.

Answer by Sharmilli Priyadarsini:

= is an assignment operator, we use a "equal to" symbol whenever we want to assign right hand side RHS value to the left hand side LHS.
E.g:

int a = 1;

// the value 1 is assigned to the variable "a", whenever you use "a" it automatically takes the value of "a" as 1 as long as it is not modified to some other value within the scope of the variable.

== is a logical operator, that you usually use while checking the left
hand side LHS is exactly equal to right hand side RHS. The other logical operators are < Less than ,> greater than,<= less than or equal to ,>= greater than or equal to ,!= not equal to. The output of logical operator is always true or false. You will be using this operator in condition statements like if and ternary operators uses both = and ==.
E.g:

int a=1;
int b= 2;
int c= 1;

//Conditional operator returning true or false uses ==

if(a==c){

//true since value of a and c are equal ie 1.

a=b;// assignment operator uses =, now value of b is assigned to a, so value of "a" is equal to 2.

}

//ternary operators also uses == to check for true or false, followed by assigning the  values to LHS from true section if condition in == satisfies, otherwise assign the false section to LHS.

// now a,b are equal with the value 2 and value of c is 1

c= a==c?c:b;

// The above is a ternary statement, first it checks if value of a is equal to c, if it is equal then c remains same, otherwise b overwrites the value of c.

//  Now a,b,c are all equal with the value 2

When would I use = vs == in programming?

As a Computer Science student, would it be better to take an internship where I’ll be programming in C++ in a Linux environment, or one w…

Answer by Sharmilli Priyadarsini:

I would suggest you take your internship on C++ on Linux. The reasons are,
1) though C# (C++++) is claimed to be Microsoft's improvised version of C++ , C# is just a dedicated Microsoft language that is written to make the best use of. NET framework, C++ had richer features over the latter and powerful enough to write system softwares. In fact, MEF is brought to. Net only in version 4.0 which used to be there in visual C++ forever.
2) Startups usually go for open-source software stacks instead going for Microsoft stack due to the licensing cost. So C++ is a recommended choice.
3) As a fresher,  your .Net skills are least important than your C, C++ and oops skills during the interviews.
4) You will probably be given with a .Net training by the company if you get into .Net stream in service conserns.

As a Computer Science student, would it be better to take an internship where I'll be programming in C++ in a Linux environment, or one w…

What is the typical thought process of an experienced programmer doing a code review of a new developer who is a fresher?

Answer by Sharmilli Priyadarsini:

Inspite of the experiences, people make mistakes, but the number of mistakes done by a fresher will be greater than the experienced. Moreover, you cannot learn if you do not make mistakes. A senior programmer doing the code review of another, will look for the same mistakes he did in the early stages of his career and advise his peer how to fix the mistakes and about the best practices to be followed while coding. Sometimes the code review will be a learning experience for the senior programmer also.

All we have to do is, being open for the comments and share your knowledge despite your experience.

What is the typical thought process of an experienced programmer doing a code review of a new developer who is a fresher?

Which standards must developers know for programming?

Programmer's checklist

Answer by Sharmilli Priyadarsini:

The below are few things to be kept in mind while coding,

1. Make your code readable with proper indentations.
2. Follow the coding standards recommended. This depends on the language and the practices followed in your project.
3. The naming convention should always be related to the functionality. A text box that invites employee names can be named as EmpName instead being called as TextBox1.
4. Make sure all the methods/functions are annotated explaining the function. Add your inline comments inside the code blocks whenever there are complex steps or logics handelled.
5. Using the right source control and IDE can really help.
6. Do not try to reinvent the wheel, If there are any inbuilt library functions available or tested algorithms available to serve your purpose, reuse them. This will improve the efficiency of your program.
7. Follow the best practices prescribed by the programming language.
8. Most importantly learn and practice.

Which standards must developers know for programming?

Changing your kendo grid’s row colour on selecting your rows

Dear readers,

It has been a while since my last technical post. This time, I am going to blog about a very common row colouring functionality using kendo grids. With a little bit of styling knowledge and JQuery, we are very good to go.

Kendo UI Controls are provide us a high degree of user interactiveness by making use of the best features of HTML5, CSS3, AJAX and JQuery for developing state of art mobile and desktop web applications. I will demonstrate the colour changing functionality with my working sample created in JS fiddle.

http://jsfiddle.net/sharmilli/pLsemde8/embedded/

 

in the javascript section and css section of the fiddle, you will be able to follow the script with inline annotation .

Hope it helps, Happy Learning!!!