1

I know some people suggesting to use JQuery or other Javascript libs. I am looking to use common gridview or similar. Just let you know this is my learning phase so it could be very basic question for advance users.

Carson63000
  • 4,215
  • 2
  • 24
  • 38
Builder
  • 1,046
  • 2
  • 10
  • 30

3 Answers3

3

You can't (or at least shouldn't if it's even possible) use the actual WebForms controls in MVC. If this is indeed a learning experience for you, you'd be better off learning some new ways of doing these things.

At its simplest, if you just want to display a table of data then you would have that data as an object in your model. For example, if you want to have a table of Customers then your model might have a property such as:

public IEnumerable<Customer> Customers { get; set; }

Let's say for the sake of argument that a Customer has a Name and an Address property on it. Then in your view, you'd simply loop through those for your table:

@foreach (var customer in Model.Customers)
{
    <tr>
        <td>@customer.Name</td>
        <td>@customer.Address</td>
    </tr>
}

Starting from there, you can build more functionality appropriately. For example, maybe each record in the table needs to have a link to "edit" that Customer. Then you'd build a link to the corresponding edit action using perhaps a customer's ID property (again, for the sake of argument, assume there's one there):

@foreach (var customer in Model.Customers)
{
    <tr>
        <td>@customer.Name</td>
        <td>@customer.Address</td>
        <td>@Html.ActionLink("Edit", "Customers", "Edit", new { id = customer.ID }, null)</td>
    </tr>
}

(There are different overloads of the ActionLink method you can use.)

As you build this, you will get a better understanding of how simple the client-side rendering of this really is (or should be) and find yourself generally more hands-on with your HTML than generally happens in WebForms. Continuing along this path, you'll find it becomes a lot easier to make use of some pretty powerful client-side plugins (there are a ton of jQuery plugins for tables, for example). Things like paging, sorting, etc. become a lot cleaner through either AJAX calls (which are trivial to write server-side in MVC) or through just rendering all of the data in the initial page load and paging/sorting/filtering client-side.

David
  • 208,112
  • 36
  • 198
  • 279
  • Thanks David, I will try this solution and currently I am not using IIenumerable, just passing the datatable object which is working for me. Thanks again. – Builder Jul 26 '13 at 23:35
  • @qazifarhan: You should try to move away from `DataTable`s and `DataSet`s and things like that. You'll find that objects become a _lot_ easier to support when they have meaningful names with compiler-enforced types. But if you are just using a `DataTable`, its `.Rows` property is the `Model.Customers` in this case. `Rows` is a collection over which you can enumerate. – David Jul 26 '13 at 23:46
2

You might like to take a look at this question: grid controls for ASP.NET MVC?

It was closed as not constructive for being a request for recommendations, but there are certainly a lot of data grid options recommended there that you might like to check out.

But in short, ASP.NET MVC does not really share WebForms' design philosophy of dragging and dropping complex components - its philosophy is more aimed at "I know exactly what I want on my page, now I need to implement it". If that's not what you're after, you might be better sticking with WebForms.

Community
  • 1
  • 1
Carson63000
  • 4,215
  • 2
  • 24
  • 38
  • Nice link, and very useful controls with so much variety. I am planning to try some of them. Thanks for tip. – Builder Jul 26 '13 at 23:39
1

HTML elements that show information to user or gather data from him/her can be categorized in simple and complex; simple elements like TextBox, ComboBox, etc. have HtmlHelper in ASP.NET MVC , so it is rational that developers seek for HtmlHelpers for complex elements like GridView; it's complex because it should have paging, filtering, grouping, etc. In my last project with ASP.NET MVC I needed to use one of them and I found two fantastic library:

  1. Grid.Mvc
  2. Telerik Extensions for ASP.NET MVC

Both of them are Open Source and efficient, but I prefer the first one because it's simple, well-designed and easy to configure.

Abbas Amiri
  • 3,074
  • 1
  • 23
  • 23
  • Thanks Abbas, I am looking similar to these grids, I will definitely try one of these in my solution. Thanks for your help. – Builder Jul 26 '13 at 23:36