我怎样才能在mvc中搜索 [英] How can I search in mvc

查看:93
本文介绍了我怎样才能在mvc中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个玩家控制器,其中包含属性ID,姓名,年龄等。

我想按名称搜索玩家。我写了如下查询:



I have a player controller with property id, Name, age etc.
I want to search a player by name . I wrote the query as follows:

public ActionResult Search(Player player)
{
     Player name = context.Players.Single(x => x.PlayerName == player.ToString()) ; //error is here
        
     return View(name);
}



请你告诉我究竟是什么查询。



谢谢


Would you please tell me what would be the query exactly.

Thanks

推荐答案

1.您忘了提供有关错误的详细信息;



2.如果您想搜索按照你说的名称,你应该改变你的代码:

1.You forgot to give details about the error;

2.If you want to search by name as you said, you should change your code like this:
public ActionResult Search(Player player)
{
string playerName = palyer.PlayerName; //Should be the player name that come from UI!
Player player= context.Players.FirstOrDefault(x => x.PlayerName == playerName ); 
//Note that the "player" result could be null, so you should manage also this case in your View or here!
return View(player);
}


这篇关于我怎样才能在mvc中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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