C# 在mvc razor页面上搜索列表
我有以下型号:C# 在mvc razor页面上搜索列表,c#,model-view-controller,razor,asp.net-core-mvc,C#,Model View Controller,Razor,Asp.net Core Mvc,我有以下型号: public class Photo { public string Id { get; set; } public List<string> Hashtag { get; set; } public string PhotoUrl { get; set; } public string SearchString { get; set; } } 公共课照片 { 公共字符串Id{get;
public class Photo
{
public string Id { get; set; }
public List<string> Hashtag { get; set; }
public string PhotoUrl { get; set; }
public string SearchString { get; set; }
}
公共课照片
{
公共字符串Id{get;set;}
公共列表标签{get;set;}
公共字符串PhotoUrl{get;set;}
公共字符串搜索字符串{get;set;}
}
我的观点是这样的:
@model PhotoSearch.Models.Photo[]
<div class="text-center">
<p>Search.</p>
<form>
<p>
Title: <input type="text" asp-for="SearchString" />
<input type="submit" value="Filter" />
</p>
</form>
</div>
<h2>Photos</h2>
@{
foreach (var photo in @Model)
{
<table>
<tr>
<th>URL</th>
<th>Hashtags</th>
</tr>
<tr>
<th>@photo.PhotoUrl %</th>
<th>@String.Join(", ", @photo.Hashtag.ToArray());</th>
</tr>
</table>
}
}
@model PhotoSearch.Models.Photo[]
搜索
标题:
照片
@{
foreach(模型中的var照片)
{
统一资源定位地址
标签
@photo.PhotoUrl%
@Join(“,”,@photo.Hashtag.ToArray());
}
}
我从API中获取一个最初为Json格式的照片列表,然后将其反序列化到上述对象中:
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.GetAsync(baseUrl))
{
using (HttpContent content = response.Content)
{
string data = await content.ReadAsStringAsync();
if (data != null)
{
var photoList = JsonConvert.DeserializeObject<Photo[]>(data);
if (!String.IsNullOrEmpty(searchString))
{
var filteredList = photoList.Where(v => v.Hashtags.Contains(searchString));
return View(filteredList);
}
return View(photoList);
}
}
}
}
使用(HttpClient=new HttpClient())
{
使用(httpresponsemessageresponse=wait client.GetAsync(baseUrl))
{
使用(HttpContent=response.content)
{
字符串数据=等待内容。ReadAsStringAsync();
如果(数据!=null)
{
var photoList=JsonConvert.DeserializeObject(数据);
如果(!String.IsNullOrEmpty(searchString))
{
var filteredList=photoList.Where(v=>v.Hashtags.Contains(searchString));
返回视图(filteredList);
}
返回视图(照片列表);
}
}
}
}
我在顶部有一个搜索表单,因为我希望用户能够输入一个hashtag列表,它将过滤结果,就像填充了搜索字符串一样,一个不同的视图将只显示过滤后的照片。我的问题是SearchString是Photo的属性,而不是视图中正在使用的Photo[]。如果我使用foreach,搜索框只会出现几次。有没有更简单的方法可以过滤照片?提供一个小演示: 视图:
@{
ViewBag.Title=“主页”;
}
@模型WebApplication2.Models.Photo[]
搜索
@使用(Html.BeginForm())
{
标题:@Html.TextBox(“搜索字符串”)
}
照片
@{
foreach(模型中的var照片)
{
统一资源定位地址
标签
@photo.PhotoUrl%
@Join(“,”,@photo.Hashtag.ToArray());
}
}
控制器:
public class HomeController : Controller
{
public List<Photo> photoList = new List<Photo>();
public ActionResult Index(string searchString)
{
photoList.Add(new Photo() { Hashtag = new List<string>() { "test", "test2" }, Id = "1", PhotoUrl = "http://localhost/1.jpg", SearchString = "test1" });
photoList.Add(new Photo() { Hashtag = new List<string>() { "Jhon", "Flowers" }, Id = "1", PhotoUrl = "http://localhost/2.jpg", SearchString = "flower" });
if (!String.IsNullOrEmpty(searchString))
{
photoList = photoList.Where(x => x.SearchString.Contains(searchString)).ToList();
}
return View(photoList.ToArray());
}
}
公共类HomeController:控制器
{
公共列表照片列表=新列表();
公共操作结果索引(字符串搜索字符串)
{
添加(newphoto(){Hashtag=newlist(){“test”,“test2”},Id=“1”,PhotoUrl=”http://localhost/1.jpg,SearchString=“test1”});
添加(newphoto(){Hashtag=newlist(){“Jhon”,“Flowers”},Id=“1”,PhotoUrl=”http://localhost/2.jpg,SearchString=“flower”});
如果(!String.IsNullOrEmpty(searchString))
{
photoList=photoList.Where(x=>x.SearchString.Contains(SearchString)).ToList();
}
返回视图(photoList.ToArray());
}
}
此处不需要使用asp。您只需将输入名称设置为SearchString
,因为模型绑定基于名称
<input type="text" id="SearchString" name="SearchString" />
<input type="text" id="SearchString" name="SearchString" />