使用存储库在MVC中搜索 [英] Searching in MVC using repository

查看:88
本文介绍了使用存储库在MVC中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我被我自己的代码卡住了,因为他们搜索团队的所有玩家。我不知道如何解除。

我创建模型为Team.cs:

 公开 团队
{
public int TeamID { get ; set ; }
[必需]
[显示(名称= 团队)]
public string TeamName { get ; set ; }
public string 详细信息{ get ; set ; }
}





和Player.cs



< pre lang =cs> namespace FootBall.Models
{
public < span class =code-keyword> class 播放器
{
public int PlayerID { get ; set ; }
[必需]
[显示(名称= 名称)]
public string PlayerName { get ; set ; }
[显示(名称= Gersy No)]
public string PlayerPosition { get ; set ; }
[显示(名称= 详细信息)]
public string PlayerDetails { get ; set ; }
public int TeamID {获得; set ; }
}
}





我希望得到所有球员的球队名单而我我发送团队ID进行搜索,这将由玩家的团队ID匹配,因为我提供团队ID以及玩家的所有信息。



我写了一个通过TeamRepository搜索它的代码。



这里我们在TeamController.cs中写道



  public  ActionResult搜索( int  id)
{
ViewData [ 搜索] = repo.PlayerRepository.Search(id);
return 查看(ViewData [ 搜索]);
}







和PlayerRepository.cs包含



公共玩家搜索(int id)

{

玩家玩家= context.Players.Where(i => i.TeamID == id ).FirstOrDefault();

返回玩家;



}



但是玩家并没有得到任何结果。



你能帮我提前吗?谢谢......

解决方案

以下系统是正确的





播放器播放器= context.Players.Where(i = >  i.TeamID == id ).FirstOrDefault(); 





你需要通过调试
来检查你可以使用以下



  var  pl =(来自 x   context.Players 
其中​​ TeamID == id
选择 x)FirstOrDefault();







和内联查询





  var  data = context.Database.SqlQuery< player> 
从玩家中选择*
,其中TeamID =
+ id + < span class =code-string> ); < / player >


Actually I got stuck by my own code for searching " all players for a team". I don't know the way to relieve.
I created to model as Team.cs:

public class Team
   {
       public int TeamID { get; set; }
       [Required]
       [Display(Name = "Team")]
       public string TeamName { get; set; }
       public string Details { get; set; }
   }



And Player.cs

namespace FootBall.Models
{
    public class Player
    {
        public int PlayerID { get; set; }
        [Required]
        [Display(Name = "Name")]
        public string PlayerName { get; set; }
        [Display(Name = "Gersy No")]
        public string PlayerPosition { get; set; }
        [Display(Name = "Details")]
        public string PlayerDetails { get; set; }
        public int TeamID { get; set; }
    }
}



I want to get all Player's list of a team while I am sending the team ID to search which will be matched by the the player's team Id, As I provide team ID with the all information of a player.

I wrote a code to search it through TeamRepository.

Here we wrote in TeamController.cs

public ActionResult Search(int id)
        {
            ViewData["Search"] = repo.PlayerRepository.Search(id);
            return View(ViewData["Search"]);
        }




and PlayerRepository.cs contains

public Player Search(int id)
{
Player player = context.Players.Where(i => i.TeamID == id).FirstOrDefault();
return player;

}

But "player" didn't get nothing.

Would you please help me to ahead? Thanks......

解决方案

following systex is correct 



Player player = context.Players.Where(i => i.TeamID == id).FirstOrDefault();



You need to check by debugging
you can use following also


var pl = (from x in context.Players
                           where TeamID == id 
                           select x)FirstOrDefault();




and by the inline query



var data = context.Database.SqlQuery<player>
                                   ("select * 
                                   from Players where TeamID=" + id + "", "");</player>


这篇关于使用存储库在MVC中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆