如何在mvc 5中的按钮单击事件上显示webgrid [英] How to display webgrid on button click event in mvc 5
本文介绍了如何在mvc 5中的按钮单击事件上显示webgrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在开发mvc 5项目我希望能够在按钮点击事件上显示webgrid,也就是说,我有一个搜索按钮在视图上,如果找到记录,我想在webgrid上显示搜索结果。
以下是我的代码:
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:
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:
@{
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.
推荐答案
将您的webgrid代码放在此代码块中以检查viewbag是否为null
place your webgrid code inside this code block to check if viewbag is null
@if (ViewBag.ValReceipt != null)
{
show your webgrid
}
else
{
show some message
}
这篇关于如何在mvc 5中的按钮单击事件上显示webgrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文