I have a webgrid in an ASP.NET MVC3 page, that needs to have a link on each row, that in turn gets data for that row.
Now in webforms asp.net, i can just add template column and shove a server control button in there and attach an event to that button so that whenever the user clicks on that row, i grab the userid for that row and then query the database for that user,
return the data and that's it..
well in MVC it ain't that straightforward...
So how do i put an hyperlink in that MVC3 WebGrid control that can call a json ajax method to asynchronously do a database call?
Of course, the reason it was suggested to use an json ajax call was to keep the web grid data...
all i need to do is add a link to a webgrid that calls a javascript function in a controller.
this is pretty simple stuff in a web form, but for some reason it's obfuscated or overcomplicated beyond all recognition in MVC, and I don't know why....
should be pretty straightforward i would think, but i haven't been able to find a coherent example anywhere.
1. webgrid.
2. link template column in that webgrid that calls an ajax/json method that calls a method that in turn returns some data.
I don't understand the problem you are experiencing: just put a Ajax action link into your template. and put the id of the record in the route parameters. If you experience problems once you have written your code...post it here and all community may help
you.
<div>Hi Drdexter33,</div> <div>It is also pretty simple stuff to get the select row in WebGrid Helper. You can use @item.GetSelectLink outputs a HTML anchor tag with the row selected. This is passed as a QueryString, and the name of the QueryString is set by
the selectionFieldName property set on the grid:
drdexter33
Contributor
2289 Points
889 Posts
MVC3 WebGrid Question.
Mar 29, 2011 04:41 PM|LINK
I have a webgrid in an ASP.NET MVC3 page, that needs to have a link on each row, that in turn gets data for that row.
Now in webforms asp.net, i can just add template column and shove a server control button in there and attach an event to that button so that whenever the user clicks on that row, i grab the userid for that row and then query the database for that user, return the data and that's it..
well in MVC it ain't that straightforward...
So how do i put an hyperlink in that MVC3 WebGrid control that can call a json ajax method to asynchronously do a database call?
Of course, the reason it was suggested to use an json ajax call was to keep the web grid data...
no viewstate in mvc...
thanks.
doug
francesco ab...
All-Star
20912 Points
3279 Posts
Re: MVC3 WebGrid Question.
Mar 29, 2011 05:01 PM|LINK
Give a look to the DataGrid of my Mvc Controls Toolkit here: http://mvccontrolstoolkit.codeplex.com/wikipage?title=Update/Insert/Delete/%20Templated%20Datagrid
and to its associated Master-Detai Form that do exactly an ajax call:
http://mvccontrolstoolkit.codeplex.com/wikipage?title=DetailFormFor%20Helper
When data arrive from ajax also the grid rows arre updated and the new values are hilighted.
Here a screenshot and links to tutorials samples etc: http://mvccontrolstoolkit.codeplex.com/wikipage?title=DataGrid%20and%20Pager%20Complete%20Example
Mvc Controls Toolkit | Data Moving Plug-in Videos
drdexter33
Contributor
2289 Points
889 Posts
Re: MVC3 WebGrid Question.
Mar 29, 2011 09:55 PM|LINK
all i need to do is add a link to a webgrid that calls a javascript function in a controller.
this is pretty simple stuff in a web form, but for some reason it's obfuscated or overcomplicated beyond all recognition in MVC, and I don't know why....
should be pretty straightforward i would think, but i haven't been able to find a coherent example anywhere.
1. webgrid.
2. link template column in that webgrid that calls an ajax/json method that calls a method that in turn returns some data.
francesco ab...
All-Star
20912 Points
3279 Posts
Re: MVC3 WebGrid Question.
Mar 30, 2011 04:27 PM|LINK
I don't understand the problem you are experiencing: just put a Ajax action link into your template. and put the id of the record in the route parameters. If you experience problems once you have written your code...post it here and all community may help you.
Mvc Controls Toolkit | Data Moving Plug-in Videos
Forest Cheng...
Star
8370 Points
819 Posts
Re: MVC3 WebGrid Question.
Mar 31, 2011 05:12 AM|LINK
WebGrid grid = new WebGrid(Model, selectionFieldName:"SelectedRow"); @grid.GetHtml( columns:grid.Columns( grid.Column("Edit",header:null,format:@<text>@item.GetSelectLink("Edit")</text>), grid.Column("Name", "Name", style: "product"), grid.Column("Description", format: @<i>@item.Description</i>), grid.Column("Category", format: @<text>@item.Category</text>), grid.Column("Price",format:@<i>@item.Price</i>) ))</div> <div>To find out which row is selected is just as easy, something like this:@if (grid.HasSelection) { //Do what thing you want here.... @{Html.RenderAction("Delete","Home",new {product = grid.SelectedRow});} }</div> <div> </div> <div>Hope this helpful,</div> <div>Forest Cheng</div>If you have any feedback about my replies,please contact msdnmg@microsoft.com.
Microsoft One Code Framework
drdexter33
Contributor
2289 Points
889 Posts
Re: MVC3 WebGrid Question.
Apr 01, 2011 01:53 PM|LINK
YES..that is helpful
can you explain what these parameters are doing?
grid.Column("Edit", header:null, format:@<text>@item.GetSelectLink("Edit")</text>),
Also:
This looks like an event handler, but can you explain what's going on here as well?
@if (grid.HasSelection)
{
@{Html.RenderAction("Delete","Home",new {product = grid.SelectedRow});}
}
thanks
Forest Cheng...
Star
8370 Points
819 Posts
Re: MVC3 WebGrid Question.
Apr 01, 2011 02:25 PM|LINK
Hi Drdexter,
Hope this link can help you: http://www.dotnetcurry.com/ShowArticle.aspx?ID=655
Hope this helpful,
Forest Cheng
If you have any feedback about my replies,please contact msdnmg@microsoft.com.
Microsoft One Code Framework