How to bind drop down with grid-view in MVC3
@{ var listItems = new List(); listItems.Add(new SelectListItem { Text = String.Empty, Value = String.Empty }); foreach (var name in ViewBag.Names) { listItems.Add(new SelectListItem { Text = name, Value = name }); }}@Html.DropDownList("Categories", listItems, "Select", new { onchange = "javascript:sendParam();" })
@{
var grid = new WebGrid(source: ViewBag.webgriddata,
defaultSort: "Name",
rowsPerPage: 3);
}
@grid.GetHtml(
tableStyle: "grid",
columns: grid.Columns(
grid.Column("Name"),
grid.Column("Address"), grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.Value.ID })), grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.Value.ID })), grid.Column("Class")
)
)
PUBLIC CLASS HOMECONTROLLER : CONTROLLER
{
ENTITYFRAMEWORKDATAENTITIES OBJ = NEW ENTITYFRAMEWORKDATAENTITIES();
PUBLIC ACTIONRESULT INDEX()
{
VAR RESULT = FROM C IN OBJ.TBLDETAILS
SELECT C;
VIEWBAG.WEBGRIDDATA = RESULT.TOLIST();
LIST ABC = NEW LIST(2);
VAR Q = FROM A IN OBJ.TBLDEPTS
SELECT A;
FOREACH (VAR I IN Q)
{
ABC.ADD(I.DEPT);
}
VIEWBAG.NAMES = ABC;
RETURN VIEW(Q);
}
PUBLIC ACTIONRESULT ABOUT(STRING VAL)
{
RETURN VIEW();
}
PUBLIC ACTIONRESULT CATEGORYDETAILS(STRING ID)
{
INT ID = (FROM D IN OBJ.TBLDEPTS
WHERE D.DEPT == ID
SELECT D.ID).FIRSTORDEFAULT();
VAR RESULT = FROM C IN OBJ.TBLDETAILS
WHERE C.DEPTID == ID
SELECT C;
VIEWBAG.WEBGRIDDATA = RESULT.TOLIST();
LIST ABC = NEW LIST(2);
VAR Q = FROM A IN OBJ.TBLDEPTS
SELECT A;
FOREACH (VAR I IN Q)
{
ABC.ADD(I.DEPT);
}
VIEWBAG.NAMES = ABC;
RETURN VIEW("INDEX");
}
PUBLIC ACTIONRESULT EDIT(INT ID)
{
TBLDETAIL OBJ2 = NEW TBLDETAIL();
VAR DD = GETDATA(ID);
OBJ2.ADDRESS = DD.ADDRESS;
OBJ2.CLASS= DD.CLASS;
OBJ2.NAME = DD.NAME;
OBJ2.DEPTID = DD.DEPTID;
RETURN VIEW(OBJ2);
}
[HTTPPOST]
PUBLIC ACTIONRESULT EDIT(TBLDETAIL MODEL)
{
TBLDETAIL OBJ2 = NEW TBLDETAIL();
OBJ2.ID = MODEL.ID;
OBJ2.ADDRESS = MODEL.ADDRESS;
OBJ2.CLASS = MODEL.CLASS;
OBJ2.NAME = MODEL.NAME;
OBJ2.DEPTID = MODEL.DEPTID;
OBJ.TBLDETAILS.ATTACH(OBJ2);
OBJ.OBJECTSTATEMANAGER.CHANGEOBJECTSTATE(OBJ2, SYSTEM.DATA.ENTITYSTATE.MODIFIED);
OBJ.SAVECHANGES();
RETURN REDIRECTTOACTION("INDEX");
}
PUBLIC TBLDETAIL GETDATA(INT ID)
{
ENTITYFRAMEWORKDATAENTITIES OBJ = NEW ENTITYFRAMEWORKDATAENTITIES();
VAR DD = FROM E IN OBJ.TBLDETAILS
WHERE E.ID == ID
SELECT E;
RETURN DD.FIRSTORDEFAULT();
}
PUBLIC ACTIONRESULT DELETE(INT ID)
{
TBLDETAIL OBJ2 = NEW TBLDETAIL();
TBLDETAIL PRODUCT = (FROM C IN OBJ.TBLDETAILS
WHERE C.ID == ID
SELECT C).FIRSTORDEFAULT();
OBJ.TBLDETAILS.DELETEOBJECT(PRODUCT);
OBJ.SAVECHANGES();
RETURN REDIRECTTOACTION("INDEX");
}
}
}
No comments:
Post a Comment