【问题标题】:Passing data from view to controller and controller to view将数据从视图传递到控制器和控制器以查看
【发布时间】:2013-02-01 10:56:13
【问题描述】:

我将日期从视图传递到控制器,然后我将获得从控制器到视图的这两个日期之间的记录数。由于将获取多个记录,因此我使用 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”可以找到(您是否缺少 using 指令或程序集引用?)

【问题讨论】:

  • 您是否正在向控制器发布可枚举数据?
  • @Karthik 不,我只是通过了两个日期而已。
  • 问题是当您从控制器获取值以查看时?
  • @Karthik 否,在将日期从视图发送到控制器时。看,我在代码之后也添加了我的编译错误。
  • @Karthik 我在第 1 行遇到错误。 51.

标签: c# asp.net-mvc ienumerable


【解决方案1】:

试试这样:

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

希望对你有帮助。

【讨论】:

    【解决方案2】:

    在这种情况下,您的视图模型是 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;
    

    现在可以从视图中访问这些值

    我个人会选择选项一,因为它很干净

    【讨论】:

    • 他正在枚举集合,但这不适用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多