-1

So I'm trying to make a simple MVC system that will search through a database of books by author and/or title. I have a model called Book.cs with title and author properties. In my controller I made an ActionResult as follows:

public ActionResult Search(string theAuthor, string theTitle)
{
    if (theAuthor == null && theTitle == null)
    {
        ViewBag.title = "Search for a book by author and/or title";
    }
    else
    {
        ViewBag.title = "Results:";
    }
    List<Book> allBooks = db.Books.ToList();
    List<Book> booksFound = new List<Book>();

    foreach (Book theBook in allBooks)
    {
        if (theAuthor != null && theTitle != null)
        {
            if (theBook.author == theAuthor && theBook.title == theTitle) booksFound.Add(theBook);
        }else if (theAuthor == null)
        {
            if (theBook.title == theTitle) booksFound.Add(theBook);
        }else if (theBook == null)
        {
            if (theBook.author == theAuthor) booksFound.Add(theBook);
        }

    }

    return View("Search", booksFound);
}

Now, this returns a List of books, so I assume that in my view I have to use List<Book> model, and so I did (@model List<Book>). But the problem is how am I going to send data to the action result? I tried using

@Html.TextBoxFor (x => x.author)

But that gives the

'List<Book>' does not contain a definition for 'author' and no extension method 'author' accepting a first argument of type 'List<Book>' could be found (are you missing a using directive or an assembly reference?)

error. Now that makes sense to me because I guess I can't access model class property if my model is a list. So am I doing something wrong or I should use another way to pass data? Thanks

Craig W.
  • 17,838
  • 6
  • 49
  • 82
  • Seems like the > and < things get removed automatically on this forum. I meant @model List < book > – Aleksa Kojadinovic Mar 22 '16 at 23:48
  • everywhere you see just 'List', it's suppoed to be 'List < book > ' – Aleksa Kojadinovic Mar 22 '16 at 23:49
  • use backticks for inline code. – Daniel A. White Mar 22 '16 at 23:50
  • Show your view. If your model is `List` then your view needs a `for` loop (or custom `EditorTemplate`) to access each item in the collection. –  Mar 22 '16 at 23:59
  • The only thing my view contains is the model line I mentioned previously and the TextBoxFor line that I also mentioned. Mind explaining a little bit more? – Aleksa Kojadinovic Mar 23 '16 at 00:04
  • Are you trying to work with only one book or a collection of books? As your code stands right now, you have a collection of books, but only have one textbox. So, what is it, in English - not code, that you are trying to achieve? – Ellesedil Mar 23 '16 at 00:06
  • @AleksaKojadinovic, Refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) for an example of how to generate a form to edit items in a collection –  Mar 23 '16 at 00:11

1 Answers1

0

I didn't get your question when you said "But the problem is how am I going to send data to the action result?" but as much as I could comprehend from your question it seems you want to iterate the model which is a list of 'Book' objects inside MVC view. Controller has passed that list to your view as a model and then you want to create some UI elements based on the elements in the list. Here is how you can do it:

@for (int i = 0; i < Model.Count; i++)
{
    @Html.TextBoxFor(x=> x[i].author)
}

To answer your other concern, there is absolutely no error in the way you have passed the model (which is a list) from controller to view but you were trying to access the author property on the list itself when it is available on the list elements. Hope this helps!

RBT
  • 24,161
  • 21
  • 159
  • 240
  • You cannot use a `foreach` loop to generate form controls for collection items. –  Mar 23 '16 at 00:12
  • @StephenMuecke You were right! I fixed the problem by using a for loop instead of foreach. I've also executed the same in my development environment and seem to be working fine. I hope this helps the user now. – RBT Mar 23 '16 at 00:46
  • This [link](http://stackoverflow.com/questions/8894442/mvc-razor-view-nested-foreachs-model) helped me rectify the error in my earlier answer. – RBT Mar 23 '16 at 00:52