我无法在webgrid中获取数据。 这是我的控制器代码。
public ActionResult Index()
{
List<tm_grp_group> pm = db.tm_grp_group.ToList();
return View();
}
//
// GET: /GroupMaster/Details/5
public JsonResult GetAllData()
{
List<tm_grp_group> tm = db.tm_grp_group.ToList();
return Json(tm, JsonRequestBehavior.AllowGet);
}
public void SaveNew(string grp_name, string grp_description, bool grp_isactive)
{
tm_grp_group tm = new tm_grp_group();
tm.grp_name = grp_name;
tm.grp_description = grp_description;
tm.grp_isactive = grp_isactive;
db.SaveChanges();
}
public void DeleteById(int grp_id)
{
tm_grp_group pro = db.tm_grp_group.First(x => x.grp_id == grp_id);
db.tm_grp_group.Remove(pro);
db.SaveChanges();
}
public void UpdateEdit(int grp_id, string grp_name, string grp_description, bool grp_isactive)
{
tm_grp_group pro = db.tm_grp_group.First(x => x.grp_id == grp_id);
pro.grp_id = grp_id;
pro.grp_name = grp_name;
pro.grp_description = grp_description;
pro.grp_isactive = grp_isactive;
db.SaveChanges();
}
public JsonResult GetDataById(int grp_id)
{
tm_grp_group pro = db.tm_grp_group.First(x => x.grp_id == grp_id);
return Json(pro, JsonRequestBehavior.AllowGet);
}
}
}
这是我的观看代码。
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
function ViewAll() {
/////////// Fetch all Method /////////////
$('#tblgrpmaster tbody').html('');
$.ajax(
{
type: 'POST',
url: 'GroupMaster/GetAllData',
dataType: 'json',
data: { id: 'grp_id' },
success: function (ashdata) {
var items = '';
$.each(ashdata, function (i, item) {
var rows = "<tr>"
+ "<td >" + item.grp_id + "</td>"
+ "<td >" + item.grp_name + "</td>"
+ "<td >" + item.grp_description + "</td>"
+ "<td >" + item.grp_isactive + "</td>"
+ "<td ><a href='#' onclick='GetDataEdit(" + item.grp_id +
");'>Edit</a> | <a href='#' onclick='Delete(" + item.grp_id + ");' title='" + item.grp_id + "'>Delete</a></td>"
+ "</tr>";
$('#tblgrpmaster tbody').append(rows);
});
}
});
}
$(document).ready(function () {
ViewAll();
});
<table>
@Html.Hidden("grp_id")
<tr><td>Group Name : </td><td>@Html.TextBox("grp_name")</td></tr>
<tr><td>Group Description : </td><td>@Html.TextBox("grp_description")</td></tr>
<tr><td>Group IsActive : </td><td>@Html.TextBox("grp_isactive")</td></tr>
<tr><td></td><td><a href="#" id="Save" onclick="SaveNew();">Save</a></td></tr>
</table>
<table id="tblgrpmaster">
<thead>
<tr>
<th align="left" >
GroupID
</th>
<th align="left" >
Group Name
</th>
<th align="left" >
Group Description
</th>
<th align="left">
Group IsActive
</th>
</tr>
</thead>
<tbody></tbody>
</table>
由于空间问题,我没有复制到此处保存的编辑删除代码。每当第一次运行i index动作方法时,我想在视图中显示数据。但我无法在webgrid中获取数据。我可否知道我在这里出错了?
答案 0 :(得分:0)
更改你的ajax调用:
$.ajax(
{
type: 'GET',
url: 'GroupMaster/GetAllData',
dataType: 'json',
success: function (ashdata) {
var items = '';
$.each(ashdata, function (i, item) {
var rows = "<tr>"
+ "<td >" + item.grp_id + "</td>"
+ "<td >" + item.grp_name + "</td>"
+ "<td >" + item.grp_description + "</td>"
+ "<td >" + item.grp_isactive + "</td>"
+ "<td ><a href='#' onclick='GetDataEdit(" + item.grp_id +
");'>Edit</a> | <a href='#' onclick='Delete(" + item.grp_id + ");' title='" + item.grp_id + "'>Delete</a></td>"
+ "</tr>";
$('#tblgrpmaster tbody').append(rows);
});
},
error: function(e)
{
alert(JSON.stringify(e));
}
});
正如您所看到的,GetAllData不接受任何参数,它也是HttpGet方法,因此需要更改类型以获取和删除数据部分