Webgrid Questions

I am wanting to create a webpage that has a webgrid where you can select a row and it will show the details of that row below the webgrid for editing. Could someone provide an example of how to do this?

I wrote and article about doing this using the Web Pages framework. You can adapt it to MVC: http://www.mikesdotnetting.com/Article/168/The-WebGrid-Helper-Making-Selections

Thank you for the reply. I found that article very helpful. Except when you got to this part:

    var book = grid.SelectedRow;
    @RenderPage("~/Partials/EditBook.cshtml", new { Book = book } )

How did you get the value for book? I do not see anywhere in the code you gave where that is located. 

The second parameter of the RenderPage method takes an anonymous object representing data to be passed to the partial. In this case, the value (book, with a lower case b) is the SelectedRow property of the grid. The SelectedRow property is a dynamic
object whose properties are made up from the column names. So book is a dynamic object with BookId, Title, Description etc properties.  This is passed to the Partial’s
PageData property (also accessible via the dynamic Page property). So in the partial, the Id of the book is obtained like this:

var bookid = Page.Book.BookId;

Thank you for the response. Sorry for my delayed response. But I am trying to do this in a MVC format, so I would not want to access the data from a webpage. How do you pass that value to a controller to then access the data?

You still need to pass the data to the View. In MVC, you get the data within the controller and pass it to the view either as a strongly typed model or in ViewBag. Then you will bind the model to the WebGrid in the View. 

I am trying your method and actually querying the database from the partial view but I am using Entity Framework to access the database and I do not have the functionality do the Database.Open command. Again is there a way to pass the selected value from
the webgrid to my controller and then render that partial page by a strongly typed Edit page?

Leave a Reply