Solved

ASP.NET MVC 5 Where to begin.

Posted on 2014-04-08
9
780 Views
Last Modified: 2014-05-01
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?
0
Comment
Question by:asp_net2
  • 5
  • 4
9 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39986263
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
 
LVL 4

Author Comment

by:asp_net2
ID: 39986328
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39986346
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
 
LVL 4

Author Comment

by:asp_net2
ID: 39986362
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39986659
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
 
LVL 4

Author Comment

by:asp_net2
ID: 39986756
Ok, now how would you add that data to a CheckBoxList Control?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39986946
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
 
LVL 4

Author Comment

by:asp_net2
ID: 39987039
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39987067
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now