This is a migrated thread and some comments may be shown as answers.
[GridAction] controller method reading data from input elements in the view
2 Answers 22 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Matthew
Top achievements
Rank 1
Matthew asked on 23 Apr 2013, 02:28 PM
I've implemented batch editing in a radgrid by following this example..  The grid itself displays a list of search results.  I pass the search criteria to the view using ViewData dictionary.

After the edit, I want to make sure the grid displays the exact same recordset, meaning I have to somehow get the search results stored in the view by way of hidden inputs into the [GridAction] method in the controller.

My view contains two hidden elements:
<input type="hidden" id="FirstName" name="FirstName" value="@ViewData["FirstName"]"/>
<input type="hidden" id="LastName" name="LastName" value="@ViewData["LastName"]"/>

Here is the grid in my view:  You can see the [GridAction] method I'm referring to is called _SaveBatchEditing

@(Html.Telerik().Grid(Model)
            .Name("Grid")
            .DataKeys(d => d.Add<int>(a => a.Id).RouteKey("Id"))
            .Columns(columns =>
            {
                columns.Bound<int>(c => c.Id).Width(100);
                columns.Bound<string>(c => c.FirstName).Width(120);
                columns.Bound<string>(c => c.LastName).Width(200);
                columns.Bound<string>(c => c.ECPostalCode).Width(100);
                columns.Bound<string>(c => c.Phone).Width(120);
                columns.Bound<string>(c => c.Street).Width(200);
                columns.Bound<string>(c => c.City).Width(100);
                columns.Bound<string>(c => c.Province).Width(75);
                columns.Bound<string>(c => c.PostalCode).Width(100);
                columns.Bound<string>(c => c.Email).Width(100);
                columns.Bound<bool>(c => c.OkToContact).Width(50);
                /*columns.Bound<string>(c => c.BirthDay).Width(130).Format("{0:d}");*/
                columns.Command(commands =>
                {
                    commands.Edit();
                    commands.Delete();
                }).Width(200);
             
            })
            .DataBinding(dataBinding => dataBinding
            .Ajax()
                .Select("_SelectBatchEditing", "Home")
                .Update("_SaveBatchEditing", "Home")
            )
 
            .ToolBar(commands => {
            commands.Insert();
            commands.SubmitChanges();
            })
 
            .Sortable()
            .Pageable()
            .Scrollable(scrolling => scrolling.Height(494)).Footer(false)
            .Editable(editing => editing.Mode(GridEditMode.InCell))
            .Editable(editing => editing.Mode(GridEditMode.InCell).DefaultDataItem(new ContactView
            {
                OkToContact = true
            }))
            .ClientEvents(events =>
            events.OnDataBinding("Grid_onDataBinding").OnError("Grid_onError"))
         )

And here is my controller method.  How do I get the values of those hidden elements into the controller

[AcceptVerbs(HttpVerbs.Post)]
       [GridAction]
       public ActionResult _SaveBatchEditing([Bind(Prefix = "inserted")]IEnumerable<Contact> insertedContacts,
           [Bind(Prefix = "updated")]IEnumerable<Contact> updatedContacts,
           [Bind(Prefix = "deleted")]IEnumerable<Contact> deletedContacts)
       {
 
           if (insertedContacts != null)
           {
               foreach (var product in insertedContacts)
               {
                   //SessionProductRepository.Insert(product);
               }
           }
           if (updatedContacts != null)
           {
               foreach (var product in updatedContacts)
               {
                   //code to update
               }
           }
           if (deletedContacts != null)
           {
               foreach (var product in deletedContacts)
               {
                   //SessionProductRepository.Delete(product);
               }
           }
 
           IList<ContactView> Contacts = new List<ContactView>();
           // Here is where I want to redo the search using the hidden inputs
           // in the view, shown above.
           return View(new GridModel(Contacts));
       }

Can anyone shed some insight?  Thanks so much!



2 Answers, 1 is accepted

Sort by
0
Matthew
Top achievements
Rank 1
answered on 23 Apr 2013, 07:47 PM
I have found the answer at http://www.telerik.com/community/forums/aspnet-mvc/general/batch-editing-return-gridmodel.aspx:

function onSubmitChanges(e) {
    e.values.startDate = $("#startDate").val();
    e.values.endDate = $("#endDate").val();
}

I can then access the values through FormCollection parameter

0
Ricardo
Top achievements
Rank 2
answered on 02 May 2013, 11:48 AM
Hi Matthew.

You could also do the following:
on your javascript:
function onSubmitChanges(e) {
    e.values.FirstName = $("#FirstName ").val();
    e.values.LastName = $("#LastName").val();
}

and on your controller, instead of using FormCollection parameter, you could do this:
[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult _SaveBatchEditing([Bind(Prefix = "inserted")]IEnumerable<Contact> insertedContacts,
[Bind(Prefix = "updated")]IEnumerable<Contact> updatedContacts,
[Bind(Prefix = "deleted")]IEnumerable<Contact> deletedContacts,
string FirstName,
string LastName)
{
...
}

Tags
Grid
Asked by
Matthew
Top achievements
Rank 1
Answers by
Matthew
Top achievements
Rank 1
Ricardo
Top achievements
Rank 2
Share this question
or