どこから始めればよいのか...インターネット上で方法について同様のことを見つけることができますが、何かをしたいという私の特定の方法ではうまくいかないようです。私は部分的なビューの有無にかかわらず試しましたが、ほとんど成功しませんでした。
簡単な要約:Ajaxフォームで強く型付けされたビューがあります。フォームの下に、コードブロックを繰り返すforeachループがあります。フォームの選択肢(フィルター)からコードブロックを更新できるようにする必要があります。
これが私の見解である「FindATeacher.cshtml」です(多くの異なるアイデアを試した後)。
@model Teachers.Models.OmniModel
@{
ViewBag.Title = "FindATeacher";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Find a Teacher</h2>
@using (Ajax.BeginForm("FilterTeachers", "Home", new AjaxOptions { HttpMethod = "Post", OnSuccess = "onSuccess" }))
{
<div id="ContentFilter">
<div class="filterLabels">
<p>Search by Name</p>
<p>Filter By Instrument</p>
<p>Filter By City</p>
</div>
<div class="filterObjects">
<p>
<input type="text" id="nameTXT" />
<button type="submit" id="findButton">find</button>
</p>
<p>@Html.DropDownList("InstrumentID", (SelectList)Model.Instruments, "-- Select an Instrument --", new { id = "instrumentDD" })</p>
<p>@Html.DropDownList("CityID", (SelectList)Model.Cities, "-- Select a City --", new { id = "cityDD" })</p>
</div>
</div>
}
<hr />
@foreach (var r in Model.Teachers)
{
<div id="ContentResults">
<div id="avatar">
<img src="i" />
</div>
<div id="demographics">
<h6>@r.Teacher</h6>
<strong>@r.StudioName</strong>
<p><a href="#">@r.URL</a></p>
<p>@r.StreetAddress</p>
<p>@r.City, @r.AddressStateID @r.Zip</p>
<p>@r.Phone</p>
<p>@r.EmailAddress</p>
</div>
<div id="studioDetails">
<p><strong>Instrument(s) Taught</strong></p>
<p>
@{
var instrumentString = r.Instruments.Aggregate("", (a, b) => a + b.Instrument + ", ");
if (instrumentString.Length != 0)
{
instrumentString = instrumentString.Remove(instrumentString.LastIndexOf(","));
}
}
@instrumentString
</p>
<br />
@if (r.Information != "" && r.Information != null)
{
<p><strong>Information</strong></p>
<p>@r.Information</p>
}
</div>
</div>
}
これが私のコントローラーです。コードブロックを更新しないだけで、コントローラーで結果が正しく返されます。
public ActionResult FindATeacher()
{
Model.Instruments = new SelectList(TeacherService.GetInstrumentList(0),"InstrumentID","Instrument");
Model.Cities = new SelectList(TeacherService.GetCityList(),"CityID","City");
Model.Teachers = TeacherService.GetTeacherList("", 0);
return View(Model);
}
[HttpPost]
public JsonResult FilterTeachers(String teacherName, String instrumentID, String cityID)
{
Model.Teachers = TeacherService.GetTeacherList("John", 0, 0);
return Json(Model.Teachers);
}
ありがとう。
@VishalVaishyaは正しいアイデアを提示しますが、カスタムjavascriptコードを含まないより簡単な方法があります。AjaxOptionsにはUpdateTargetIdプロパティがあり、AJAXツールキットは、指定されたターゲットが必要であることを意味すると解釈します。コントローラから返送された結果で更新されます。
FindATeacher.cshtml:
@using (Ajax.BeginForm("FilterTeachers", "Home", new AjaxOptions {
HttpMethod = "Post", UpdateTargetId = "TeacherList" }))
{
...
}
<hr />
<div id="TeacherList">
@Partial("TeacherList", Model.Teachers)
</div>
TeacherList.cshtml
@model IEnumerable<Teacher>
@foreach(var teacher in Model)
{
...
}
コントローラのアクション:
[HttpPost]
public ActionResult FilterTeachers(String teacherName, String instrumentID, String cityID)
{
Model.Teachers = TeacherService.GetTeacherList(teacherName, instrumentID, cityID);
return PartialView("TeacherList", Model.Teachers);
}
次の方法を試すことができます。
Foreachループを別の部分ビューに分割します。そして、フィルター/クリックイベントの部分ビューをロードし、フィルターされたパラメーターをコントローラーアクションに渡します。
JS変更イベントコードは次のようになります。
var teacherName = ''; //get your selected teachername
var instrumentID = ''; //get your selected instrumentid
var cityID = ''; //get your selected city id
var url = '@Url.Action("FilterTeachers", "ControllerName", new { teacherName = "teacher-Name", instrumentID="instrument-ID", cityID="city-ID" })';
url = url.replace("teacher-Name", teacherName).replace("instrument-ID", instrumentID).replace("city-ID", cityID);
$('#result').load(url);