1

just trying to fathom out LINQ to SQL and datacontext class. I am learning MVC coding and have a database with some test data. I have created my datacontext and have been able to get this far in the controller.

ClientClassesDataContext context2 = new ClientClassesDataContext();
var result2 = context2.up_GetClient(4,1);
up_GetClientResult myObject = result2.SingleOrDefault();
return View(myObject);

This returns a list of clients, my part I'm stuck at is how to pass this to the view and put it in a grid style. Even passing the object to the view with only one row of data, I'm kinda stuck on how t even access items and display in, say, a text box.

Even just any pointers or links on best practice for LINQ to SQL etc would be a great help. There seems to be a lot of varying info out there.

The View is empty with the code below.

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Test</h2>

</asp:Content>

Any help or pointers would be appreciated.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • Which MVC framework are you using, may I know? With MVC3.0 you can make use of WebGrid helper for which you can get a lot of samples online as well. Otherwise if you want to have complete control over the data display you can simply use HTML table. But do not expect a full blown GridView available within MVC framework as in the case of WebForms/WinForms. – Siva Gopal Jun 13 '13 at 08:17
  • You can refer to : http://stackoverflow.com/questions/5138618/create-gridview-in-asp-net-mvc3-0 & http://forums.asp.net/t/1687421.aspx/1 – Siva Gopal Jun 13 '13 at 08:18

1 Answers1

1
Model:

public class MyModel 
    {

        IEnumerable<client> _getClient { get; set; }
    }

Controller:

ClientClassesDataContext context2 = new ClientClassesDataContext();
var result2 = context2.up_GetClient(4,1);
MyModel _mymdl=new MyModel();
_mymdl._getClient = result2.SingleOrDefault();
return View(_mymdl);

VIew:

@model ProjectName.web.MyModel
@{
    ViewBag.Title = "";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div>
foreach (var item in Model._getClient)
        {

//Add your login to create Table
}
</div>
Umesh Sehta
  • 10,555
  • 5
  • 39
  • 68