• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

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.

 Site SS
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>
0
wademi
Asked:
wademi
  • 2
1 Solution
 
strickddCommented:
You need to surround your search controls with a form:

<% Html.BeginForm(); %>
 <label for="Search server">Search:</label>
<%= Html.TextBox("Search") %>
   <button class="okButton" type="submit" value="Search" >Search</button>
<% Html.EndForm(); %>

Open in new window

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

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now