我正在重写帖子以使其更清晰
我有一个具有以下操作的HomeController
public ActionResult About() { MapDetailsRepository repMapDetail = new MapDetailsRepository("name=ge"); YearRepository repYear = new YearRepository("name=ge"); SuperMapModel smM = new SuperMapModel(); smM.saVM = new StudentAssessmentViewModel(); MapDetailResultSet mapDetailResultSet = repMapDetail.GetMapDetails(53); smM.saVM.mapDetails = mapDetailResultSet.mapDetails.ToList(); smM.saVM.results = mapDetailResultSet.results.ToList(); smM.saVM.students = mapDetailResultSet.students.ToList(); smM.scM = new SearchControlViewModel(); smM.scM.YearList = new SelectList(repYear.GetAll(), "yearID", "year"); return View(smM); } public ActionResult DisplaySearchResults(string searchText) { MapDetailsRepository repMapDetail = new MapDetailsRepository("name=ge"); SuperMapModel smM = new SuperMapModel(); smM.saVM = new StudentAssessmentViewModel(); MapDetailResultSet mapDetailResultSet = repMapDetail.GetMapDetails(22); smM.saVM.mapDetails = mapDetailResultSet.mapDetails.ToList(); smM.saVM.results = mapDetailResultSet.results.ToList(); smM.saVM.students = mapDetailResultSet.students.ToList(); return PartialView("~/Views/Maps/_MapDetailsList.cshtml", smM.saVM); }
SuperMapModel是具有2个视图模型的超类-scM用于搜索面板部分控制-saVM用于MapDetailList部分控制
在页面上加载SuperMapModel传递给View,该View将各自的模型传递给每个部分控件
<div> @Html.Partial("_SearchPanel", Model.scM) </div> <div class="row"> <div class="col-md-12 table-responsive" > @Html.Partial("~/Views/Maps/_MapDetailsList.cshtml", Model.saVM) </div>
我的MapDetails部分控制非常复杂,因此我提供了一个简化的版本
<table> <thead> <tr><th/><th/><th/></tr> <tr><th/><th/><th/></tr> </thead> <tbody> @foreach(ShortStudent geStudent in Model.students) { <tr> <td>@geStudent.firstname</td> foreach (run it x number of times) { <td> @foreach (ShortResult geResult in Model.results) { if (geResult.ResultValue != null) { <script>{ setResultValue(@colIndex, @rowIndex, '@geResult.ResultValue'); }</script> } } </td> } </tr> } </tbody> </table>
setResultValue函数在主视图(关于)上定义,它设置当前td的innerhtml
function setResultValue(colIndex, rowIndex, resultValue) { alert("inside about"); var cell = $("#MapDetails").children().children()[rowIndex].children[colIndex]; cell.innerHTML = resultValue; }
PS此网格将转换为document.ready上的剑道网格。
现在,它可以完美地在页面加载中工作,但是当我从searchpanel内部单击搜索按钮时,我会执行以下操作(在searchpanel部分控件上定义)
function refreshGrid() { alert("search panel"); var url = '@Url.Action("DisplaySearchResults", "Home")'; $("#MapDetails").load(url, { searchText: "Lee" }, function () { alert("success"); }); }
PS这里的回叫从不触发
所以现在我只将部分视图与 DisplaySearchResults中* 的新数据绑定 *
现在很明显,它在主页上找不到脚本函数setResultValue,因此我在局部视图mapdetailslist上复制了相同的脚本函数setResultValue(这当然是一种不好的做法)
更新
调试后,我得到了更多详细信息,所以现在页面加载时出现了新问题,我可以得到$(“#MapDetails”)。children()。children()[rowIndex],但单击按钮后未定义
所以问题是我无法使用以下代码获取td元素
$("#MapDetails").children().children()[rowIndex]
我已经以一种非常笨拙的方式解决了它,但是很想拥有一个更整洁的解决方案
function setResultValue(colIndex, rowIndex, resultValue) { if ($("#MapDetails").children().children()[rowIndex] != undefined) { var cell = $("#MapDetails").children().children()[rowIndex].children[colIndex]; cell.innerHTML = resultValue; } else { var newRow = 0, newCol = 0, assessmentLength = 5, elementPos = 0; newRow = rowIndex - 3; newCol = colIndex - 4; elementPos = (newRow * assessmentLength) + newCol; $(".test").get(elementPos).innerHTML = resultValue; } }
您可以在标记中看到.test是我的td的类
我的问题是仍然存在部分视图渲染时加载脚本元素和dom元素的顺序