c# - 返回复杂类型以使用 AJAX 查看

标签 c# javascript jquery ajax asp.net-mvc

我正在尝试做什么。

我有一个由 EF 从存储过程生成的复杂类型。它返回 GetAvailableRooms_Results 列表。我希望用户选择 2 个日期,然后返回存储过程返回的可用房间(复杂类型)列表。

我不是 100% 确定我是否需要返回一个复杂类型,我愿意使用 Ajax.Begin Form、Ajax.Action Link 或明显的 ajax 和 jquery....我已经尝试过它们全部都失败了,每次都因为不同的原因失败。

我无法找到使用 Ajax 返回复杂类型的示例。此外,我通常很难使用 JavaScript 而不是发布问题,但是最后一个屏幕截图让我在这里提出问题......因为我不认为它与 JavaScript 相关。

我的代码

存储过程 - GetAvailableRooms

declare @arrive as datetime, @depart as datetime
set @arrive = '2013/11/29'
set @depart = '2013/12/01'

SELECT  Room.Name, Format(Reservation.StartDate,'D') AS 'Date', 
        (Room.Capacity) - (Count(Reservation.StartDate)) AS Available, 
        Count(RoomReservation.RoomId) AS 'Reservations', Room.Gender 
FROM    Room
        FULL JOIN RoomReservation 
        ON RoomReservation.RoomId = Room.RoomId 
        FULL JOIN Reservation 
        ON Reservation.ReservationId = RoomReservation.ReservationId
        WHERE StartDate BETWEEN @arrive AND @depart
GROUP BY Room.Capacity, Room.Gender, Room.Name, Reservation.StartDate, 
         Reservation.EndDate 

复杂类型(存储过程结果)GetAvailableRooms_Result

public partial class GetAvailableRooms_Result
{
    public string Name { get; set; }
    public Nullable<int> Available { get; set; }
    public Nullable<int> Reservations { get; set; }
    public Nullable<System.DateTime> Date { get; set; }
    public Nullable<bool> Gender { get; set; }
}

从 Reservation/Index.cshtml 调用存储过程 GetAvailableRooms

 @Html.ActionLink("Available Rooms", "Index",
            controllerName: "GetAvailableRooms_Result",
            routeValues: new { arrive = item.StartDate, depart = item.EndDate },
            htmlAttributes: item.StartDate)

我尝试过的

我尝试使用 Ajax.ActionLink 将复杂类型的部分 View 中的结果返回到我调用它的页面。像这样...

    @using Skimos.Models
@model List<ReservationIndexViewModel>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table table-striped">
    <tr>
        <th>
            Room Name
        </th>
        <th>
            Arrival Date
        </th>
        <th>
            Depart Date
        </th>
        <th>
            Member Id
        </th>
        <th>
            Price
        </th>
        <th>
            Rooms
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
        <td>
            @Skimos.Services.HtmlHelpers.RoomName(item.RoomId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.StartDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EndDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
            <td>
                @Html.ActionLink("Meals", "AvailableMeals",
            new { arrive = item.StartDate, depart = item.EndDate })
            </td>
            <td>
                @Html.ActionLink("Available Rooms", "Index",
                "GetAvailableRooms_Result",
            new { arrive = item.StartDate.Date, depart = item.EndDate.Date }, 
                 item.StartDate.Date)//<-- this was just required for the 
                                                 overload I also tried it as null.

            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.ReservationId }) |
                @Html.ActionLink("Details", "Details", new { id=item.ReservationId }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.ReservationId })
            </td>
        </tr>
    }
</table>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/Bootstrap-datepicker")
    @Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
    <script type="text/javascript">
        $('.datepicker ').datepicker({
            weekStart: 1,
            autoclose: true,
            todayHighlight: true,
        });
        </script>
}

我也尝试过使用

我为复杂类型创建了一个 Controller ,并使用Ajax.Beginform返回复杂类型的存储部分 View 。

使用 Skimos.Models @模型列表 @{ ViewBag.Title = "开始预订"; }

<h2>StartReservation</h2>


@using (Ajax.BeginForm("AvailableRooms", new AjaxOptions 
{ HttpMethod = "Get", UpdateTargetId = "rooms", 
OnFailure = "searchFailed", OnSuccess = "data" }))

{
    <input class="datepicker" id="StartDate" 
name="StartDate" tabindex="1" type="text" value="">

<input class="datepicker" id="EndDate" 
name="EndDate" tabindex="1" type="text" value="">

   <input type="submit" class="btn-warning" />
    <div id="rooms">
    </div>
      }


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/Bootstrap-datepicker")
    @Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
  }
<script type="text/javascript">

    $('.datepicker').datepicker({
        weekStart: 1,
        autoclose: true,
        todayHighlight: true,
    });

function searchFailed() { $("#availableRooms").html("Search failed."); }
</script>

将我推向边缘的错误

enter image description here

最后一次尝试

下面是一个 ActionResult,我尝试在部分 View 中返回复杂类型。

 public ActionResult AvailableRooms(DateTime arrive, DateTime depart)
        {
            var rooms = db.GetAvailableRooms(arrive, depart);
            return PartialView("AvailabeRooms", rooms.ToList());
        }

返回此错误:参数字典包含方法“System.Web.Mvc.ActionResultAvailableRooms(System. “Skimos.Controllers.ReservationController”中的“DateTime、System.DateTime)”。可选参数必须是引用类型、可为 null 的类型,或者声明为可选参数。

最佳答案

看起来您正在将存储过程中的日期格式化为字符串。 EF 尝试将此字符串值设置为 Date?字段 ( Nullable<System.Date> ),并且该字段不接受字符串。

导致问题的存储过程:

SELECT  Room.Name, Format(Reservation.StartDate,'D') AS 'Date', -- <-- this is formatting as a string, not a datetime.
    (Room.Capacity) - (Count(Reservation.StartDate)) AS Available, 
    Count(RoomReservation.RoomId) AS 'Reservations', Room.Gender  --...

将 GetAvailableRoomsResult 对象更改为如下所示:

public partial class GetAvailableRooms_Result
{
    public string Name { get; set; }
    public Nullable<int> Available { get; set; }
    public Nullable<int> Reservations { get; set; }
    public String Date { get; set; } // <-- change is here.
    public Nullable<bool> Gender { get; set; }
}

关于c# - 返回复杂类型以使用 AJAX 查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20415876/

相关文章:

javascript - 无法调用适配器过程

javascript - typescript + Angular

jquery - 单击页面上其他任何内容时隐藏元素

javascript - 单击其中之一后如何交换 href 属性的位置?

javascript - 在先前选择的同名单选按钮上调用函数

c# - 自动化任务

c# - C#中如何判断哪个个人证书来自硬件设备?

javascript - 我如何实现自动建议,如浏览器搜索字段(不是自动完成下拉菜单)?

c# - Visual Studio 2012 C# 中的 mysql 连接状态

c# - 异步任务同步