Kendo网格值未传递给模型

时间:2013-07-19 12:10:55

标签: c# kendo-ui kendo-grid

用户放入网格的信息似乎并未实际传入模型(我的理论),在控制器中返回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相同,我评论它,因为它不起作用。

我在问题区域后面停止了控制器,即locations为空的foreach语句,错误为“对象引用未设置为对象的实例”。 在可变税中,Locations列表也为空,让我相信该列表中实际上没有任何值。

修改/溶液

我最后通过在所有列之后添加ClientTemplate格式来修复它,并将此函数放在我视图的最后:

<script>

function index(dataItem) {
    var data = $("#Locations").data("kendoGrid").dataSource.data();
    return data.indexOf(dataItem);
}

</script>

1 个答案:

答案 0 :(得分:0)

尝试:

@(Html.Kendo()
      .Grid<Johnson.Billing.Models.Location>()
      .BindTo(Model.Locations)

而不是:

@(Html.Kendo().Grid(Model.Locations)