Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8172359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:45:50+00:00 2026-06-06T21:45:50+00:00

following on from an earlier post: Adding a search form using a DateTime template

  • 0

following on from an earlier post: Adding a search form using a DateTime template

I also tried following the Search example here: 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-application

…but that doesn’t make use of a ViewModel as suggested in the answer to my previous post.

I am trying to help with a search from, which will take two dates (from/to), based on a ViewModel of:

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

My Views/Search/Index.cshtml is:

@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>
} 

The Get: /Search/Index controller is:

    //
    // GET: /Search/

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

So far so good – my view shows the textboxes with the dates defaulting to today (using a DateTime Helper). When I click Search, the code goes to the SearchController // Post: / Search as follows:

    //
    // 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());

        }

Where I’m at now unsure is the last couple of lines of the Post controller – how do I return the list of rooms in IQueryable rooms – and show the list of rooms on the screen? Do I redirect to another View (if so, how do I pass the list of rooms to that view)? If I just try and run the code above, I get the error:

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'.

Am I trying to mix apples and pears – is there anyway of showing the list of rooms under the From and To search box (ViewModel)?

Thank you,

Mark

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:45:51+00:00Added an answer on June 6, 2026 at 9:45 pm

    You could return a different view that will contain the search results:

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

    And inside Result.cshtml:

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

    If you want to stay no the same view, modify your view model so that it contains a third property which will represent the search results (a collection of rooms) that you will populate inside your POST controller action and redisplay the same view:

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

    and then inside the view you could have a section to display the results:

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

    But in both cases I guess that the Room object is a domain model. Good practice dictates that you should define a view model in which you include only the information that your view needs:

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

    and then have an IEnumerable<RoomViewModel> Rooms { get; set; } property to work with. You could still fetch your IQueryable<Room> domain model in the controller action from your DAL layer, but before passing it to the view make sure to perform the conversion to the view model. It is where AutoMapper could come in handy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from a post I made earlier, I am making progress with what
Following on from my earlier question about creating Address Books (many thanks Peter!), I
Following up from an earlier question on extracting the n'th regex match , I
Following on from my earlier question , I am still having issues with loading
I'm getting the following from a third party library (one example): @%SystemRoot%\SomePath\SomeFile.Dll,-203 I know
In my earlier post there was a problem with JSF charset handling, but also
Following on from an earlier question I'm trying to get multiple (widgets or in
Following on from my earlier question, I have decided to have a go at
I'm looping through all the songs from an iPhone's music library using the following
I'm following up on my earlier question: Mono.Cecil: call base class' method from other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.