Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
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 yesterday
Hi MVC3 framework – user1071017 21 hours ago

1 Answer

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>
share|improve this answer
OK, tried the above code and the line IEnumerable<client> _getClient { get; set; } shows error - Unknown type 'client' – user1071017 21 hours ago
its an example, change Client to your table name. – sehta umesh 4 hours ago
ok thanks got that now. Can I finally ask, what is the _GetClient reference in the model? – user1071017 1 hour ago
Ok worked that out, the public was needed so I could access _GetClient from the model public IEnumerable<tbl_Client> _GetClient{ get; set; } – user1071017 54 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.