xoxo_tw
xoxo_tw

Reputation: 269

Search using Linq from the view

I am trying to make a TextBox in my view, that i can search from using Linq statements.

When I try to implement the search from the View, the compiler cant find my prods List.

When I try implement it from the Controller, it cant find my searchBox, which in both case I understand , but I dont understand how to fix the problem. or if its best to implement the search in the Controller or in the View ??

I have made comment in the code, where I tried my search function.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Uppgift_1.Models;

namespace Uppgift_1.Controllers
{
    public class ProductController : Controller
    {

        public ActionResult Index()
        {
            List<MyProduct> prods = new List<MyProduct>()
            {
                .....add Products......
            };

// Linq search
            var searchResults = (from s in prods where(s.ProductName == searchBox || s.ProductId == searchBox) select s).ToList();

            return View(prods);
        }

    }
}





@model IEnumerable<Uppgift_1.Models.MyProduct>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>

        @foreach (var x in Model)
        { 
            <div>
                <h3>@x.ProductName</h3>
                Article# @x.ProductId
                <h4>@x.PriceSell.ToString("c")</h4>
            </div><hr />        
        }

      <div>
        <input type="text" name="searchBox" />

             // Link search
            @{
            var searchResults = (from s in prods where (s.ProductName == searchBox ||     s.ProductId == searchBox)
                                 select s).ToList();

            }
      </div>
</body>
</html>

Upvotes: 0

Views: 1480

Answers (3)

Jedediah
Jedediah

Reputation: 1944

A few things here.

prods is undefined in your Razor view because variable names that you pass into your view from your controller aren't the same. So even though you're using return View(prods) in your controller, the variable prods won't exist in your view. The variable Model is actually what you're expecting the prods object to be.

Even if prods were defined in your view, your Linq query still wouldn't work, because you can't read an input value directly in Razor. For example:

<input type="text" name="searchQuery" />
@{
    var query = searchQuery;
    //searchModel isn't a variable, it's just the name you gave to your HTML input.
}

In order to read that value, you need to send a request back to your controller. You need to do that by submitting a form, or sending an AJAX request to your controller. Once your controller gets that request, that is where you'd have your linq query, or maybe in your model, depending on your program's architecture. A really basic example would look something like this:

<form method="post" action="/yourcontroller/search">
    <input type="text" name="searchQuery" />
    <input type="submit" value="Search" />
</form>

And then your controller action would be something like this:

public ActionResult Search(string searchQuery)
{
    var results = (from x in YourCollection where x.Field == searchQuery select x).ToList();
    return View(results);
}

Upvotes: 1

asymptoticFault
asymptoticFault

Reputation: 4529

You are passing your prods list as the Model to the view so you do have it just not under the same variable name. Add this line to your view under Layout = null;:

var prods = Model;

MVC is not like Web Forms where you can see controls from your code behind and see code behind variables from your markup (aspx). There is clear separation between the Controllers and the Views, you have to explicitly pass information between them. Data is passed from a Controller to a View via the Model or more general ViewBag and ViewData collections. Data is passed from the Views to the Controllers via Request Data which can come from several sources; route values, querystring or form values.

As to where to perform the search, the Views should only contain view logic which would be anything relevant to assembling the view. I believe the search functionality would most decidedly fall outside that category so it should either be implemented in the controller or, according to strict separation of concerns, be implemented in a repository/data access layer.

Upvotes: 1

devdigital
devdigital

Reputation: 34349

You would normally either send all of the required data to the client (e.g. as JSON stored on the client response) and then perform the filtering client side in JavaScript.

Alternatively, if you want to perform the search on the server side, then you would either post the search term to the server, process, and return the view, or perform the search asynchronously with AJAX, perhaps using a library such as jQuery to make this easier.

Upvotes: 1

Related Questions