1

I have a page which load data from multiple partial views. One of the partial view has few links. say 3. Every time user clicks on the link I reload the page with a data related to clicked link. The form looks like this.

@model TestPostback.Models.HomeModel
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<form id="testform" method="post" action="@Url.Action("Index","Home")" runat="server">
    <div>
        <table>
            <tr>
                <td>                    
                        <a href="@Url.Content("/Home/Index?selectedValue=Personal")"> This is a test page </a>
                </td>
            </tr>            
        </table>
    </div>
</form>

<div>
    <table>
        <tr>
            <td style="vertical-align:top;">                                        
                <h2 class="expand">Test Expand</h2>
                <div class="collapse">
                    <table id="testgrid" class="scroll" cellpadding="0" cellspacing="0"></table>
                </div>
            </td>
        </tr>
    </table>
</div>


<script type="text/javascript">    

    jQuery(document).ready(function () {
        var grid = jQuery("#testgrid");        
        grid.jqGrid({
            url: 'Home/GridData/',
            datatype: 'json',
            mtype: 'POST',
            colNames: ['Id', 'Votes', 'Title'],
            colModel: [
                        { name: 'Id', index: 'Id', width: 40, align: 'left' },
                        { name: 'Votes', index: 'Votes', width: 40, align: 'left' },
                        { name: 'Title', index: 'Title', width: 400, align: 'left'}
                    ],
            rowNum: 10,
            rowList: [5, 10, 20, 50],
            sortname: 'Id',
            sortorder: "desc",
            viewrecords: true,
            imgpath: '',
            caption: 'My first grid'
        });        
    });    
</script>

Controller Code :-

 public ActionResult Index(string selectedValue)
        {
            return View();
        }

        public JsonResult GridData(string sidx, string sord, int page, int rows)
        {
            int totalPages = 1; // we'll implement later
            int pageSize = rows;
            int totalRecords = 3; // implement later

            var jsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,
                rows = new[]{
                    new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}},
                    new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?"}},
                    new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?"}}
                }
            };
            return Json(jsonData);
        }

Here is what I am trying to do. When i click on Link "This is a Test Page", it calls the controller with some value but it does not load my grid. I want that value because i want to load a grid with different value based on user clicks. what are the changes I have to make for this to work?

please advice.

Vivek Patel
  • 349
  • 2
  • 4
  • 21
  • are you loading via ajax? code here doesnt show and since you have just one form here and no grids shown I have to imagine so? more details please : ) – Adam Tuliper Jan 04 '12 at 18:09

2 Answers2

2

I'd recommend following the PRG (post redirect get) pattern: so after the post, you redirect to whichever action method is suitable (in this case the one that displays all the jqGrids). With MVC this can be achieved using a RedirectResult or the RedirectToAction method available on Controller.

Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
  • This wont work because I have to load few other partial views with data/text from database. So I want to do is return a view which loads all the data in my partial views and also then call those controller methods which loads my Grid Data. – Vivek Patel Jan 04 '12 at 17:40
0

Are you serializing your data when you get it back (ie serializeGridData)

Are you loading by JSON?

If so, check out Setting the content-type of requests performed by jQuery jqGrid

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71