Solved

ASP.NET MVC 5 Where to begin.

Posted on 2014-04-08
9
799 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 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