C#DOTNETCONSOLEAPPLICATION

MVC2

mvc

 MODEL-Step 1: Open Solution Explorer and Look for the Models Folder. After that right click on Models  Add  Class…

Step 2: Create a New class ItemList.cs and click on Add button.

Step 3: Add the following code in ItemList.cs class.


public class ItemList
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }


Step 4: Create a New Controller ItemController.cs. Right click on Controllers  Add  Controller…

Step 5: Select MVC 5 Controller – Empty and click Add button.

Step 6. Now open ItemController.cs and add the following code.


public ActionResult Index()

        {

            ViewBag.ItemList = "Computer Shop Item List Page";


            List<ItemList> IList = new List<ItemList>()

            {

                new ItemList {ID=1, Name="iPhone", Category="Mobile", Price=2393 },

                new ItemList {ID=2, Name="HardDisk", Category="Computer", Price=9399 },

                new ItemList {ID=3, Name="Mouse", Category="Computer", Price=120 },

                new ItemList {ID=4, Name="Samsung Note3", Category="Mobile", Price=9348 }

            };


            return View(IList);

        }

    }


 


Step 7: Now, right click on Index() Action method and click on Add view. Give View Name: Index and click on Add button.

in index.cshtml add the code

@foreach (var i in Model)
{
<h1>@ViewBag.ItemList</h1>
    <b>ID : </b> @i.ID <br />
    <b>Name : </b> @i.Name <br />
    <b>Category : </b> @i.Category <br />
    <b>Price : </b> @i.Price <br />
    <hr />
}

Run the program

Item/index

=======================================================================


VIEW BAG
CREATE A CLASS IN MODEL FOLDER

name of the class is Student

public class Student
    {
        public int StudentID { get; set;}
        public string StudentName { get; set; }
        public int Age { get; set; }
    }

next create a controller and its name studentController

public class studentController : Controller
    {
        IList<Student> studentList = new List<Student>() {
                    new Student(){ StudentID=1, StudentName="Steve", Age = 21 },
                    new Student(){ StudentID=2, StudentName="Bill", Age = 25 },
                    new Student(){ StudentID=3, StudentName="Ram", Age = 20 },
                    new Student(){ StudentID=4, StudentName="Ron", Age = 31 },
                    new Student(){ StudentID=5, StudentName="Rob", Age = 19 }
                };
        // GET: Student
        public ActionResult Index()
        {
            ViewBag.TotalStudents = studentList.Count();

            return View();
        }
    }

create a view

<label>Total Students:</label>  @ViewBag.TotalStudents
==================================================
                                    VIEWBAG

model
public class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

controller

public ActionResult Index() //We'll set the ViewBag values in this action
        {
            ViewBag.Title = "TECHZMATRIX";
            ViewBag.Description = "software training";

            ViewBag.UserNow = new User()
            {
                Name = "sree",
                ID = 4,
            };

            return View();

        }

VIEW
<h2>Index</h2>
<h3>@ViewBag.Title</h3>

<p>@ViewBag.Description</p>

Your name:

<div><dl><dt>Name:</dt>
        <dd>@ViewBag.UserNow.Name</dd>
        <dt>ID:</dt>
        <dd>@ViewBag.UserNow.ID</dd></dl></div>
=========================================
VIEWDATA
MODEL

public class sum
    {
        public static int addition(int a,int b,int c)
        {
            return a + b + c;
        }
    }

CONTROLLER

public class oneController : Controller
    {
        // GET: one
        public ActionResult Index(int n1,int n2,int n3)
               {
            ViewData["message"] = string.Format("{0}+{1}+{2}={3}", n1, n2, n3, Models.sum.addition(n1, n2, n3));
            return View();
        }

    }

VIEW

<h4>@ViewData["message"] </h4>

TO RUN

http://localhost:56421/one/Index?n1=2&n2=3&n3=4
===========================================
                            VIEWDATA
MODEL

public class Student
    {
        public string StudentName { get; set; }
        
    }

CONTROLLER

public ActionResult Index()
        {
            IList<Student> studentList = new List<Student>();
            studentList.Add(new Student() { StudentName = "Bill" });
            studentList.Add(new Student() { StudentName = "Steve" });
            studentList.Add(new Student() { StudentName = "Ram" });

            ViewData["students"] = studentList;
            return View();
        }

VIEW

<ul>
    @foreach (var std in ViewData["students"] as IList<WebApplication9.Models.Student>)
    {
        <li>
            @std.StudentName
        </li>
    }
</ul>
===================================================================
                TEMPDATA
tempdata-It represents a set of data that persists only from one request to the next. It is derived from TempDataDictionary, we can use its object to pass data as we did in ViewData. The value of TempData persists only from one request to the next. Retention is used to mark key to persist data so that it can retain for the next request.

We can also use TempData to pass data from one action to another action.

CONTROLLER

public ActionResult Index()
        {
            List<string> Courses = new List<string>();
            Courses.Add("J2SE");
            Courses.Add("J2EE");
            Courses.Add("Spring");
            Courses.Add("Hibernates");
            TempData["Courses"] = Courses;
            return View();
        }

VIEW

<ul>
    @{
        foreach (var Courses in TempData["Courses"] as List<string>)
        {
            <li> @Courses</li>
        }
    }
</ul>

===============================================================

 LINQ IN MVC

LINQ-Language INtegrated Query

Language-Integrated Query (LINQ) is a powerful query language introduced with .Net 3.5 & Visual Studio 2008. LINQ can be used with C# or Visual Basic to query different data sources.


CREATE TABLE Publisher

(

    Id int Primary Key NOT NULL IDENTITY(1,1),

    Name nvarchar(50) NOT NULL,

    [Year] nvarchar(4) NOT NULL

)


CREATE TABLE BOOK

(

    Id int Primary Key NOT NULL IDENTITY(1,1),

    Title nvarchar(50) NOT NULL,

    Auther nvarchar(50)NOT NULL,

    [Year] nvarchar (4) NOT NULL,

    Price decimal(6,2)NOT NULL,

    PublisherId int NOT NULL

)


ALTER TABLE

 BOOK

ADD CONSTRAINT FK_BOOK_PUBLISHER FOREIGN KEY (PublisherId)

REFERENCES Publisher(Id)ON DELETE CASCADE




public class PublisherModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Year { get; set; }
    }

CREATE CONTROLLER
 
public class PublisherController : Controller
    {
        private operationDataContext context;
        public PublisherController()
        {
            context = new operationDataContext();
        }
        public ActionResult Index()
        {
            IList<PublisherModel> publisherList = new List<PublisherModel>();
            var query = from publisher in context.Publishers
                        select publisher;

            var publishers = query.ToList();
            foreach (var publisherData in publishers)
            {
                publisherList.Add(new PublisherModel()
                {
                    Id = publisherData.Id,
                    Name = publisherData.Name,
                    Year = publisherData.Year
                });
            }
            return View(publisherList);
        }

        public ActionResult Create()
        {
            PublisherModel model = new PublisherModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(PublisherModel model)
        {
            try
            {
                Publisher publisher = new Publisher()
                {
                    Name = model.Name,
                    Year =model.Year
                };
                context.Publishers.InsertOnSubmit(publisher);
                context.SubmitChanges();
                return RedirectToAction("Create");
            }
            catch
            {
                return View(model);
            }
        }
    }
}

CREATE VIEW

VIEW
Now we create a view to add a new publisher. To create the view, use the following procedure:
1. Right-click on the Action Method Create (GET).
2. The View Name is already filled in so don't change it.
3. The View Engine already selected Razor so don't change it.
4. Check the checkbox "Create a strongly-typed-view" because we are creating a strongly typed view.
5. Choose the Model class "PublisherModel" so it can be bound with the view.

@model LinqToSQLMvcApplication.Models.PublisherModel

                @{

                    ViewBag.Title = "Create";

                }

                <h2>Create</h2>

                @using (Html.BeginForm())

                {.                 @Html.ValidationSummary(true)

                    <fieldset>

                        <legend>PublisherModel</legend>

                        <div class="editor-label">

                            @Html.LabelFor(model => model.Name)

                        </div>

                        <div class="editor-field">

                            @Html.EditorFor(model => model.Name)

                            @Html.ValidationMessageFor(model => model.Name)

                        </div>

                        <div class="editor-label">

                            @Html.LabelFor(model => model.Year)

                        </div>

                        <div class="editor-field">

                            @Html.EditorFor(model => model.Year)

                            @Html.ValidationMessageFor(model => model.Year)

                        </div>

        <p>

                            <input type="submit" value="Create" />

                        </p>                  </fieldset>

                }

                <div>

                    @Html.ActionLink("Back to List", "Create")

                </div> 

 



No comments:

Post a Comment

Techzmatrix