![](/img/trans.png)
[英]Updated Kendo UI Grid values using jQuery does not update the passed back model
[英]Kendo grid values not being passed to model
用户放入网格中的信息似乎实际上并未传递到模型中(我的理论),从而在控制器中返回了null
值。 我已经尝试了各种小修复,并在相当长的时间内环顾了一下,但是没有经验可以在这里看到问题。 有人可以在这里提供任何帮助吗?
视图:
@model Johnson.Billing.Models.TaxService
@using(Html.BeginForm("Save", "TaxService"))
{
<div>
@(Html.Kendo().Grid(Model.Locations)
.Name("Locations")
.Columns(columns =>
{
columns.Command(command => { command.Destroy(); }).Width(80);
columns.Bound(o => o.State).Title("State").EditorTemplateName("TaxService");
columns.Bound(o => o.LocationNum).Title("Loc #");
columns.Bound(o => o.BasePremium).Title("Base Prem:").Format("{0:c}");
columns.Bound(o => o.CargoPremium).Title("Cargo Prem:").Format("{0:c}");
columns.Bound(o => o.InlandMarinePremium).Title("Inland Marine Prem:").Format("{0:c}");
columns.Bound(o => o.LiabilityPremium).Title("Liability Prem:").Format("{0:c}");
columns.Bound(o => o.LimitOfLiability).Title("Limit of Liability:").Format("{0:c}");
columns.Bound(o => o.PropertyContentPremium).Title("Property Content Prem:").Format("{0:c}");
})
.ToolBar(toolbar => toolbar.Create().Text("Add a State Tax Calculation"))
.Editable(editable => editable.Mode(GridEditMode.InCell))
.DataSource(dataSource => dataSource
.Ajax()
.ServerOperation(false)
.Model(model => model.Id(o => o.LocationNum))
//.Create(update => update.Action("Location_Create", "TaxService"))
))
</div>
<input type="submit" value="Test Taxes" />
}
(创建功能是我最近添加的,但没有用,所以我暂时将其注释掉了)
模型:
public class TaxService
{
public string DepartmentType { get; set; }
public DateTime? TransactionDate { get; set; }
public string TransactionType { get; set; }
public DateTime? OriginalTransactionDate { get; set; }
public int PolicyFee { get; set; }
public int CompanyFee { get; set; }
public int CompanyID { get; set; }
public string IncludesWind { get; set; }
public string CancellationType { get; set; }
public DateTime? EffectiveDate { get; set; }
public DateTime? TransactionEffectiveDate { get; set; }
public List<Location> Locations { get; set; }
}
public class Location
{
public int LocationNum { get; set; }
public string State { get; set; }
public int BasePremium { get; set; }
public int CargoPremium { get; set; }
public int InlandMarinePremium { get; set; }
public int LiabilityPremium { get; set; }
public int LimitOfLiability { get; set; }
public int PropertyContentPremium { get; set; }
}
}
控制器:
public class TaxServiceController : Controller
{
public ActionResult Index()
{
return View(new TaxService
{
Locations = new List<Location>{
new Location()
}
});
}
//[HttpPost]
//public ActionResult Location_Create(Location loc)
//{
//var x = new Location();
//if (loc != null && ModelState.IsValid)
//{
//x.State = loc.State;
//x.LocationNum = loc.LocationNum;
//x.PropertyContentPremium = loc.PropertyContentPremium;
//x.LimitOfLiability = loc.LimitOfLiability;
//x.LiabilityPremium = loc.LiabilityPremium;
//x.InlandMarinePremium = loc.InlandMarinePremium;
//x.CargoPremium = loc.CargoPremium;
//x.BasePremium = loc.BasePremium;
//};
//return (null);
//}
[HttpPost]
public ActionResult Save(TaxService tax)
{
using (var svc = new StateTaxService.StateTaxServiceClient())
{
var locations = new List<StateTaxService.TaxLocationsRequest>();
foreach (var location in tax.Locations)
{
locations.Add(new StateTaxService.TaxLocationsRequest
{
与控制器中的Location_Create相同,我将其注释掉,因为它不起作用。
我在问题区域foreach语句之后( locations
为null的情况下)立即停止了控制器,并出现错误“对象引用未设置为对象的实例”。 在可变税项中,“ Locations
列表也为空,这使我相信该列表中实际上没有任何值。
编辑/解决方案:
我最终通过在所有列之后添加ClientTemplate
格式以及在此视图末尾放置的此功能来解决此问题:
<script>
function index(dataItem) {
var data = $("#Locations").data("kendoGrid").dataSource.data();
return data.indexOf(dataItem);
}
</script>
尝试:
@(Html.Kendo()
.Grid<Johnson.Billing.Models.Location>()
.BindTo(Model.Locations)
代替:
@(Html.Kendo().Grid(Model.Locations)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.