2016-09-30 63 views
1

嗨我正在開發應用程序,我有5個文本框和搜索按鈕。當我將數據輸入到文本框並單擊搜索時,數據庫的相應匹配數據將顯示在下表中。我已經實現了分頁,它工作正常。以下是代碼。如何在模型不返回任何數據時在MVC4中顯示消息?

[HttpGet] 
     public ActionResult Index(int? clientId, string dateofAction, int? doc_typeid, string employeeID,string citizenId,int? currentFilter, string filterdateTime,int? filterdocType,string filteredemployeeID,string filteredcitizenId,int? page) 
     { 
      ViewBag.curpageNumber = page; 

      logDetailsEnumeration model = new logDetailsEnumeration(); 
      DB_KYC3Entities db = new DB_KYC3Entities(); 
      var docTypes = from c in db.tm_doc_type select c; 
      if (clientId != null) 
      { 
       page = 1; 
      } 
      else 
      { 
       clientId = currentFilter; 
      } 
      if(dateofAction!=null) 
      { 
       page = 1; 
      } 
      else 
      { 
       dateofAction = filterdateTime; 
      } 
      if(doc_typeid != null) 
      { 
       page = 1; 
      } 
      else 
      { 
       doc_typeid = filterdocType; 

      } 
      if(employeeID!=null) 
      { 
       page = 1; 
      } 
      else 
      { 
       employeeID = filteredemployeeID; 
      } 
      if(citizenId!=null) 
      { 
       page = 1; 
      } 
      else 
      { 
       citizenId = filteredcitizenId; 
      } 

      ViewBag.CurrentFilter = clientId; 
      ViewBag.filterdateTime = dateofAction; 
      int pageSize = 8; 
      int pageNumber = (page ?? 1); 
      VerificationLogBAL obj = new VerificationLogBAL(); 
      int docType = obj.GetDocDetails(doc_typeid?? default(int)); 
      List<logDetails> logDetails = obj.getlogDetails(clientId?? default(int), dateofAction, docType, employeeID, citizenId); 
      IPagedList<logDetails> pagedLog = logDetails.ToPagedList(pageNumber, pageSize); 
      model = new logDetailsEnumeration() 
      { 
       pageNum= pageNumber, 
       doc_typeid = doc_typeid, 
       Count=logDetails.Count, 
       employeeID = employeeID, 
       citizenId= citizenId, 
       logDetails = pagedLog, 
       doctype_name=new SelectList(docTypes, "doc_typeid", "doctype_name") 
      }; 
      return View("Index",model); 
      } 
     } 

這是我的視圖代碼。

@using (Html.BeginForm("Index", "VerificationLog", FormMethod.Get)) 
    { 
     @Html.AntiForgeryToken() 
     @Html.ValidationSummary(true) 
     <div class="message"></div> 

     <div class="loginUsernamePassword"> 
      <i class="fa fa-user"></i> 
      <table width="100%" border="0" cellspacing="0" cellpadding="0" class="dataTable tableHover"> 
       <tr> 
        <th width="8%" scope="col">Client ID</th> 
        <th width="20%" scope="col"> 
         <div class="form-box form-box-default"> 
          @Html.TextBoxFor(x=>x.clientId, ViewBag.CurrentFilter as string, new { @id = "clientId", @placeholder = "Client ID", @class = "form-control", @maxlength = 20 }) 

         </div> 
        </th> 
        <th width="10%" scope="col">Date Of Action</th> 
        <th width="20%" scope="col"> 
         <div class="form-box form-box-default"> 
         @Html.TextBoxFor(x=>x.dateofAction, ViewBag.filterdateTime as string, new { @id = "dateofAction", @placeholder = "Date Of Action", @class = "txtBox form-control calender validate[required]" }) 
          @*@Html.TextBox("dateofAction", ViewBag.filterdateTime as string, new { @id = "dateofAction", @placeholder = "Date Of Action", @class = "txtBox form-control calender validate[required]" })*@ 


         </div> 
        </th> 
        <th width="15%" scope="col">Type Of Document</th> 
        <th width="17%" scope="col"> 
         <div class="form-box form-box-default"> 
          @*@Html.TextBox("typeofDocument", ViewBag.filterdateTime as string, new { @id = "typeofDocument", @placeholder = "Type Of Document", @class = "form-control", @maxlength = 20 })*@ 
          @Html.DropDownListFor(x=>x.doc_typeid,Model.doctype_name,"Select",new { @class = "form-control" }) 

               </div> 
        </th> 
       </tr> 
       <tr> 
        <th width="15%" scope="col">Employee ID</th> 
        <th width="17%" scope="col"> 
         <div class="form-box form-box-default"> 
          @Html.TextBoxFor(x=>x.employeeID, Model.employeeID, new { @id = "employeeID", @placeholder = "Employee ID", @class = "form-control", @maxlength = 20 }) 

         </div> 
        </th> 
        <th width="15%" scope="col">Citizen ID</th> 
        <th width="17%" scope="col"> 
         <div class="form-box form-box-default"> 
          @Html.TextBoxFor(x=>x.citizenId, Model.citizenId, new { @id = "citizenId", @placeholder = "Citizen ID", @class = "form-control", @maxlength = 20 }) 

         </div> 
        </th> 
        <th width="10%" scope="col" colspan="2"> 
         <input type="submit" value="Search" class="btn btn-primary btn-cons search" /> 
       </tr> 
      </table> 

      </div> 
      } 

     </div> 

     @if (Model != null && Model.logDetails.Count != 0) 
     { 
      <br /> 
      <h2>Verification Log</h2> 
      <br /> 
      <div id="GridDetails"> 
       <table width="100%" border="0" cellspacing="0" cellpadding="0" class="dataTable tableHover"> 
        <tr> 
         @*<th>@Html.ActionLink("Label", "Index", new { sortOrder = ViewBag.LabelSortParm, currentFilter = ViewBag.CurrentFilter, filterdateTime = ViewBag.filterdateTime, filterdocType = Model.doc_typeid, filteredemployeeID = Model.employeeID, filteredcitizenId = Model.citizenId, Page = ViewBag.curpageNumber })</th>*@ 
         <th>Label</th> 
         <th>Value</th> 
         <th>UpdatedOn</th> 
         <th>UpdatedBy</th> 
         <th>UpdatedStatus</th> 
         <th>RejectComment</th> 
        </tr> 
        @foreach (var group in Model.logDetails) 
        { 
         <tr> 
          <td>@group.contentLabel</td> 
          <td>@group.contentValue</td> 
          <td>@group.updatedOn</td> 
          <td>@group.updatedBy</td> 
          <td>@group.updatedStatus</td> 
          <td>@group.rejectComment</td> 
         </tr> 

        } 
       </table> 

       @Html.PagedListPager(Model.logDetails, page => Url.Action("Index", 
new { page, currentFilter = ViewBag.CurrentFilter, filterdateTime=ViewBag.filterdateTime, filterdocType= Model.doc_typeid, filteredemployeeID = Model.employeeID, filteredcitizenId = Model.citizenId })) 
       Page @(Model.logDetails.PageCount < Model.logDetails.PageNumber ? 0 : Model.logDetails.PageNumber) of @Model.logDetails.PageCount 
      </div> 

我有問題顯示消息沒有找到記錄。如果我嘗試通過檢查以下屬性來找到沒有記錄(如果(Model!= null),那麼當第一次加載頁面時也會顯示沒有找到記錄。我想在點擊提交按鈕後才顯示,並且沒有找到匹配的記錄。有什麼方法可以在上述情況下實施解決方案?謝謝

回答

2

您可以添加一個新的屬性到模型,只有當搜索完成後纔會設置。這將表明是否沒有找到記錄,並且您可以在視圖中使用邏輯來顯示適當的消息。

+0

感謝您的留言。我在想,當所有的輸入參數都爲空(只有頁面第一次加載時纔有可能),我可以設置一些值來建模屬性並在視圖上顯示消息。糾正我如果我錯了?有什麼好方法嗎? –

+0

爲什麼不直接使用SearchComplete(bool)。在初始GET時這將是錯誤的,您可以在POST上將其設置爲true。與現有的屬性結合使用,這應該給你足夠的選擇。或者,您可以添加特定的RecordsFound屬性並相應地使用SearchComplete和RecordsFound。 – ChrisBint

+0

謝謝。我會解決這個問題。我得到這個問題,因爲我沒有單獨的GET和POST操作方法? –

相關問題