I have 2 web grids in one page and I am trying to bind data to them both but from different models, would I need to create a model which references both models ? Here is my code
@model IEnumerable<DashboardBucks.Models.User>
@{
Layout = null;
WebGrid grid = new WebGrid(Model);
WebGrid grid2 = new WebGrid(Model);????
}
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="~/JS/jquery-1.10.2.js"></script>
<script type="text/javascript" src="~/JS/JsFunctions.js"></script>
<script type="text/javascript" src="~/JS/placeholders.min.js"></script>
<script type="text/javascript" src="../Styles/Menu/swimbi.js"></script>
<link href="../Styles/Menu/swimbi.css" rel="Stylesheet" type="text/css" media="screen" />
<link href="~/Styles/StyleSheet.css" rel="Stylesheet" type="text/css" media="screen" />
<meta name="viewport" content="width=device-width" />
<title>Dashboard</title>
</head>
<body onload="GetParValue()" style="background-color: white; width:99%;">
<div align="center">
@using (@Html.BeginForm("LoginResult", "User", FormMethod.Post))
{
<table style="width:100%;">
<tr>
<td>
<img src="~/Styles/Images/LogoDash.png" class="" />
</td>
<td>
<div align="right" class="placement">
<img id="flagImg" src="" class="flag" />
</div>
</td>
</tr>
</table>
<div align="center">
<div id="swimbi">
<ul>
<div align="left" style="margin-bottom: -25px; padding-top: 4px;">
<div id="date" style="padding-left: 10px; padding-top: 5px;"></div>
<div id="txt" style="padding-left: 10px;"></div>
<!– <div id="retroclockbox_xs" style="padding-left: 10px; padding-top: 10px;"></div>–>
</div>
<li id="lilast" style="margin-right: 25px;"><a data-icon="" href="#"> Country</a>
<ul>
<li><a href="?param1=1" onclick="GetParValue()">South Africa</a></li>
<li><a href="?param1=6" onclick="GetParValue()">Zimbabwe</a></li>
<li><a href="?param1=7" onclick="GetParValue()">Botswana BU</a></li>
<li><a href="?param1=9" onclick="GetParValue()">Botswana CC</a></li>
<li><a href="?param1=4" onclick="GetParValue()">Botswana TU</a></li>
<li><a href="?param1=2" onclick="GetParValue()">Kenya</a></li>
<li><a href="?param1=5" onclick="GetParValue()">Malawi</a></li>
<li><a href="?param1=3" onclick="GetParValue()">Spain</a></li>
<li><a href="?param1=8" onclick="GetParValue()">Swaziland</a></li>
</ul>
</li>
</ul>
</div>
<div>
</div>
</div>
}
<div id="gridContent" style="padding: 20px; width:30%; font-family:Trebuchet MS, Arial, Helvetica, sans-serif;">
<div style=" border-collapse: separate; border: solid 1px #ffba36;">
<div align="Left" style="padding: 7px;font-family:Trebuchet MS, Arial, Helvetica, sans-serif; color:#6c5d45; border-bottom:solid 1px #6c5d45;">
QUEUE
</div>
@grid.GetHtml( tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",
columns: new[]{
grid.Column("Password"),
grid.Column("UserName"),
grid.Column("Email")
})
</div>
</div>
<br />
<div id="gridContent" style="padding: 20px; width:30%; font-family:Trebuchet MS, Arial, Helvetica, sans-serif;">
<div style=" border-collapse: separate; border: solid 1px #ffba36;">
<div align="Left" style="padding: 7px;font-family:Trebuchet MS, Arial, Helvetica, sans-serif; color:#6c5d45; border-bottom:solid 1px #6c5d45;">
QUEUE
</div>
@grid2.GetHtml( tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",
columns: new[]{
grid2.Column("Password"),
grid2.Column("UserName"),
grid2.Column("Email")
})
</div>
</div>
</div>
</body>
</html>
Controller :
public ActionResult Dashboard() { Uri myUri = new Uri(System.Web.HttpContext.Current.Request.Url.AbsoluteUri); string param1 = HttpUtility.ParseQueryString(myUri.Query).Get("param1"); if (param1 == null) { string level = (string)System.Web.HttpContext.Current.Cache["Levels"]; string[] details = level.Split('-').ToArray(); Response.Redirect(myUri + "?" + "param1=" + details[0]); // Response.Write("<script>GetParValue();</script>"); // System.Web.HttpUtility.ParseQueryString(myUri + "?" + "param1=" + details[0]); } List<Models.User> laUserList = new List<Models.User>(); DBEntities objentity = new DBEntities(); var _objuserdetail = (from data in objentity.Users where data.CountryID==param1 select data); foreach (var item in _objuserdetail) { Models.User user = new Models.User(); user.Username = item.Username; user.Password = item.Password; user.Email = item.Age;//IS ACTUALLY EMAIL HAVENT CHANGED COLUMN NAME IN DB YET AS IM JUST TESTING laUserList.Add(user); } List<Models.testData> testList = new List<Models.testData>(); DBEntities testOb = new DBEntities(); var _objTestdetail = (from data in testOb.TestDatas select data); foreach (var item in _objTestdetail) { Models.testData tests = new Models.testData(); tests.Count = item.Count; tests.Pro = item.Process; //user.Email = item.Age;//IS ACTUALLY EMAIL HAVENT CHANGED COLUMN NAME IN DB YET AS IM JUST TESTING testList.Add(tests); } var datas = laUserList; var datas2 = testList; return View(); // HOW WOULD I RETURN BOTH datas and datas2 to my webgrids }
HOW WOULD I RETURN BOTH datas and datas2 to my webgrids in my view
Have a look through similar threads
https://www.google.com/search?q=site%3aforums.asp.net+two+models
Yes exactly! You need a single model, called a ViewModel, that has everything you need.
I wrote a post about it
here.
HI,
Create single model, it contains two model data’s, then pass to view.
public class ModelOne { // Model1 member’s}
public class ModelTwo { // Model 2 member’s }
Create common Model
public class ViewModel{
public ModelOne Grid1{ get; set;}
public ModelTwo Grid2 { get; set; }
}
set each grid item value into ViewModel object, and pass to view.
I hopeit might help to you.
Thanks,
Jai
Thanks guys for your help got it working