2016-09-21 56 views
0

我開發了一個模塊,它工作正常。我有幾個文本框並提交按鈕。提交文本框值時,我將比較那些與數據庫值並返回結果和顯示。我也用過分頁。這是我的行動方法。Mvc4在View中顯示模型屬性

[HttpGet] 
     public ActionResult Index(int? clientId, DateTime? dateofAction, int? doc_typeid, string employeeID,string citizenId,int? currentFilter, DateTime? filterdateTime,int? filterdocType,string filteredemployeeID,string filteredcitizenId,int? 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??DateTime.Now, docType, employeeID, citizenId); 
      IPagedList<logDetails> pagedLog = logDetails.ToPagedList(pageNumber, pageSize); 
      model = new logDetailsEnumeration() 
      { 
       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); 
      } 
     } 

這是查看

<div class="forms"> 
    @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>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> 
     } 

上面的代碼工作正常。我有一些困難。內部行動方法我有模型。在模型中我有Count = logDetails.Count。計數將保存返回的行數。當第一次加載頁面時,計數值將爲0,當數據庫中沒有相應的匹配數據時,計數值也將爲0.我想顯示如下所示的消息。

var records = @Html.Raw(Model.Count); 
      if(records==0) 
      { 
       fun_toastr_notify('success','No Records Found'); 
      } 

它也可以工作,但它也會在頁面加載第一次(計數爲零)時彈出消息。我可以知道我該如何避免這種情況?提前致謝。

+0

您可以使用Viewbag或Viewdata併爲其設置計數值。如果viewbag爲null,那麼它是第一個請求。當它來自你的動作時,將它設置爲計數,然後你可以在那裏進行比較。 –

回答

0

你有沒有試過類似的東西?這取決於搜索參數或任何你必須區分是否是第一次(代碼已經寫在飛行中,未測試)

<script type="javascript/text"> 
     document.ready(function(){ 
      @if (Model.logDetails.Count != 0) 
      { 
       fun_toastr_notify('success','No Records Found'); 
      } 
     }; 
</script> 
+0

謝謝。嘗試但沒有運氣。 –

+0

當第一次加載頁面時,當db中沒有相應的數據時,Model.logDetails.Count將爲零,那麼Model.logDetails.Count也將爲零。 –

0

首次傳遞一個viewbag.isfirsttime =真。 比代碼

<script> 

    @{ 
     var count = !ViewBag.IsFirstTime ? Model.Count : -1; 
    }; 
    var records = @count; 
    if (records == 0) { 
     fun_toastr_notify('success', 'No Records Found'); 
    } 
</script> 

這將工作肯定。快樂編碼!

+0

謝謝。不,它不起作用 –

+0

@NiranjanGodbole你怎麼用它?請分享代碼 –

+0

謝謝。和你上面發佈的一樣。 –