很抱歉,如果我不熟悉ASP.NET MVC的新手。
如下面的代码示例所示,我在局部视图中有Kendo Textbox和Multiselect控件。当文本框的“更改”事件被触发时,我发布表单并尝试将Kendo Multiselect控件重新绑定到模型。
Multiselect控件在初始加载时绑定到Model,但在ajax调用之后,它不会重新绑定/刷新以反映模型中的更改。
任何人都可以帮助解决我可能出错的地方吗?
请参阅下面的代码示例:
Index.cshtml:
@model MVCTest20.Models.Config
@{
ViewBag.Title = "Home Page";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script type="text/javascript">
jQuery(function ($) {
$('#SalesOrder').on("change", function () {
$.ajax({
url: '/Home/Index',
data: $('#form').serialize(),
type: 'POST'
});
});
});
</script>
<form>
@{Html.RenderPartial("_SOLookup", Model);}
</form>
_SOLookup.cshtml
<p><strong>Sales Order / Project</strong></p>
@(Html.Kendo().TextBox()
.Name("SalesOrder")) <div id="sales-order-arrow" class="help_arrow"></div>
<p><strong>Line Item</strong></p>
@(Html.Kendo().MultiSelect()
.Name("LineItems")
.Placeholder("Select Line(s)")
.HtmlAttributes(new {@class= "cb-create-control" })
.DataTextField("Name")
.DataValueField("Id")
.AutoBind(true)
.BindTo(Model.LineItemCollection))
HomeController中:
public class HomeController : Controller
{
private static Config config;
public ActionResult Index()
{
if (config == null)
{
config = new Config();
}
config.LineItemCollection = new LineItem[]
{
new LineItem {Name = "500.1 - FAS8060AE - 001 - R6", Id = "700"}
}.ToList();
return View(config);
}
[HttpPost]
public ActionResult Index(Config data)
{
data.LineItemCollection = new LineItem[]
{
new LineItem {Name = "1.1 - FAS8060AE - 001 - R6", Id = "100"},
new LineItem {Name = "3.1 - FAS8060AE - 001 - R6", Id = "200"},
new LineItem {Name = "6.1 - FAS8060AE - 001 - R6", Id = "300"}
}.ToList();
return View(data);
}
型号:
public class Config
{
public Config()
{
LineItemCollection = new List<LineItem>();
}
private string _salesorder;
public string SalesOrder
{
get
{
return _salesorder;
}
set
{
_salesorder = value;
}
}
public string LineItems { get; set; }
public List<LineItem> LineItemCollection { get; set; }
}
public class LineItem
{
public string Id { get; set; }
public string Name { get; set; }
}