您好我一直在做MVC而且陷入了一个超现实的问题。
会有几个城市的图像。点击这些图像,必须检索特定城市的学生并将其显示在不同的页面中。
正在点击时使用jqueryAjax检索城市,发送到控制器,从数据库检索并转发到SearchResults视图,但不在视图中呈现(页面也不会重定向到SearchResults SearchByTerritory)。
请帮助我,我已经搜索了很多但仍未找到任何答案。
我创建了2个页面(视图)SearchByTerritory.cshtml - 用于显示图像和SearchResults.cshtml - 分别用于显示这些视图的结果和控制器。
以下是我的代码
SearchByTerritory查看:
@{
ViewBag.Title = "Search By Territory";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.usaMap').click(function () {
var address = $(this).attr("id");
var url = "/Student/SearchByTerritory";
var address = $(this).attr("id");
$.post(url, { address: address }, function (data) {
});
})
});
</script>
<h2>SearchByTerritory</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<img src="~/Images/test1.gif" class="usaMap" id="Noida" alt="Noida" title="Noida" />
<img src="~/Images/test2.png" class="usaMap" id="Gurgaon" alt="Gurgaon" title="Gurgaon" />
<img src="~/Images/test3.png" class="usaMap" id="Mangalore" alt="Mangalore" title="Mangalore" />
//@Html.Raw(@Ajax.ActionLink("[replacetext]", "SearchByTerritory", new { address = "Noida" }, new AjaxOptions { HttpMethod = "Post" }).ToHtmlString().Replace("[replacetext]", "<img src=\"/Images/USA.jpg\" />"));
}
<p>
@Html.ActionLink("See all Users", "Index")
</p>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
控制器:
[HttpGet]
public ActionResult SearchByTerritory()
{
return View();
}
[HttpPost]
public ActionResult SearchByTerritory(string address)
{
var students = from s in db.Students
select s;
students = students.Where(s => s.Address.Contains(address));
if (students != null && students.Count() > 0)
{
ViewBag.dbs = students.ToList();
//return RedirectToAction("SearchResults", ViewBag.dbs);
return View("SearchResults", students);
}
return View("SearchEmpty");
}
SearchResults查看:
<h2>Search Results</h2>
<fieldset>
<legend>Student</legend>
@foreach (NEWDigitalYearBookRK.Models.Student s in ViewBag.dbs as List<NEWDigitalYearBookRK.Models.Student>)
{
<div class="display-label">
First Name
</div>
<div class="display-field">
@s.FirstName
</div>
<div class="display-label">
Last Name
</div>
<div class="display-field">
@s.LastName
</div>
<div class="display-label">
Age
</div>
<div class="display-field">
@s.Age
</div>
<div class="display-label">
Gender
</div>
<div class="display-field">
@s.Gender
</div>
<div class="display-label">
Batch
</div>
<div class="display-field">
@s.Batch
</div>
<div class="display-label">
Address
</div>
<div class="display-field">
@s.Address
</div>
<div class="display-label">
Class
</div>
<div class="display-field">
@s.Class
</div>
<div class="display-label">
School
</div>
<div class="display-field">
@s.School
</div>
<div class="display-label">
Domicile
</div>
<div class="display-field">
@s.Domicile
</div>
}
答案 0 :(得分:0)
好。我可以在这里给你一些提示,但你必须调试并使它工作。 您正在使用SearchByTerritory作为&#34;帖子&#34; ajax调用的方法,它返回一个普通的视图!这不起作用。你有几个选择,但让我告诉你更简单的选择:
以这种方式在searchbyterritory视图中编辑代码(使用html帮助器呈现网址的更好方法)
&lt; a href =&#34; / searchresult / noida&#34;&gt;&lt; img src =&#34;〜/ Images / test1.gif&#34;类=&#34; usaMap&#34; ID =&#34;诺依&#34; ALT =&#34;诺依&#34;标题=&#34;诺依&#34; /&GT;&LT; / A&GT;
评论您拥有的js代码。
您可以稍后增强代码以使用ajax调用,甚至可以将结果显示在与单个页面应用程序相同的页面中。但是你必须了解它是如何工作的。