• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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