Link to home
Start Free TrialLog in
Avatar of wademi
wademi

asked on

C# MVC help:LINQ to SQL

I am learning MVC using C# and need help with linq to sql

I have a controller that lookslike this:

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

namespace VTask.Controllers
{
    public class SitesController : Controller
    {
        //
        // GET: /Sites/

        SiteRepository siterepository = new SiteRepository();

        public ActionResult Index()
        {
           
            var mysites = siterepository.FindAllSites().ToList();
            return View("Index", mysites);
           
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Search(string Search)
        {
            var matchingsites = siterepository.Findmatchingsites(Search).ToList();
            return View("Index", matchingsites);
        }

    }
}

My Model has a siterepository.cs that looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VTask.Models
{
    public class SiteRepository
    {
        private SitesDataContext dbx = new SitesDataContext();

       
        public IQueryable<V_Site> FindAllSites()
        {
            return dbx.V_Sites;
           
        }

        public IQueryable<V_Site> Findmatchingsites(string macthcsite)
        {
            return from siteslike in dbx.V_Sites
                   where siteslike.Server.Contains(macthcsite)
                   select siteslike;
        }

       
    }
}

I would like like to be able to click on the search button in the screenshot and and get results from my database.

 User generated image
When I click on the search button(Findmatchingsites) nothing happens.

Is the LINQ to SQL syntax correct? What am I doing wrong here?

Here is a part of my Views->Sites Index.aspx

    <h2>Index</h2>
    <label for="Search server">Search:</label>
<%= Html.TextBox("Search") %>
   <button class="okButton" type="submit" value="Search" >Search</button>
ASKER CERTIFIED SOLUTION
Avatar of strickdd
strickdd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wademi
wademi

ASKER

Thanks strickdd: I tried that and it did not work.
[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(string Search)
        {
            var matchingsites = siterepository.Findmatchingsites(Search).ToList();
            return View("Index", matchingsites);
        }