我有一个视图,我想以这种方式返回json值,但数据未返回。当删除return视图并且仅传递return json()时,数据显示在grid中。但格式不正确意味着数据显示不像jqgrid那样正确显示控制台窗口。我正在共享操作结果和json函数以及正在使用的Grid。
请仔细检查一下,然后告诉我什么是解决方案。如何在返回视图中返回json值。
Action
public ActionResult Ownership_Detail(string MemberShipID)
{
ViewBag.id = MemberShipID;
Ownership_D(MemberShipID);
return View(Json(ViewBag.objOwnershipDetail, JsonRequestBehavior.AllowGet));
}
json Function
public JsonResult Ownership_D(string MemberShipID)
{
List<OwnerShip> OwnerShipDetail = new List<OwnerShip>();
OwnerShip obj_ownership;
using (SqlConnection con = new SqlConnection(constring))
{
if (MemberShipID == null)
{
cmd = new SqlCommand("select ISNULL(FD.FileID, '') as FileID, FD.TransDate, P.PhaseName as Project, CONCAT(RTRIM(Sec.SectorName), +'/' + I.PlotNo) as UnitNo,RP.RPersonName " +
"from MemberMaster MM " +
"inner join MembersDetail MD on MD.MemberShipID = MM.MemberShipID and MD.Srno = 1 " +
"inner join MasterFileDetail FD on FD.MembershipID = MM.MemberShipID and FD.IsOwner = 1 and FD.IsTransfered = 1 and FD.IsCancelled=0 " +
"inner join MasterFile FM on FM.FileID = FD.FileID and FM.Cancel = 0 " +
"inner join Sectors Sec on Sec.Phase_ID = FM.PhaseId and Sec.Sector_ID = FM.Sector_ID " +
"left join PlotsInventory I on I.Phase_ID = FM.PhaseId and I.Plot_ID = FM.Plot_ID " +
"inner join Phases P on P.Phase_ID = FM.PhaseId " +
"inner join ReferencePerson RP on RP.RPersonID = FM.RPersonID " +
"where MM.MemberShipID = '" + clsCommon._MembershipID + "'", con);
}
else
{
cmd = new SqlCommand("select ISNULL(FD.FileID, '') as FileID, FD.TransDate, P.PhaseName as Project, CONCAT(RTRIM(Sec.SectorName), +'/' + I.PlotNo) as UnitNo,RP.RPersonName " +
"from MemberMaster MM " +
"inner join MembersDetail MD on MD.MemberShipID = MM.MemberShipID and MD.Srno = 1 " +
"inner join MasterFileDetail FD on FD.MembershipID = MM.MemberShipID and FD.IsOwner = 1 and FD.IsTransfered = 1 and FD.IsCancelled=0 " +
"inner join MasterFile FM on FM.FileID = FD.FileID and FM.Cancel = 0 " +
"inner join Sectors Sec on Sec.Phase_ID = FM.PhaseId and Sec.Sector_ID = FM.Sector_ID " +
"left join PlotsInventory I on I.Phase_ID = FM.PhaseId and I.Plot_ID = FM.Plot_ID " +
"inner join Phases P on P.Phase_ID = FM.PhaseId " +
"inner join ReferencePerson RP on RP.RPersonID = FM.RPersonID " +
"where MM.MemberShipID = '" + MemberShipID + "'", con);
}
cmd.CommandType = CommandType.Text;
con.Open();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataTable mdt_Member = new DataTable();
ad.Fill(mdt_Member);
for (int i = 0; i < mdt_Member.Rows.Count; i++)
{
obj_ownership = new OwnerShip();
obj_ownership.FileId = mdt_Member.Rows[i]["FileId"].ToString();
if (mdt_Member.Rows[i]["TransDate"] != DBNull.Value)
{
obj_ownership.TransDate = DateTime.Parse(mdt_Member.Rows[i]["TransDate"].ToString()).ToString("yyyy-MM-dd");
}
else
obj_ownership.TransDate = string.Empty;
obj_ownership.Project = mdt_Member.Rows[i]["Project"].ToString();
obj_ownership.UnitNo = mdt_Member.Rows[i]["UnitNo"].ToString();
obj_ownership.RpersonName = mdt_Member.Rows[i]["RpersonName"].ToString();
OwnerShipDetail.Add(obj_ownership);
ViewBag.objOwnershipDetail = OwnerShipDetail;
}
con.Close();
}
return Json(OwnerShipDetail, JsonRequestBehavior.AllowGet);
}
网格
<script type="text/javascript">
$.noConflict();
var rowsToColor = [];
jQuery(document).ready(function ($) {
var $grid = $("#jqGrid");
$grid.jqGrid({
url: '@Url.Action("Ownership_D")',
datatype: 'json',
postData: { MemberShipID: function () { return $("#mID").val(); } },
jsonReader: { id: 'FileID' },
colModel: [
{ name: 'FileId', index: 'FileId', label: 'File Id', width:3 },
{ name: 'UnitNo', index: 'UnitNo', label: 'Unit No', width:7 },
{ name: 'TransDate', index: 'TransDate', label: 'Trans Date', width:8},
{ name: 'RpersonName', index: 'RpersonName', label: 'File Category', width: 10 },
{ name: 'Project', index: 'Project', label: 'Project', width: 20 }
],
additionalProperties: [],
loadonce: true,
navOptions: {
reloadGridOptions: { fromServer: true }
},
formEditing: {
closeOnEscape: true,
closeAfterEdit: true,
savekey: [true, 13],
reloadGridOptions: {
fromServer: true
}
},
viewrecords: true,
height: 400,
autowidth:true,
rowNum: 100,
rowList: [10, 20, 30, 50, 100, 500],
pager: "#jqGridPager",
rownumbers: true,
footerrow: true,
sortname: "Name",
sortorder: "desc"
});
});
</script>