使用Ajax表单的MVC 4 Razor更新foreach循环

Kel*_*nis 9 ajax asp.net-mvc jquery razor asp.net-mvc-4

从哪里开始...我可以在互联网上找到类似的东西,但是它们似乎永远不会以我想要做某事的具体方式工作.我尝试了有部分观点和没有部分观点但收效甚微.

快速概述:我有一个带有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>
}
Run Code Online (Sandbox Code Playgroud)

现在这是我的控制器.我在Controller中正确地得到了结果,只是没有更新代码块:

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);
        }
Run Code Online (Sandbox Code Playgroud)

谢谢.

Str*_*ior 14

@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>
Run Code Online (Sandbox Code Playgroud)

TeacherList.cshtml

@model IEnumerable<Teacher>
@foreach(var teacher in Model)
{
   ...
}
Run Code Online (Sandbox Code Playgroud)

控制器动作:

    [HttpPost]
    public ActionResult FilterTeachers(String teacherName, String instrumentID, String cityID)
    {
        Model.Teachers = TeacherService.GetTeacherList(teacherName, instrumentID, cityID);
        return PartialView("TeacherList", Model.Teachers);
    }
Run Code Online (Sandbox Code Playgroud)

  • `TeacherList.cshtml`是局部视图不是吗?在控制器中它应该是`返回PartialView("TeacherList",Model.Teachers);`? (2认同)