Kendo网格值未传递给模型
本文关键字:模型 网格 Kendo | 更新日期: 2023-09-27 18:24:05
用户放入网格的信息似乎实际上并没有传递到模型中(我的理论),在控制器中返回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" />
}
(create函数是我最近添加的,但不起作用,所以我暂时把它注释掉了)
型号:
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,并返回错误"Object reference not set to a instance of a Object"在可变税中,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)