1

上一篇文章的后续:使用 DateTime 模板添加搜索表单

我还尝试按照此处的搜索示例:http ://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework -in-an-asp-net-mvc-应用程序

...但这并没有像我上一篇文章的答案中所建议的那样使用 ViewModel。

我正在尝试帮助搜索,这将需要两个日期(从/到),基于以下 ViewModel:

public class SearchViewModel
{
    [Required]
    public DateTime From { get; set; }
    [Required]
    public DateTime To { get; set; }
}

我的 Views/Search/Index.cshtml 是:

@model ttp.Models.SearchViewModel
@{
    ViewBag.Title = "Search Availability";
}
<h2>Search Availability</h2>
@using (Html.BeginForm()) 
{ 
    <div class="row">
        <div class="span2">@Html.LabelFor(x => x.From)</div>
        <div class="span2">@Html.EditorFor(x => x.From)</div>
        <div class="span2">@Html.ValidationMessageFor(x => x.From)</div>
        </div>

    <div class="row">
        <div class="span2">@Html.LabelFor(x => x.To)</div>
        <div class="span2">@Html.EditorFor(x => x.To)</div>
        <div class="span2">@Html.ValidationMessageFor(x => x.To)</div>
    </div>

     <div class="row">
        <div class="span2 offset2"><button type="submit">Search</button></div>
     </div>
} 

Get: /Search/Index 控制器是:

    //
    // GET: /Search/

    public ActionResult Index()
    {
        SearchViewModel svm = new SearchViewModel();
        svm.From = DateTime.Today;
        svm.To = DateTime.Today;
        return View(svm);
    }

到目前为止一切顺利 - 我的视图显示日期默认为今天的文本框(使用 DateTime Helper)。当我单击 Search 时,代码转到 SearchController // Post: / Search,如下所示:

    //
    // Post: /Search/
    [HttpPost]
    public ActionResult Index(SearchViewModel searchViewModel)
    {
        if (!ModelState.IsValid)
        {
            // Not valid, so just return the search boxes again
            return View(searchViewModel);
        }

        // Get the From/To of the searchViewModel

        DateTime dteFrom = searchViewModel.From;
        DateTime dteto = searchViewModel.To;

        // Query the database using the posted from/to dates

        IQueryable<Room> rooms = db.Rooms.Where(r => r.Clients.Any(c => c.Departure >= dteFrom && c.Departure < dteTo));

        // This is where I'm unsure

        ViewBag.Rooms = rooms.ToList();
        return View(rooms.ToList());

        }

我现在不确定的是 Post 控制器的最后几行 - 我如何返回 IQueryable 房间中的房间列表 - 并在屏幕上显示房间列表?我是否重定向到另一个视图(如果是,我如何将房间列表传递给该视图)?如果我只是尝试运行上面的代码,我会收到错误消息:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[ttp.Models.Room]', but this dictionary requires a model item of type 'ttp.Models.SearchViewModel'.

我是否试图混合苹果和梨 - 是否有在 From 和 To 搜索框(ViewModel)下显示房间列表?

谢谢,

标记

4

2 回答 2

4

您可以返回包含搜索结果的不同视图:

IQueryable<Room> rooms = db.Rooms.Where(r => r.Clients.Any(c => c.Departure >= dteFrom && c.Departure < dteTo));
return View("Result", rooms.ToList());

在里面Result.cshtml

@model IEnumerable<Room>
... show the rooms here

如果您不想保留相同的视图,请修改您的视图模型,使其包含第三个属性,该属性将表示您将在 POST 控制器操作中填充的搜索结果(房间集合)并重新显示相同的视图:

IQueryable<Room> rooms = db.Rooms.Where(r => r.Clients.Any(c => c.Departure >= dteFrom && c.Departure < dteTo));
searchViewModel.Rooms = rooms;
return View(searchViewModel);

然后在视图中你可以有一个部分来显示结果:

@if (Model.Rooms != null)
{
    ... display the search results here
}

但在这两种情况下,我都猜想Room对象是域模型。良好的实践要求您应该定义一个视图模型,其中仅包含视图所需的信息:

public class RoomViewModel
{
    ... only properties that you need to work with on the view
}

然后有一个IEnumerable<RoomViewModel> Rooms { get; set; }属性可以使用。您仍然可以IQueryable<Room>从 DAL 层的控制器操作中获取域模型,但在将其传递给视图之前,请确保执行到视图模型的转换。这是AutoMapper可以派上用场的地方。

于 2012-07-02T12:33:26.437 回答
1

您将不同的模型返回到同一个视图,因此首先只需更改您的控制器以返回传入的searchViewModel.

ViewBag.Rooms = rooms.ToList();
return View(searchViewModel);

您还可以将房间列表返回到相同的视图,就像您对 ViewBag 所做的那样,您只是在视图中没有任何地方为列表呈现任何标记:

@model ttp.Models.SearchViewModel 
@{     
    ViewBag.Title = "Search Availability"; 
} 
<h2>Search Availability</h2> 
@using (Html.BeginForm())  
{      
    <div class="row">
        <div class="span2">@Html.LabelFor(x => x.From)</div>
        <div class="span2">@Html.EditorFor(x => x.From)</div>
        <div class="span2">@Html.ValidationMessageFor(x => x.From)</div>
    </div>
    <div class="row">
        <div class="span2">@Html.LabelFor(x => x.To)</div>
        <div class="span2">@Html.EditorFor(x => x.To)</div>
        <div class="span2">@Html.ValidationMessageFor(x => x.To)</div>
    </div>
    <div class="row">
        <div class="span2 offset2"><button type="submit">Search</button></div>
    </div> 
}
@if (ViewBag.Rooms != null)
{
    foreach (var room in ViewBag.Rooms as List<Room>)
    {
        // Build your room list markup here
    }
}
于 2012-07-02T12:41:04.087 回答