2013-02-01 157 views
1

我將視圖中的日期傳遞給控制器​​,然後從控制器中查看這兩個日期之間的記錄數。隨着更多的一個記錄將獲取,所以我使用IEnumerable,所以我可以顯示記錄列表。但是在使用IEnumerable時,它無法從視圖中傳遞選定日期並傳遞給控制器​​。任何人都可以解釋爲什麼?將數據從視圖傳遞到控制器和控制器以查看

這是我的代碼:

查看:Index.aspx的

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<ApricaCRMEvent.Models.CRM.DatabaseEntities.CRM_Doctor_Request>>" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Index 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
<link rel="stylesheet" href="../../jquery-ui-1.8.17.custom/development-bundle/themes/base/jquery.ui.all.css"> 

<script src="../../jquery-ui-1.8.17.custom/development-bundle/jquery-1.7.1.js"></script> 

<script src="../../jquery-ui-1.8.17.custom/development-bundle/ui/jquery.ui.core.js"></script> 

<script src="../../jquery-ui-1.8.17.custom/development-bundle/ui/jquery.ui.widget.js"></script> 

<script src="../../jquery-ui-1.8.17.custom/development-bundle/ui/jquery.ui.datepicker.js"></script> 

<link rel="stylesheet" href="../../jquery-ui-1.8.17.custom/development-bundle/demos/demos.css"> 

<script> 


    function PassValue() { 
     var todate = document.getElementById("Date_Modified"); 
     var fromdate = document.getElementById("Date_Created"); 
     todate.value = document.getElementById("txtToDate").value; 
     fromdate.value = document.getElementById("txtFromDate").value; 
    } 

    $(function() { 

     $("#txtToDate").datepicker({ 
      altField: "#alternate", 
      altFormat: "DD, d MM, yy" 
     }); 


     $("#txtFromDate").datepicker({ 
      altField: "#alternate", 
      altFormat: "DD, d MM, yy" 
     }); 
    }); 

</script> 

<div> 
<h2>Search by Date</h2> 
    <% using (Html.BeginForm()) 
     { %> 
     <%: Html.ValidationSummary(true, "Profile Updation was unsuccessful. Please correct the errors and try again.") %> 

     <%= Html.HiddenFor(model=>model.Date_Created) %> 
     <%= Html.HiddenFor(model=>model.Date_Modified) %> 
     <table> 
      <tr> 
      <td>Enter From Date</td> 
      <td><input type="text" id="txtFromDate"></td> 
      <td>Enter To Date</td> 
      <td><input type="text" id="txtToDate"></td> 

      <td><input type="submit" name="btnSearchDate" value="Search" onclick="PassValue()" /></td> 
      </tr> 
     </table> 
    <% } %> 
</div> 
<div id="showtable"> 
<table> 
    <tr> 
     <th> 
      Request_For_Id 
     </th> 
     <th> 
      Territory 
     </th> 
     <th> 
      Estimated_Amount 
     </th> 
     <th> 
      Actual_Amount 
     </th> 
     <th> 
      Date_Created 
     </th> 
     <th> 
      Compute_CRM_State 
     </th> 
     <th> 
      Compute_Event_Type 
     </th> 
    </tr> 
    <% foreach (var item in Model) 
     { %> 
    <tr> 
     <td> 
      <%: Html.DisplayFor(modelItem => item.Request_For_Id)%> 
     </td> 
     <td> 
      <%: Html.DisplayFor(modelItem => item.Territory)%> 
     </td> 

     <td> 
      <%: Html.DisplayFor(modelItem => item.Estimated_Amount)%> 
     </td> 
     <td> 
      <%: Html.DisplayFor(modelItem => item.Actual_Amount)%> 
     </td> 
     <td> 
      <%: Html.DisplayFor(modelItem => item.Date_Created)%> 
     </td> 
     <td> 
      <%: Html.DisplayFor(modelItem => item.Compute_CRM_State)%> 
     </td> 
     <td> 
      <%: Html.DisplayFor(modelItem => item.Compute_Event_Type)%> 
     </td> 
    </tr> 
    <%} %> 
</table> 
</div> 
</asp:Content> 

控制器

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using ApricaCRMEvent.Models.CRM.DatabaseEntities; 
using ApricaCRMEvent.Models.CRM.DataLayer; 

namespace ApricaCRMEvent.Controllers.CRM 
{ 
    public class SearchDateWiseController : Controller 
    { 
     // 
     // GET: /SearchDateWise/ 
     CRM_Doctor_Request dateObj = new CRM_Doctor_Request(); 

     public ActionResult Index() 
     { 
      return View(); 
     } 

     [HttpPost] 
     public ActionResult Index(CRM_Doctor_Request model) 
     { 

    return View(SearchMDLNoDL.getDateWiseDetails(model.Date_Created,model.Date_Modified).ToList()); 
     } 

    } 
} 

雖然使用了IEnumerable顯示編譯錯誤

Line 49:   <%: Html.ValidationSummary(true, "Profile Updation was unsuccessful. Please correct the errors and try again.") %> 
Line 50: 
Line 51:   **<%= Html.HiddenFor(model=>model.Date_Created) %> 
Line 52:   <%= Html.HiddenFor(model=>model.Date_Modified) %>** 
Line 53:   <table> 

編譯器錯誤消息:CS1061:'System.Collections.Generic.IEnumerable'不包含'Date_Created'的定義,也沒有接受類型'System.Collections.Generic.IEnumerable'的第一個參數的擴展方法'Date_Created'可以找到(您是否缺少使用指令或程序集引用?)

+0

您是否將可枚舉數據發佈到控制器? –

+0

@Karthik不,只有我只是通過兩個日期。 –

+0

問題是當你從控制器獲取值來查看? –

回答

-1

您在本例中的視圖模型是IEnumurable。它只包含在枚舉器上定義的方法。你有兩個選擇。

選項之一:定義一個視圖模型,如以下

public class MyViewModel { 
    public CRM_Doctor_Request ReportRequests { get; set;} 
    public DateTime Date_Created { get; set;} 
    public DateTime Date_Modified { get; set;} 
} 

現在你可以使用這個視圖模型將數據傳遞到視圖。

第二個選項:使用ViewBag將日期傳遞給視圖。在你的控制器

ViewBag.Date_Created = dateCreated; 
ViewBag.Date_Modified = dateModified; 

現在,這些值現在可以從查看

個人而言,我會去選擇一個訪問,由於其清潔

+0

雖然他正在枚舉集合,但這不適用 – glosrob

1

嘗試這樣的:

<% foreach (var date in Model) { %> 
     <%= Html.HiddenFor(date=>date.Date_Created) %> 
     <%= Html.HiddenFor(date=>date.Date_Modified) %> 
    <% } %> 

希望能幫助到你。

相關問題