gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:25:09 25 4
gpt4 key购买 nike

从哪里开始...我可以在互联网上找到类似的东西,但它们似乎从来不符合我想要做某事的特定方式。我尝试过使用或不使用部分 View ,但收效甚微。

快速概述:我有一个带有 Ajax 表单的强类型 View 。在表单下方,我有一个重复代码块的 foreach 循环。我需要能够从表单选择(过滤器)更新代码块。

这是我当前的 View “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>
}

现在这是我的 Controller 。我在 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);
}

谢谢。

最佳答案

@VishalVaishya 提出了正确的想法,但有一种更简单的方法,不涉及自定义 JavaScript 代码:AjaxOptions 有一个 UpdateTargetId 属性,AJAX 工具包将解释该属性,表示您希望使用发送的结果更新给定的目标从 Controller 返回。

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)
{
...
}

Controller 操作:

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

关于ajax - MVC 4 Razor 使用 Ajax 表单更新 foreach 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19626480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com