Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET MVC 5 Where to begin.

Posted on 2014-04-08
9
Medium Priority
?
815 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

670 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