I have a telerik grid which is populated with some results. When I select a row, I want two values from the grid to be set and these two values will be passed to a controller. I only know how to get and set one value in my javascript, please can someone
tell me how I can get two values from the grid?
Also, in my javascript, I want to direct to an action. However, at the end of my action (i.e. the url), I want to set a route value and pass another value through in the query string. Does anyone know how I can write some kind of @Html.Action(...) in the
javascript? I am using MVC3 Razor.
Any help would be much much appreciated.
Thank you very much
The following is my code:
@(Html.Telerik().Grid(Model.Results)
.Name("Grid")
.PrefixUrlParameters(false)
.Columns(columns =>
{
columns.Bound(o => o.Id).Width(50);
columns.Bound(o => o.OId).Width(50);
})
.Sortable(sorting => sorting.Enabled(false))
.Selectable(selectable => selectable.Enabled(true))
.EnableCustomBinding(true)
.ClientEvents(ce => ce.OnDataBinding("FS").OnRowSelect("onRowSelected"))
.DataKeys(datakeys => datakeys.Add(o => o.Id))
.RowAction(row => row.Selected = row.DataItem.Id.Equals(Model.ResultSet[0].Id))
)
<script type="text/javascript">
function onRowSelected(e) {
var sId = e.row.cells[0].innerHTML;
//This should be set to o.OId, how can I do this?
var oId = e.row.cells[0].innerHTML;
$.ajax({
type: 'GET',
url: '/Controller/Action/',
success: function () {
window.location.href = '/Controller/Action/' //How can I add a route value (oId) and pass sId through to the querystring
}
});
}
</script>
function onRowSelected(e) {
var sId = e.row.cells[0].innerHTML;
var oId = e.row.cells[1].innerHTML;
$.ajax({
type: 'GET',
url: '/Controller/Action/',
success: function () {
window.location.href = '/Controller/Save/?oId=' + oId + '&sId=' + sId //How can I add a route value (oId) and pass sId through to the querystring
}
});
}
// In your action method now you can do
public ActionResult Save(string oId,string sId){
// oID and sId have the values from the view.
return View();
}
Instead of changing the entire page url, you might want to consider using jquery and ajax to send the data http://api.jquery.com/jQuery.ajax/
oId is a route value, but i explicity have to say for example new { oId = oId} so that this certain route value is set, its not part of the querystring like sId. is there a way of me creating/mimicking new { oId = oId } in my javascript method mentioned
above?
Thank you for your reply. I am unable to just say the following because it will create another route value
'/Controller/Save/' + oId + '?sId=' + sId
This is why I need to do something like new { oId=oId, sId=sId } as the code recognises oId as a specific type of route. Is it at all possible in javascript to call some kind of Html.Action(...) or create some kind of action link?
Your advice is much appreciated,
Thank you very much
What does the route table look like? If you know the expected format of the route it's easiest to just build the string yourself. I mean, ok in a different page use @Html.Action and pass in both values, what is the link or url? Based on that I can show you
how to build the string in javascript.
young345
Is it at all possible in javascript to call some kind of Html.Action(...) or create some kind of action link?
young345
Member
201 Points
721 Posts
How can I set 2 values when selecting a row in my telerik grid?
Mar 28, 2012 05:37 PM|LINK
Hi,
I have a telerik grid which is populated with some results. When I select a row, I want two values from the grid to be set and these two values will be passed to a controller. I only know how to get and set one value in my javascript, please can someone tell me how I can get two values from the grid?
Also, in my javascript, I want to direct to an action. However, at the end of my action (i.e. the url), I want to set a route value and pass another value through in the query string. Does anyone know how I can write some kind of @Html.Action(...) in the javascript? I am using MVC3 Razor.
Any help would be much much appreciated.
Thank you very much
The following is my code:
@(Html.Telerik().Grid(Model.Results) .Name("Grid") .PrefixUrlParameters(false) .Columns(columns => { columns.Bound(o => o.Id).Width(50); columns.Bound(o => o.OId).Width(50); }) .Sortable(sorting => sorting.Enabled(false)) .Selectable(selectable => selectable.Enabled(true)) .EnableCustomBinding(true) .ClientEvents(ce => ce.OnDataBinding("FS").OnRowSelect("onRowSelected")) .DataKeys(datakeys => datakeys.Add(o => o.Id)) .RowAction(row => row.Selected = row.DataItem.Id.Equals(Model.ResultSet[0].Id)) ) <script type="text/javascript"> function onRowSelected(e) { var sId = e.row.cells[0].innerHTML; //This should be set to o.OId, how can I do this? var oId = e.row.cells[0].innerHTML; $.ajax({ type: 'GET', url: '/Controller/Action/', success: function () { window.location.href = '/Controller/Action/' //How can I add a route value (oId) and pass sId through to the querystring } }); } </script>CodeHobo
All-Star
18647 Points
2647 Posts
Re: How can I set 2 values when selecting a row in my telerik grid?
Mar 28, 2012 06:17 PM|LINK
oId is the second cell so
function onRowSelected(e) { var sId = e.row.cells[0].innerHTML; var oId = e.row.cells[1].innerHTML; $.ajax({ type: 'GET', url: '/Controller/Action/', success: function () { window.location.href = '/Controller/Save/?oId=' + oId + '&sId=' + sId //How can I add a route value (oId) and pass sId through to the querystring } }); } // In your action method now you can do public ActionResult Save(string oId,string sId){ // oID and sId have the values from the view. return View(); }Instead of changing the entire page url, you might want to consider using jquery and ajax to send the data
http://api.jquery.com/jQuery.ajax/
Blog | Twitter : @Hattan
young345
Member
201 Points
721 Posts
Re: How can I set 2 values when selecting a row in my telerik grid?
Mar 28, 2012 06:47 PM|LINK
thank you for your help.
oId is a route value, but i explicity have to say for example new { oId = oId} so that this certain route value is set, its not part of the querystring like sId. is there a way of me creating/mimicking new { oId = oId } in my javascript method mentioned above?
thank you very much
CodeHobo
All-Star
18647 Points
2647 Posts
Re: How can I set 2 values when selecting a row in my telerik grid?
Mar 28, 2012 07:46 PM|LINK
if it's a route value then just do this
Blog | Twitter : @Hattan
young345
Member
201 Points
721 Posts
Re: How can I set 2 values when selecting a row in my telerik grid?
Mar 28, 2012 08:09 PM|LINK
Thank you for your reply. I am unable to just say the following because it will create another route value
This is why I need to do something like new { oId=oId, sId=sId } as the code recognises oId as a specific type of route.Is it at all possible in javascript to call some kind of Html.Action(...) or create some kind of action link?
Your advice is much appreciated,
Thank you very much
CodeHobo
All-Star
18647 Points
2647 Posts
Re: How can I set 2 values when selecting a row in my telerik grid?
Mar 28, 2012 08:34 PM|LINK
What does the route table look like? If you know the expected format of the route it's easiest to just build the string yourself. I mean, ok in a different page use @Html.Action and pass in both values, what is the link or url? Based on that I can show you how to build the string in javascript.
By default no, because @Html is server side and javascript is client side. However, take a look at this blog posts which is able to solve this problem and provide routing capabilities from javascript
http://haacked.com/archive/2011/08/18/calling-asp-net-mvc-action-methods-from-javascript.aspx
Blog | Twitter : @Hattan