开发者

asp mvc how i can search record from data base

开发者 https://www.devze.com 2023-02-17 02:19 出处:网络
I want search record from the sql server data base but i can\'t search the record my index formcode is as follows:

I want search record from the sql server data base but i can't search the record my index form code is as follows:

<%using (Html.BeginForm("Submit","Candidates",FormMethod.Post)){ %>
  Enter keyword to search
  <div id="searchBox">
   <input type= "text" name="FirstName" 
                id="FirstName" maxlength="70" onfocus="this.value=''" value=""/><br />

   <input id="search" type="submit" value="Search" />
  </div>
<% } %>

mysql Repository code

public class SqlCandidatesRepository:ICandidatesRepository
 {
   private Table<Candidate> candidatesTable;
   public SqlCandidatesRepository(string connectionString)
   {
     candidatesTable = (new DataContext(connectionString)).GetTable<Candidate>(); 
   }
   public IQueryable<Candidate> SearchCandidate(string key)
   {
     var candidate = (from p in 
                         candidatesTable 
                           where p.FirstName == key
                          开发者_如何学JAVA  select p);

     return candidate;
     }  
 }

Controller code

  public class CandidatesController : Controller
     {
    //
    // GET: /Products/
    private ICandidatesRepository candidatesRepository;
    public CandidatesController(ICandidatesRepository candidatesRepository)
    {


        this.candidatesRepository = candidatesRepository;
    }

public ActionResult CandidateSearch(string name)
 {
  var candidate = candidatesRepository.SearchCandidate(name);
  return View(candidate.ToList());
 }
       }    

Exception trace :

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /Home/Submit**

Please help and give me sample code thanks.


Your markup is creating a form which will post to the action "Submit" on the "Candidates" controller.

I don't know what your controller is called but your action is called "CandidateSearch".

Change your markup to: (assuming that your page was rendered by the same controller that it is posting back to).

 <% using (Html.BeginForm("CandidateSearch")){ %>

OR

  <% using (Html.BeginForm("CandidateSearch","TheControllerName")){ %>
0

精彩评论

暂无评论...
验证码 换一张
取 消