2013-10-14 51 views
0

我想创建一个自定义网格,该网格将有三列,并且行可以是取决于数据的任何数字。但我的问题是数据是可用的JSON。我创建了网格状结构很多次,但就是与模型和集合,如:使用Json数据在Mvc 4.0中创建自定义网格

首先创建列

@{ 
       if (Model.MessageList.Count > 0) 
       { 
       <div class="GridView_Div"> 
        <div class="GridHeader_Div"> 
         <div class="GridHeaderColoums_Div">Message</div> 
         <div class="GridHeaderColoums_Div">Sent Date</div> 
         <div class="GridHeaderColoums_Div">Receive Date</div> 
         <div class="GridHeaderColoums_Div">Actions</div> 
        </div> 
        <div class="GridData_Div"> 
         @{ 
        for (int i = 0; i < Model.MessageList.Count; i++) 
        { 
         string resultMessage = string.Empty; 
         string newMessage = string.Empty; 
         string result1 = Model.MessageList.ElementAt(i).Message; 
         int length = result1.Length; 
         if (length > 5) 
         { 
          resultMessage = result1.Substring(0, 5); 
          newMessage = resultMessage + "......"; 
         } 
         else 
         { 
          resultMessage = result1.Substring(0); 
          newMessage = resultMessage + "......"; 
         } 
          <div class="Grid_Row"> 
           <div class="GridData_Coloums"> 
            <a href="#" onclick="ShowMessageDetail('@Model.MessageList.ElementAt(i).pkMessageId')" id="@Model.MessageList.ElementAt(i).pkMessageId">@newMessage</a> 
           </div> 
           <div class="GridData_Coloums">@Model.MessageList.ElementAt(i).Sent_Date</div> 
           <div class="GridData_Coloums">&nbsp; @Model.MessageList.ElementAt(i).Receive_Date</div> 
           <div class="GridData_Coloums"> 
            <input type="button" value="Delete" id="@Model.MessageList.ElementAt(i).pkMessageId"/> 
           </div> 
          </div> 
        } 
         } 
        </div> 
       </div> 
       } 
       else 
       { 
       <div style="width: 50%; float: left; margin-left: 10%;">No Message Found</div> 
       } 
      } 

但我怎么可以创建JSON数据状结构网格的div?

请帮我解决这个问题。非常感谢您

+0

如果你是与第三方库OK下面的链接,我建议KendoUI,它有一个强大的网格绑定prefectly使用JSON。 –

+0

先生,我使用过KendoUI。控制真的非常好,如你所说,但我真的想用这种方式做事。我的意思是我想要自己构建它。请帮助如果这可能。 – Sandy

回答

1

这是一个挑战,对我来说,我做到了。

在视图

$(document).ready(function() { 
     $("#hdnPkClientId").val(''); 
     $("#txt_Autocomplete").autocomplete({ 
      source: function (request, response) { 
       $.ajax({ 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        url: "/Home/SearchClientDetail", 
        data: "{'searchtext':'" + document.getElementById('txt_Autocomplete').value + "'}", 
        dataType: "json", 
        success: function (data) { 
         response($.map(data.Data, function (item) { 

          return { 

           label: item.Name, 
           value: item.id, 
           data: item 
          }; 
         })); 
        }, 
        error: function (xhr) 
        { } 
       }); 
      }, 
      select: function (event, ui) { 
       var detailArr = ui.item.label.split(','); 
       $("#txt_Autocomplete").val(detailArr[0]); 
       $("#hdnPkClientId").val(ui.item.data.Id); 
       $("#Evaluation_Anch").attr("href", "/Evaluation/Index?Client_ID=" + ui.item.data.Id); 
       $.ajax({ 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        url: "/ClientHome/GetSelectedClientDetails", 
        data: "{'ClientId':'" + document.getElementById('hdnPkClientId').value + "'}", 
        dataType: "json", 
        success: function (data) { 
         $("#Client_Name").html(data.Info.Name); 
         $("#Client_Address").html(data.Info.Address); 
         $("#Client_OtherAddressDetails").html(data.Info.OtherAddressDetails); 
         $("#Client_DOB").html(data.Info.DOB); 
         $("#Client_Phone").html(data.Info.Phone); 
         $("Client_MobilePhone").html(data.Info.Mobile_Phone); 
         var DataDiv = "<table width='100' border='0' cellspacing='0' cellpadding='0'> <tr> <th class='head'>Date</th> <th class='head'>Document Type</th> <th class='head'>Provider</th> </tr>"; 
         for (var iRow = 0; iRow < data.Info.Prev_Doc_List.length; iRow++) { 
          var temp = data.Info.Prev_Doc_List[iRow]; 
          DataDiv += "<tr>"; 
          DataDiv += "<td>" + temp.Created_Date + "</td>"; 
          DataDiv += "<td><a id='" + temp.Page_ID + "' href='" + temp.RedirectAddress + "'>" + temp.Doc_type + "</a></td>"; 
          DataDiv += "<td>" + temp.Provider + "</td>"; 
          DataDiv += "</tr>"; 
         } 
         DataDiv += "</table>"; 
         $("#PreviousDocumentDiv").html(DataDiv); 
        }, 
        error: function (xhr) 
        { } 
       }); 
       return false; 
      } 
     }); 
    }); 

在控制器

[HttpPost] 
     public ActionResult GetSelectedClientDetails(string ClientId) 
     { 
      ProgressNotesService objService = new ProgressNotesService(); 
      var Client_Detail = objService.GetSelectedClient(Convert.ToInt32(ClientId)); 
      if (Client_Detail != null) 
      { 
       Session["Client_ID"] = Client_Detail.Id; 
      } 
      return Json(new { Info = Client_Detail }); 
     } 

在服务

public ClientDetailModel GetSelectedClient(int ClientID) 
     { 
      ClientDetailModel ClientDetail = new ClientDetailModel(); 
      List<PreviousDocuments> objDocsList = new List<PreviousDocuments>(); 
      using (DataContext DB = new DataContext()) 
      { 
       var Row = DB.tbl.Where(m => m.ID == ClientID).FirstOrDefault(); 
       if (Row != null) 
       { 
        ClientDetail.Id = Row.ID.ToString(); 
        ClientDetail.Name = Row.First_Name + " " + Row.Last_Name; 
        ClientDetail.Address = Row.Address; 
        ClientDetail.Client_DOB = (DateTime)Row.DOB; 
        ClientDetail.DOB = ClientDetail.Client_DOB.ToString("MM/dd/yyyy"); 
        ClientDetail.OtherAddressDetails = (Row.City == "" || Row.City == null ? "N/A" + " " + "," : Row.City + " ,") + (Row.State == "" || Row.State == null ? "N/A" + " " + "," : Row.State + " ,") + (Row.ZipCode == "" || Row.ZipCode == null ? "N/A" : Row.ZipCode); 
        ClientDetail.Phone = Row.Phone; 
        ClientDetail.Mobile_Phone = Row.OtherContact_Phone; 
        var ProgressNoteRecords = DB.ProgressNotes.Where(m => m.FkReferralID == ClientID).ToList(); 
        if (ProgressNoteRecords.Count() > 0) 
        { 
         for (int iRow = 0; iRow < ProgressNoteRecords.Count(); iRow++) 
         { 
          objDocsList.Add(new PreviousDocuments 
          { 
           Created_Date = Convert.ToString(ProgressNoteRecords.ElementAt(iRow).Created_Date), 
           Doc_type = "Progress Note", 
           Provider = " blah blah", 
           Page_ID = Convert.ToString(ProgressNoteRecords.ElementAt(iRow).Id), 
           RedirectAddress = "../ProgressNote/Add?ProgressNote_ID=" + Convert.ToString(ProgressNoteRecords.ElementAt(iRow).Id) 

          }); 
         } 
        } 
        var Ref = DB.tblrefServices.Where(m => m.ID == ClientID).ToList(); 
        if (Ref.Count > 0) 
        { 
         for (int iRow = 0; iRow < Ref.Count(); iRow++) 
         { 
          objDocsList.Add(new PreviousDocuments 
          { 
           Created_Date = Convert.ToString(Ref.ElementAt(iRow).First_Name), 
           Doc_type = "Referral Service", 
           Provider = "blah blah", 
           Page_ID = Convert.ToString(Ref.ElementAt(iRow).ID) 
          }); 
         } 
        } 
        ClientDetail.Prev_Doc_List = objDocsList; 
       } 

      } 
      return ClientDetail; 

     } 

在模型

public class ClientDetailModel 
    { 
     public ClientDetailModel() 
     { 
      Prev_Doc_List = new List<PreviousDocuments>(); 
     } 
     public string Name { get; set; } 
     public string Id { get; set; } 
     public string DOB { get; set; } 
     public string Address { get; set; } 
     public string OtherAddressDetails { get; set; } 
     public string Phone { get; set; } 
     public string Mobile_Phone { get; set; } 
     public DateTime Client_DOB { get; set; } 
     public List<PreviousDocuments> Prev_Doc_List { get; set; } 

    } 
    public class PreviousDocuments 
    { 
     public string Created_Date { get; set; } 
     public string Doc_type { get; set; } 
     public string Provider { get; set; } 
     public string Page_ID { get; set; } 
     public string RedirectAddress { get; set; } 
    } 
0

您可以使用KnockoutJS你的JSON数据绑定到您的自定义网格

+0

您可以请解释一下这个方法更多一些。请告诉一些有用的参考资料还有一些演示代码或我怎么使用它? – Sandy

+0

你可以在那里找到资源 - > http://knockoutjs.com/ –

+0

还有很多样品在那里... –

相关问题