Wednesday, January 13, 2016

MVC Drop Down List

using MvcApplication3.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication3.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            return View();
        }
        public ActionResult About()
        {
            return View();
        }
        public ActionResult GetSt()
        {
            List<Student> studentList = new List<Student>();
            List<SelectListItem> ItemList = new List<SelectListItem>();
            Student st1 = new Student() { Id = 1, Name = "Kassa" };
            Student st2 = new Student() { Id = 2, Name = "Sameera" };
            studentList.Add(st1);
            studentList.Add(st2);
            foreach (var student in studentList)
            {
                ItemList.Add(new SelectListItem() { Value = student.Id.ToString(), Text = student.Name });
            }
            ViewBag.StudentsBag = ItemList;
            return View();
        }
        [HttpPost]
        public ActionResult AddSt()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddStEnd(FormCollection form)
        {
            var itemId = form["Id"].Split(',');
            var itemName = form["Name"].Split(',');
            return RedirectToAction("GetSt");
        }
        [HttpPost]
        public ActionResult GetDe(FormCollection item)
        {
            //SelectListItem it = item;
            var addedItems = item["StudentListDrop"].Split(',');
            int selectedId = Convert.ToInt32(addedItems[0]);
            Student ss = new Student() { Id = 2, Name = "Samee" };
            ViewBag.StudentS = ss;
            return View();
        }
    }
}




<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>GetSt</title>
</head>
<body>
    <div>
        <%using(Html.BeginForm("GetDe","Home",FormMethod.Post)){ %>
        <% = Html.DropDownList("StudentListDrop", (List<SelectListItem>)ViewBag.StudentsBag,"Select Student") %>
        <%= Html.TextBox("mmmmm") %>
        <br />
        <br />
        <input type="submit" value="GetDetails" />
        <%} %>
    </div>
    <div>
        <%=Html.ActionLink("Add New","AddSt","Home") %>
        <%using(Html.BeginForm("AddSt","Home",FormMethod.Post)){ %>
            <input type="submit" value="Add" />
        <%} %>
    </div>
</body>
</html>





<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>GetDe</title>
    <h1>dfdfndjfn :</h1>
   
</head>
<body>
    <div>
        <%=Html.Label("Id :") %>
        <br />
        <%=Html.TextBox("Id",((MvcApplication3.Models.Student)ViewBag.StudentS).Id.ToString()) %>
        <br />
        <%=Html.Label("Name :") %>
        <br />
        <%=Html.TextBox("Name",((MvcApplication3.Models.Student)ViewBag.StudentS).Name) %>
    </div>
</body>
</html>


<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>AddSt</title>
</head>
<body>
    <div>
        <%using (Html.BeginForm("AddStEnd","Home",FormMethod.Post)){ %>
            <%=Html.Label("Id :") %>
            <br />
            <%=Html.TextBox("Id") %>
            <br />
            <%=Html.Label("Name :") %>
            <br />
            <%=Html.TextBox("Name") %>
            <br />
            <input type="submit" value="Add" />
        <%} %>
    </div>
</body>
</html>



https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx