我正在学习ASP.NET MVC,我在练习项目(MVC Music Store)中遇到了问题。我已经部分地寻找了一位艺术家。我希望局部视图不带任何参数并自行工作。
右半部分的局部视图
我有艺术家搜索局部视图的视图特定模型。该模型如下:
public class ArtistSearch
{
public string SearchString { get; set; }
public List<Artist> SearchResult { get; set; }
public ArtistSearch()
{
SearchResult=new List<Artist>();
}
}
控制器代码如下:
public ActionResult Search(string query)
{
ArtistSearch asResult = new ArtistSearch();
if (query != null)
{
var temp = from a in db.Artists
where a.Name.Contains(query)
select a;
asResult.SearchResult = temp.ToList();
asResult.SearchString = query;
}
return PartialView(asResult);
}
部分视图如下:
@model MvcMusicStore.Models.ArtistSearch
<div class="big-search-box">
<form action="@Url.Action("Search","Artist")" method="post" role="form">
<div class="input-group">
@Html.TextBox("query", @Model.SearchString, new { @class = "form-control nrb input-lg", placeholder = "Input your search query..." })
<div class="input-group-btn">
<input type="submit" name="Send" value="Search" class="btn btn-primary btn-iconed btn-lg" />
</div>
</div>
</form>
</div>
<div class="big-search-result-info clearfix">
<div class="pull-left">Showing results for <strong>@Model.SearchString</strong>.</div>
<div class="pull-right"><strong>@Model.SearchResult.Count</strong> artist(s) found.</div>
</div>
<table>
@foreach (var item in @Model.SearchResult)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
<a href="@item.Id" >
<img src=@item.PhotoURL alt=@item.Name style="width:100px;height:70px;">
</a>
</td>
</tr>
}
</table>
我希望将此部分视图放在网站的任何位置。假设我在艺术家/索引控制器视图页面上放置它(使用RenderAction)。
我想要实现的简单功能是,当我点击搜索时,它应该使用搜索结果自行更新部分视图。现在它将我转移到艺术家/搜索页面。
感谢您的耐心等待。
答案 0 :(得分:0)
尝试使用Ajax.BeginForm,下面是一个例子:
<强>动作强>
public ActionResult Search(string query)
{
ArtistSearch asResult = new ArtistSearch();
if (query != null)
{
var temp = from a in db.Artists
where a.Name.Contains(query)
select a;
asResult.SearchResult = temp.ToList();
asResult.SearchString = query;
}
return PartialView("MyParitalView",asResult);
}
查看强>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
@using (Ajax.BeginForm("Search", "Home", new AjaxOptions { UpdateTargetId = "result" }))
{
<div class="input-group">
@Html.TextBox("query", @Model.SearchString, new { @class = "form-control nrb input-lg", placeholder = "Input your search query..." })
<div class="input-group-btn">
<input type="submit" name="Send" value="Search" class="btn btn-primary btn-iconed btn-lg" />
</div>
</div>
}
<div id="result"></div>
Parital View:MyParitalView
@model MvcMusicStore.Models.ArtistSearch
<div class="big-search-result-info clearfix">
<div class="pull-left">Showing results for <strong>@Model.SearchString</strong>.</div>
<div class="pull-right"><strong>@Model.SearchResult.Count</strong> artist(s) found.</div>
</div>
<table>
@foreach (var item in @Model.SearchResult)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
<a href="@item.Id">
<img src=@item.PhotoURL alt=@item.Name style="width:100px;height:70px;">
</a>
</td>
</tr>
}
</table>