【发布时间】:2016-07-15 08:19:06
【问题描述】:
我在使用 ajax 重新加载部分视图时遇到问题。
我在部分视图中显示了我的国家/地区表单,最初当我加载主页时,所有内容都正确呈现。
如下图所示,我可以使用自动完成功能搜索国家,并且可以从我的组合框中选择一个国家(两者都是 kendo-mvc 控件)
当我从自动完成中选择一个并尝试通过 ajax 加载所选国家/地区的信息时,问题就出现了。重新加载表单,显示信息,但未正确呈现控件。自动完成功能停止工作,组合框呈现为普通文本框,显示 CountryID 而不是 Country 名称。
我认为的代码
@using (Html.BeginForm("Index", "CountryManagement", FormMethod.Post, new { id = "country-form" }))
{
@Html.ValidationSummary(true);
<div id="form-content">
@Html.Partial("_CountryForm", Model)
</div>
}
我的部分视图中的代码
<div class="form-group">
@Html.Label("Search Country", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-3">
@(Html.Kendo().AutoCompleteFor(m => m.Search)
.DataTextField("Designation")
//.DataValueField("CountryID")
.Filter("contains")
.MinLength(2)
.DataSource(d => { d.Read(r => r.Action("SearchCountry", "Common")); })
.Events(e => e.Change("onChange")).Deferred()
)
</div>
</div>
我的控制器中的代码
[HttpPost]
public PartialViewResult Edit(int id)
{
//HTTP GET: api/Country/CountryDetails?id={id}&lang={lang}
dynamic model = //code to get selcted country data
return PartialView("_CountryForm", model);
}
我的 .js 文件中的代码
function onChange() {
if ($("#Search").data("handler")) {
var data = $("#Search").data("handler").dataSource.data();
var country = data.find(x => x.Designation == $("#Search").val());
console.log("Country")
if (country) {
var request = $.post('/CountryManagement/Edit', { id: country.CountryID });
request.success(function (data) {
$("#form-content").html(data);
});
}
}
}
页面加载时生成的 HTML 代码(仅限自动完成和下拉菜单)
<div class="form-group">
<label class="control-label col-md-2" for="Search_Country">Search Country</label>
<div class="col-md-3">
<span tabindex="-1" role="presentation" class="k-widget k-autocomplete k-header k-state-default k-state-hover"><input data-val="true" data-val-length="Description must have between 1 and 150 characters" data-val-length-max="150" data-val-length-min="1" id="Search" name="Search" type="text" data-role="autocomplete" class="k-input" autocomplete="off" role="textbox" aria-haspopup="true" aria-disabled="false" aria-readonly="false" aria-owns="Search_listbox" aria-autocomplete="list" has-focus="false" style="width: 100%;"><span class="k-icon k-loading" style="display:none"></span></span>
</div>
</div>
<br>
<br>
<div class="form-group">
<label class="control-label col-md-2" for="Country">Country</label>
<div class="col-md-3">
<span class="k-widget k-combobox k-header"><span tabindex="-1" unselectable="on" class="k-dropdown-wrap k-state-default"><input name="CountryID_input" class="k-input" type="text" autocomplete="off" title="" role="combobox" aria-expanded="false" tabindex="0" aria-disabled="false" aria-readonly="false" aria-autocomplete="list" aria-owns="CountryID_listbox" has-focus="false" style="width: 100%;"><span tabindex="-1" unselectable="on" class="k-select"><span unselectable="on" class="k-icon k-i-arrow-s" role="button" tabindex="-1" aria-controls="CountryID_listbox">select</span></span></span><input data-val="true" data-val-number="The field CountryID must be a number." id="CountryID" name="CountryID" type="text" value="0" data-role="combobox" aria-disabled="false" aria-readonly="false" has-focus="false" style="display: none;"></span>
<span class="field-validation-valid text-danger" data-valmsg-for="CountryID" data-valmsg-replace="true"></span>
</div>
</div>
部分视图重新加载时生成的 HTML 代码(仅限自动完成和下拉菜单)
<div class="form-group">
<label class="control-label col-md-2" for="Search_Country">Search Country</label>
<div class="col-md-3">
<input data-val="true" data-val-length="Description must have between 1 and 150 characters" data-val-length-max="150" data-val-length-min="1" id="Search" name="Search" type="text" value="South Africa">
</div>
</div>
<br>
<br>
<div class="form-group">
<label class="control-label col-md-2" for="Country">Country</label>
<div class="col-md-3">
<input data-val="true" data-val-number="The field CountryID must be a number." id="CountryID" name="CountryID" type="text" value="1003">
<span class="field-validation-valid text-danger" data-valmsg-for="CountryID" data-valmsg-replace="true"></span>
</div>
</div>
我不确定这是否是 ASP.Net MVC 局部视图、Kendo 控件或在重新加载局部视图时应重新运行的某些脚本的问题。 有人可以帮帮我吗?
【问题讨论】:
-
你能显示你的js被调用的部分吗(你视图中的javascript部分?)
-
嗨@RaphaëlAlthaus,我从我的部分视图中添加了一些代码。因为我的自动完成是一个 Kendo-mvc 控件,所以调用 Javascript 的部分是自动完成定义的一部分,并且它绑定到更改事件。 == > .Events(e => e.Change("onChange")。谢谢
-
你能在 ajax 调用后显示生成的局部视图的 html 吗?可能是这样的:stackoverflow.com/questions/22412473/…
-
感谢您的链接,但不是同一个问题。我的模型绑定工作得很好,我得到了数据。但是,从您的最后一个请求中,我注意到确实在服务器加载时生成的 html 代码与在 ajax 重新加载时生成的代码不同。大多数不同的人似乎都对一些缺失的属性和剑道造型感到厌恶。
-
您在浏览器控制台中看到任何错误吗?
标签: c# ajax asp.net-mvc kendo-asp.net-mvc