ASP.NET MVC 5 Where to begin.

Hello Experts,

I would like to start learning how to use MVC. I have been using ASP.NET Web Forms since version 2 but I have never attempted to learn MVC. Everyone seems to be using MVC and all of my applications I developed have been Web Forms using ADO.NET for data access which I code myself.

I was looking at the http://www.asp.net website under MVC at the tutorials. I see that there are two ways to go about Data Access with MVC. I see that you can code first which populates the database for you. But I was not sure if that is the preferred method when working with Data Access from SQL using MVC or if there is another method such as creating database first then use MVC. Also, what method is easier and less buggy if  you need to modify the database after creating it first using the code first method.

Any help would be greatly appreciated. The web forms that I have created involved CRUD on data from SQL and I have used CRUD for populating data and mofifying data from DropDownList, CheckBoxs and RadioButtons. Is this easy to do with MVC?
LVL 4
asp_net2Asked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
MVC doesn't have server controls in the way that WebForms does, so there isn't a CheckBoxList. You can roll your own like (warning: simplified example!):
@model MvcApplication1.Controllers.Person

<div>
    <span>Title:</span>
    <ul>
        @foreach (var item in ViewBag.Titles)
        {
            <li>
                <input id="title"
                       type="checkbox"
                       value="@item"
                       @(item == @Model.Title ? "checked" : "") />
                <span>@item</span>
            </li>
        }
    </ul>
     
</div>
<div>
    <span>First Name:</span>
    @Html.TextBoxFor(p => p.FirstName)
</div>
<div>
    <span>Last Name:</span>
    @Html.TextBoxFor(p => p.LastName)
</div>

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
You can do everything with MVC that you could do with WebForms, it's just the mechanics and the semantics of how you render pages that is fundamentally different.

Data access methods aren't specific to MVC, whatever you used for WebForms will still work with MVC. Having spent time as a DBA I prefer the database first approach to developing the model, but your preference might differ. They all end up with essentially the same result, so i'd suggest having a play with each and seeing which you prefer.
0
 
asp_net2Author Commented:
Hi Carl Tawn,

Ok, I will try the Code First approach. I keep seeing that in all the tutorials that I look at for MVC. What about populating the data to DropDownList, CheckBoxs and RadioButtons. How is that done using MVC?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Carl TawnSystems and Integration DeveloperCommented:
Normally you return a Model and a SelectList to your view. The SelectList is used to populate a DropDownList, etc, with selectable values. The Model is then bound to the list in order to select the current value.

See if this example makes it any clearer:
http://www.aspnetmvcninja.com/views/asp-net-mvc-select-list-example
0
 
asp_net2Author Commented:
Ok, That was for a select list. I was looking on how to populate data to DropDownList were you can only select one value. I can look into that myself though on Google. Can you provide me an easy way to understand what Model, View and Controller are and how they operate along with there duties in an easy way that I may be understand?

Sorry, just having a hard time wrapping my head around this from Web Forms :)
0
 
Carl TawnSystems and Integration DeveloperCommented:
In simplistic terms the Model encapsulates your data. The View presents the Model to the client. And the Controller acts as the conduit between the two, retrieving data (Model) from a data store and sending it to a View.

Maybe this simple example will help. I've made it an edit view and included a DropDownList just to demonstrate. This could be made a bit slicker by wrapping the Model and lookup options in a ViewModel, but if you're just starting out it's probably best not to confuse things further :)

This is the controller and a simple Model (the model is in the same code file just for brevity):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ViewResult Index()
        {
            // create a list of lookup values and add it to the ViewBag
            List<string> titles = new List<string> { "Mr", "Mrs", "Miss" };
            ViewBag.Titles = titles;

            // create an instance of our Model
            Person person = new Person() { Title = "Mr", FirstName = "Carl", LastName = "Tawn" };
            
            // return a View passing our Model
            return View(person);
        }
    }

    // this is our Model
    public class Person
    {
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

Open in new window

This is the Index view:
@model MvcApplication1.Controllers.Person

<div>
    <span>Title:</span>
    @Html.DropDownListFor(p => p.Title, new SelectList(ViewBag.Titles))
</div>
<div>
    <span>First Name:</span>
    @Html.TextBoxFor(p => p.FirstName)
</div>
<div>
    <span>Last Name:</span>
    @Html.TextBoxFor(p => p.LastName)
</div>

Open in new window

0
 
asp_net2Author Commented:
Ok, now how would you add that data to a CheckBoxList Control?
0
 
asp_net2Author Commented:
Ok, just one more question now. How do I implement the Security in MVC 5? I would like to use the default authentication where you add users to SQL DB. However, I would like to add the Security Tables and Stored Procedures for my existing Connection string that I created based on my sample from this website.

Example that I created today: http://www.asp.net/mvc/tutorials/mvc-5/database-first-development/setting-up-database

My webconfig file looks like the following below. I setup my own SQL Server DB and did not use the default one "LocalDB" that ASP.NET MVC uses out of the box.

<connectionStrings>
    <add name="ContosoUniversityEntities" connectionString="metadata=res://*/Models.ContosoModel.csdl|res://*/Models.ContosoModel.ssdl|res://*/Models.ContosoModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=NDDING\SQLEXPRESS;initial catalog=ContosoUniversity;user id=sa;password=w$ql;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
There are several options. You can roll your own if you just want basic user/password authentication.

The more evolved options are listed here, including the ASP.Net Identity, which is the replacement for ASP.Net Membership: http://www.asp.net/mvc/overview/security
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.