通过字符串值MVC 5的搜索方法 [英] Search method by string value MVC 5
问题描述
作为我项目的一部分,我需要找到一种通过字符串搜索对象并在视图中显示结果的方法.感谢您的帮助.在我的 MainMedia
视图中,我有一个侧面部分,是我手动将字符串值传递给 SearchMedia
方法:
A a part of my project i need to find a way to search my object by a string and show a result in view. Your help is appreciated.
in my MainMedia
view i have a sidesection were i manually pass a string value to a SearchMedia
method:
@section SideBar{
<ul>
<li> @Html.ActionLink("Astronomy", "SearchMedia", new {searchString = "Astronomy" })</li>
<li> @Html.ActionLink("World", "SearchMedia", new { searchString = "World" })</li>
<li> @Html.ActionLink("Movies", "SearchMedia", new { searchString = "Movies" })</li>
</ul>
}
此方法应检查每个对象是否包含 TagsEnum
字符串,然后在 SearchMedia
视图中显示一个对象.
This method should check every object if TagsEnum
string and then display an object in SearchMedia
view.
这是我的媒体课
public class Media
{
public int Id { get; set; }
public string title { get; set; }
public string description { get; set; }
public string body { get; set; }
public string ImagePath { get; set; }
public string VideoLink { get; set; }
public string Source { get; set; }
public string tags { get; set; }
public TagsEnum TagsEnum { get; set; }
}
TagsEnum类
public enum TagsEnum
{
[Display(Name = "Astronomy and space")]
Astronomy,
[Display(Name = "World around us")]
World,
[Display(Name = "Movies, video")]
Movies
}
和最终 MediaMainController
SearchMedia
方法
public ActionResult SearchMedia(string searchString)
{
db.Medias.Where(i => i.TagsEnum.ToString() == searchString);
return View(db.Medias.OrderBy(it => it.Title));
}
据我了解, .Where()
应该找到一个匹配项并返回一个对象,但是它不起作用.我如何解决呢?也许还有其他方法可以做到?谢谢
As i understand .Where()
should find a match and return an object, however it is not working. How i can sort it out? Perhaps there are other ways to do it? Thank you
更新我已经这样更改了:
Update I have changed it like this:
var result = db.Medias.Where(TagsEnum => TagsEnum.ToString() == searchString);
return View(result.OrderBy(it => it.title));
但我仍然看不到要按搜索排序的结果
but i still dont see the results to be sorted by search
更新2 我有一个用于创建对象列表的 MediaViewModel
类,它看起来像这样:
Update 2
I have a class MediaViewModel
which i use to create a list of objects, it looks like this:
public class MediaViewModel
{
public List<Media> media { get; set; }
public List<Video> video { get; set; }
}
如果我设置 SearchMedia
这样的视图
@model PhClub.Models.MediaViewModel
@foreach (var b in Model.media)
{}
我遇到错误:传递到字典中的模型项的类型为System.Linq.Enumerable + WhereListIterator 1 [PhClub.Models.Media],但是此字典需要模型项的类型为PhClub.Models.MediaViewModel.如果我将其设置为
i'm getting an error: The model item passed into the dictionary is of type System.Linq.Enumerable+WhereListIterator 1[PhClub.Models.Media], but this dictionary requires a model item of type PhClub.Models.MediaViewModel. If i set it up as
`@model IEnumerable<PhClub.Models.Media>
@foreach (var b in Model)
{}`
这是说媒体"类型的值不能转换为字符串.我认为我需要更改 SearchMedia
方法以支持 MediaView
类,但是我还没有弄清楚.感谢帮助
it is saying Values of type 'Media' can not be converted to string.
I think i need to change SearchMedia
method to support MediaView
class, but i didnt figure it out yet. Help is appreciated
推荐答案
您应将其分配给变量并使用它,
You should assign it to a variable and use it,
var result = db.Medias.Where(i => i.TagsEnum.ToString() == searchString);
return View(result.OrderBy(it => it.Title));
这篇关于通过字符串值MVC 5的搜索方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!