This is a migrated thread and some comments may be shown as answers.

Access hidden field from parial view in grid editortemplate

5 Answers 1435 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Stephen asked on 18 Nov 2013, 06:52 PM
I've got a view with a kendo grid on it.  Above that I have a partial view that loads a value and stores it in a hidden.  I need to be able to pass the value of this hidden field to the get, add, update, and delete methods of the grid preferably without having to load the grid via jquery (if possible).

Or is there a way that the hidden field on the view could be accessible or passed to the editortemplate for the grid so that I can make it part of the view model when a row is added or updated?

Here is the main view:
@model IEnumerable<PASSAdmin.ViewModels.UserFacilityAdmin.ProposalTypeViewModel>
 
@{
    ViewBag.Title = "Proposal Types";
}
 
<h2>Proposal Types</h2>
 
@Html.Partial("_LastViewedUserFacility")
 
@{    
    Html.Kendo().Grid(Model)
    .Name("gridProposalTypes")
    .Columns(columns =>
    {
        columns.Command(command => { command.Edit(); }).Width(50);
        columns.Bound(o => o.Code);
        columns.Bound(o => o.Description);
        columns.Bound(o => o.Technique_Selection_Count).Title("# Techniques");
        columns.Bound(o => o.Resource_Selection_Count).Title("# Resources");
        columns.Bound(o => o.Selection_Order).Title("Selection Order");       
        columns.Bound(o => o.Active);
        columns.Command(command => { command.Destroy(); }).Width(50);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("UserFacilityAdmin/ProposalType").Window(window => window.Width(400)))
    .Pageable()
    .Sortable()
    .DataSource(dataSource => dataSource
        .Server()
        .Model(model => model.Id(o => o.ID))
        .Create(create => create.Action("AddProposalType", "UserFacilityAdmin"))
        .Read(read => read.Action("ProposalTypes", "UserFacilityAdmin"))
        .Update(update => update.Action("UpdateProposalType", "UserFacilityAdmin"))
        .Destroy(destroy => destroy.Action("DeleteProposalType", "UserFacilityAdmin"))
    )
    .Render();
}
Here is the partial view loaded above the grid:
<p>User Facility: <span id="userfacility"></span></p>
 
<input type="hidden" name="User_Facility_ID" id="User_Facility_ID" />
 
<script type="text/javascript">
$(document).ready(function () {
    $.post('/Search/GetLastViewedUserFacilityID', function (data) {
        $("#userfacility").html(data);
        $("#User_Facility_ID").val(data);
    });
});
</script>
The "User_Facility_ID" is the value I need when doing the get, add, update, and delete for the grid.





5 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 20 Nov 2013, 01:09 PM
Hello Stephen,

To send additional values to the server you need to use the Data function of the different operations. The documentations explains how to achieve it for the read operations, same goes for the rest of the operations

http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/faq#how-do-i-send-values-to-my-action-method-when-binding-the-grid?


Kind Regards,
Petur Subev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Stephen
Top achievements
Rank 1
answered on 20 Nov 2013, 02:52 PM
Hi,

I've got two questions:

1. I've seen that and I can make it work in ajax for the read.  On the other methods like Create it does not pass the variable.  I am using a editortemplate and this seems to be the problem.  If I remove my editortemplate it passes the parameter but when I have the editor template in place it passes a null.  How do I correct this as I need the editortemplate.

2. I'm wondering if there is a way I can get the value of the hidden loaded with the partial view and still have the grid in server binding mode.  I assume that the partial view and the grid load at the same time so the grid would not have access to the hidden field from the partial view, but maybe there is a way to delay the loading of the grid or reload the datasource somehow after the partial view loads?  If so, what is the syntax for appending the value of the hidden to the parameter for the read and other operations?
.Read(read => read.Action("Read", "Home", new { userID = value_of_the_hidden }))

Thanks,

Steve
0
Petur Subev
Telerik team
answered on 22 Nov 2013, 01:42 PM
Hello again,

1) Please demonstrate the problem with a small demo project so we can investigate further. Here is what I tried on my side and it seems to be sending the values to the server properly.
     http://screencast.com/t/tiO9l6vAOnk
2) I am not sure I understand the situation, the page is fully re-loaded when using server binding and you cannot change the values that you are about to send dynamically, consider using Ajax bound Grid.


Kind Regards,
Petur Subev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Stephen
Top achievements
Rank 1
answered on 22 Nov 2013, 04:27 PM
Hi,

1. I'll try to put together a sample project but the problem I described in my most recent response with the Add was with Ajax binding, not server binding.

2. With the server binding option, I'm not looking to change the value of the parameter dynamically, but I won't have a hardcoded parameter like you do in your example.  The value of the parameter I need is the value of a hidden on the same view.  Is there a way to do that.

<input type="hidden" id="User_Facility_ID" name="User_Facility_ID">
 
...
.Read(read => read.Action("Read", "Home", new { userID = value_of_the_hidden_above }))
...

0
Petur Subev
Telerik team
answered on 26 Nov 2013, 04:34 PM
Hello again,

The following code is a C# code executed on the server-side:

.Read(read => read.Action("Read", "Home", new { userID = value_of_the_hidden_above }))

You cannot access hidden fields since to access them you need to use JavaScript.

Kind Regards,
Petur Subev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Stephen
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Stephen
Top achievements
Rank 1
Share this question
or