Click here to Skip to main content
15,904,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I am working on mvc 5 project i want to be able to display webgrid on button click event, that is, i have a search button on the view, i want to display search results on the webgrid if the record(s) is/are found.

below is my code:

C#
public ActionResult ValidateReceipt()
       {
           return View();
       }

       [HttpPost]
       public ActionResult ValidateReceipt(Payment_History payment)
       {
           var receiptSerialNo =
               (from r in db.Payment_History where (r.pin_serial_no == payment.pin_serial_no) select r).FirstOrDefault();
           var studentsReciept = new List<Payment_History>();
           if (receiptSerialNo != null)
           {
               studentsReciept.Add(new Payment_History { surname = receiptSerialNo.surname, amount_paid =  receiptSerialNo.amount_paid, school_fees_pin = receiptSerialNo.school_fees_pin, term = receiptSerialNo.term});
               ViewBag.ValReceipt = studentsReciept;
           }
           return View(studentsReciept);
       }


View Code:

C#
@{

               var obj = new WebGrid(ViewBag.ValReceipt, canPage : true, canSort : false, rowsPerPage : 1);
               obj.Pager(WebGridPagerModes.All);
           }

           @obj.GetHtml(tableStyle: "webgrid-table",
               headerStyle: "webgrid-header",
               footerStyle: "webgrid-footer",
               alternatingRowStyle: "webgrid-alternating-row",
               rowStyle: "webgrid-row-style",
               columns: obj.Columns(
                   obj.Column("pin_serial_no", "Fee SerialNo"),
                    obj.Column("School_fees_pin", "PIN"),
                   obj.Column("surname", "Surname"),
                    obj.Column("first_name", "Firstname"),
                   obj.Column("amount_paid", "Fee"),
                   obj.Column("session_year", "Session"),
                   obj.Column("term", "Term"),
                   obj.Column("class", "Class")
                   ))

Thanks for your help.
Posted
Updated 27-Sep-15 1:09am
v3

1 solution

place your webgrid code inside this code block to check if viewbag is null


HTML
@if (ViewBag.ValReceipt != null)
{
    show your webgrid        
}
else
{
    show some message       
}
 
Share this answer
 
Comments
Uwakpeter 28-Sep-15 4:34am    
Thanks for your response, i have tried this but having exception, i tried to google it, but couldnt fix it, the exception it thrown is:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[SIMSPortal.Payment_History]', but this dictionary requires a model item of type 'SIMSPortal.Payment_History'.

please i will appreciate if you could assist any further. thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900