Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Wednesday, 13 March 2013

MVC 4 - How to launch the ASP.NET Configuration website


Watch on Youtube:


Go to Project/ASP.NET Configuration

MVC 4 - Simple example how to populate drop down list


Watch online:



1. Create new project (MVC 4)
2. Add entity connection
3. Add controller
4. Add
private CompanyTestDatabaseEntities db = new CompanyTestDatabaseEntities();
5. Add dispose method
         protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
6. Add Index Method - it will populate ViewBag with data from database
         public ActionResult Index()
        {
            ViewBag.GroupId = new SelectList(db.GroupMasters, "GroupId", "GroupName");
            return View();
        }
7. Whole Controller code looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestPassingToDropDown.Models;

namespace TestPassingToDropDown.Controllers
{
    public class FirstController : Controller
    {
        private CompanyTestDatabaseEntities db = new CompanyTestDatabaseEntities();
        //
        // GET: /First/

        public ActionResult Index()
        {
            ViewBag.GroupId = new SelectList(db.GroupMasters, "GroupId", "GroupName");
            return View();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
8. Create view and add the following code to it
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<div>
    @Html.DropDownList("GroupId", String.Empty)

</div>
9. That's it.

Tuesday, 5 March 2013

MVC - How to pass values to Controller and display it in View

In this example we pass value to the controller and then display it in View

Watch online:
in your controller receive some value (Name)
      public ActionResult Index(string Name)
        {
            ViewBag.Message = HttpUtility.HtmlEncode("The name is: " + Name);
            return View();
        }

now, in view display that value (ViewBag.Message)
<p>@ViewBag.Message</p>

now if you navigate to Test page and provide it with some "Name" - the value will be displayed on the screen.

http://localhost:49771/Test?Name=Frank

Sunday, 14 October 2012

MVC - C# - Simple Example how to pass parameters from controller to view

Watch this example on YouTube


or follow these instructions:
Create the following function in your controller class

        public ActionResult PassValueFromControllerToView(string text, int numb = 10)
        {
            ViewBag.Text = text;
            ViewBag.Numb = numb;
            return View();
        }
This function receives string and number, if number is not passed, it will be defaulted to 10.
Next, right click somewhere inside the class and select Add View...

Click Add
Add the following code:
<b>Display string that was passed to function for number of times - by defult 10</b>
<ul>
    @for (int i = 0; i < ViewBag.Numb; i++){
        <li>
            @ViewBag.Text;
        </li>
    }
</ul>
Now when you run the program and navigate to controller name(in my case it is called user), function name  http://localhost:51480/user/PassValueFromControllerToView?text=this is my text&numb=4

The following screen will be displayed


If I don't pass any number, it will default to 10
http://localhost:51480/user/PassValueFromControllerToView?text=this is my text