Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.NET MVC 5 Where to begin.

Posted on 2014-04-08
9
Medium Priority
?
824 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

916 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