mvc3 razor grid with dropdownlist bind...

Posted by Kannappan under ASP.NET on 6/25/2013 | Points: 10 | Views : 4366 | Status : [Member] | Replies : 5
Hi,
I am beginner in MVC razor 3 ,need to bind drop down values in grid
and that should be selected value in drop down based on Employee.

Given image in there is no selected value in Rights dropdown.
Kindly guide me.

Regards,
Kannappan




Responses

Posted by: saisagarnallaagilelearning-15961 on: 6/26/2013 [Member] Starter | Points: 25

Up
0
Down
first you create one action result for grid view details. in that call your stored procedure and pass this stored procudure output using entity frame work.
when u click on edit button write code for redirect to this grid action.

Kannappan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kannappan on: 6/26/2013 [Member] Starter | Points: 25

Up
0
Down
i have done all things and loaded in to grid as well as drop down items ,my problem is depends on the employee their rights should be in selected in that drop down list while page load itself...


Kannappan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: saisagarnallaagilelearning-15961 on: 6/26/2013 [Member] Starter | Points: 25

Up
0
Down
@Html.DropDownListFor(modelItem => item.intNationalityID, (IEnumerable<SelectListItem>)ViewBag.Nationality, "--Select--", new { @style = "width:100%", @class = "textBox", tabindex = 16 })



Kannappan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kannappan on: 6/26/2013 [Member] Starter | Points: 25

Up
0
Down
Kindly go through this image ,you will get clear idea where i m stuck.
 Download source file

Kannappan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kannappan on: 6/26/2013 [Member] Starter | Points: 25

Up
0
Down
as well as here with given model,controller and view of this page.

Controller


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LC_PRO.DAL;
using LC_PRO.Models;
using LC_PRO.Model;



namespace LC_PRO.Controllers
{
public class AddUserController : Controller
{
BAL.BAL objBal = new BAL.BAL();
EmplyInfoModel objEmplyInfo = new EmplyInfoModel();
[HttpGet]
public ActionResult UserAdd()
{
objEmplyInfo.EmplyInformationModel = GridLoadEmplyDetails();
return View(objEmplyInfo);
}

[HttpPost]
public ActionResult UserAdd(BindBase data)
{
return RedirectToAction("UserAdd", "Adduser");

}

public List<EmplyInformation> GridLoadEmplyDetails()
{
List<EmplyInformation> GetEmplyName = new List<EmplyInformation>();
using (var Emp_data = new LC_PRODataContext())
{
GetEmplyName = (from emp in Emp_data.Emplies
select new EmplyInformation
{
Emp_Firstname = emp.Emply_Frst,
Emp_Lastname = emp.Emply_Lst,
emplyId = emp.Emply_Id,
RightsModel = GetRights(emp.Emply_Id)

}).ToList<EmplyInformation>();

}
return GetEmplyName;
}


public SelectList GetRights(int emplyId)
{
List<RightsModel> objRights = new List<RightsModel>();

using (var data = new LC_PRODataContext())
{
objRights = (from em in data.EmplyInfos
join er in data.Emply_Rights on em.Rights_Id equals er.Rights_Id
// where em.Emply_Id.Equals(emplyId)
select new RightsModel
{
RightsId = em.Rights_Id,
RightsName = er.Rights_Name,

}).ToList<RightsModel>();


}

SelectList objlistoEmplyTobind = new SelectList(objRights, "RightsId", "RightsName");
return objlistoEmplyTobind;

}







}
}


Model

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

namespace LC_PRO.Model
{
public class EmplyInfoModel
{
public List<EmplyInformation> EmplyInformationModel { get; set; }
}
public class EmplyInformation
{

public string Emp_Firstname { get; set; }
public string Emp_Lastname { get; set; }
public int emplyId { get; set; }
public SelectList RightsModel { get; set; }
}
public class RightsModel
{

public int? RightsId { get; set; }

public string RightsName { get; set; }
public int? selected { get; set; }
}




}

View
@model LC_PRO.Model.EmplyInfoModel
@{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/MasterLayout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<title>UserAdd</title>
<link href="../../Content/Common.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
<table align="center" width="980">
<tr>
<td align="center">
@using (@Html.BeginForm())
{

var grid = new WebGrid(source: Model.EmplyInformationModel, rowsPerPage: 10);

@grid.GetHtml(tableStyle: "gridTable",headerStyle: "gridHead",footerStyle: "gridFooter",rowStyle: "gridRow",alternatingRowStyle: "gridAltRow",
columns: grid.Columns(
grid.Column("Emp_Firstname", "Emp_Firstname"),
grid.Column("Emp_Lastname", "Emp_Lastname"),
grid.Column(header: "Rights", format: @item => Html.DropDownList("value", (IEnumerable<SelectListItem>)Model.EmplyInformationModel[0].RightsModel))))

}

</td>
</tr>
</table>
</div>
</body>
</html>


Kannappan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response