Mikkel
Mikkel

Reputation: 1865

MVC 5 coming from WebPages

I've been using WebPages for a while now and I find it quite amazing, though, I hear a lot of bad things about it mainly because it's not very good for large projects. What is the reason for this? Is it because of the performance? I'm not sure myself.

Anyway, I decided to try out MVC as it's the most respected and most used ASP framework, and I see that it uses Razor syntax as well, which I am a big fan of.

So; I've been following the MVC 5 MovieDb tutorial on www.asp.net but for example, in that tutorial you learn how to create, edit, delete and view movies on the website, but the problem is, I really don't like the way Visual Studio generates the entire thing for you. I prefer to do that kind of stuff by myself.

For example; In WebPages you would use a foreach to show the data you pull from the database, which means you can show the data exactly the way you want to show it, but it seems that in MVC whenever you generate a Controller, it shows it in a grid for you.

Is there any other recommended tutorials that I can use where you learn how to make it yourself?

And; I'm wondering, in MVC, do you have to use those custom input fields etc.? Because in WebPages you can simply use a regular HTML input field and give it a name. But in MVC it seems it uses something like @Html.InputField (I don't remember what it was called).

Sorry for this messy post, but I'm pretty confused.

Thank you in advance.

Upvotes: 2

Views: 99

Answers (2)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

Visual Studio doesn't have to generate things for you. It just provides the tools to help you be more productive. If you know how an MVC project works, you can do it all by hand. It's your lack of knowledge that prevents you from doing that at this point. As a novice, these tools help you get going a lot faster, and as you learn how to do things in more detail you can decide whether to keep using them or do them yourself.

You also do not have to use Html helpers, but they take care of a lot of things for you that you would otherwise have to deal with yourself. Again, it's about productivity. It also helps you to do things that are more complex so you don't get them wrong and create bugs.

The biggest example of where Html helpers shine is with validation. If you don't use the helpers, then you're going to have to do all the validation code (both client and server) by hand.

Another place these helpers are important is when you use nested models. MVC model binding requires a specific syntax in the naming of your variables, and the helpers assist you in getting this right.

One other place that helpers are important is in processing the metadata associated with models. For instance, let's say you have a model for a users shopping cart. And you use this model on literally a hundred pages. If you use your template helpers to define the field names using the [Display(Name="Shopping Cart")] attribute, and you decide to later change a name like.. Cart to Basket, then you need only change the model and it gets reflected everywhere it's used.

Finally, helpers also provide a very powerful templating system not that different from the old ASP.NET controls such as Repeaters, and what not. These are called Editor Templates, and use the syntax @Html.EditorFor(). You can create modular template files that get rendered whenever you use an EditorFor on a specific type, and they will automatically iterate over your collections applying the markup you define.

Simply put, these helpers may seem annoying or pointless, or just like they are more work... but you will quickly find that it's actually more work to NOT use them once you get beyond very trivial pages.

Upvotes: 1

Mike Brind
Mike Brind

Reputation: 30035

The "bad things" you hear are generally from people who don't know what they are talking about or who have a very slanted view of the world.

Web Pages is one of the three ASP.NET development frameworks. MVC and Web Forms are the other two. There is nothing "wrong" with any of them, but each one of them has its pros and cons. At the moment, I develop/maintain sites built using all three options. The performance across all of them is to all practical intents, the same. They are all based on ASP.NET.

Razor was initially developed for use in the Web Pages framework. It was subsequently borrowed by the MVC team as a view engine.

You can use HTML input fields in MVC just like in Web Pages. You can also use these @Html input fields in Web Pages (they're called Html helpers). I wrote about them a while ago: http://www.mikesdotnetting.com/Article/184/HTML-Helpers-For-Forms-In-Razor-Web-Pages

One of the things that MVC encourages is to take a strongly typed view of the world. This means that you deal with custom objects/models in your views. One type of Html helper in MVC is strongly typed and automatically binds to properties of your view. Using these helpers makes your life easier as an MVC developer. You will see this as you progress through learning MVC.

Upvotes: 1

Related Questions