asp.net mvc - How to bind a table without(edit,delete,create) in mvc contrib grid -


i have used mvccontrib grid in mvc 2.0 want bind table in gridview...so far coding is....i dont no whther correct or wrong...

homecontroller.cs:

public actionresult list(int? page) {     using (productdatacontext db = new productdatacontext())     {         viewdata["product"] = db.products.tolist().aspagination(page ?? 1, 10);         return view("product");     } } 

i have cretaed list view that....... coding should write in index.aspx

so far used coding not working......

<% html.grid((list<product>)viewdata["product"])     .columns(column =>         {             column.for(c => c.categoryid);             column.for(c => c.supplierid);          }).render();   %> 

it shows there no data available. or there other coding?

my issue want bind table in mvccontrib grid.

if shows there's no data available problem db.products.tolist().aspagination(page ?? 1, 10) returns no elements (empty collection). why happens impossible information provided. largely depend on implementation of productdatacontext , available data in data store.

this being said, recommend using typed views:

public actionresult list(int? page) {     using (productdatacontext db = new productdatacontext())     {         var products = db.products.tolist().aspagination(page ?? 1, 10);         return view("product", products);     } } 

and view becomes:

<%@ page     language="c#"      inherits="system.web.mvc.viewpage<ienumerable<appname.models.product>>" %> <%@ import namespace="appname.models" %>  <%= html.grid<product>(model)         .columns(column =>         {             column.for(c => c.categoryid);             column.for(c => c.supplierid);         }) %> 

notice how view typed collection of products.

plain, simple, typed.


update:

as requested in comments section here's example of adding edit , delete links each row:

<%= html.grid<product>(model)         .columns(column =>         {             column.for("tablelinks").named("");             column.for(c => c.categoryid);             column.for(c => c.supplierid);         }) %> 

and in tablelinks.ascx partial:

<%@ control      language="c#"      inherits="system.web.mvc.viewusercontrol<appname.models.product>" %> <%@ import namespace="appname.models" %>  <td>     <%: html.actionlink<productscontroller>(c => c.edit(model.id), "edit") %> |     <% using (html.beginform<productscontroller>(c => c.destroy(model.id))) { %>         <%: html.httpmethodoverride(httpverbs.delete) %>         <input type="submit" value="delete" />     <% } %> </td> 

which assumes of course following actions present in productscontroller:

public actionresult edit(int id) ... [httpdelete] public actionresult destroy(int id) 

i invite checkout sample mvc application wrote illustrates concepts.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -