简体   繁体   English

Kendo网格值未传递到模型

[英]Kendo grid values not being passed to model

The information a user puts into the grid seems to not actually pass into the model (my theory), returning a null value in the controller. 用户放入网格中的信息似乎实际上并未传递到模型中(我的理论),从而在控制器中返回了null值。 I have tried various little fixes, and have looked around it for quite some time, but dont have the experience to see the issue here. 我已经尝试了各种小修复,并在相当长的时间内环顾了一下,但是没有经验可以在这里看到问题。 Can anyone offer any help here? 有人可以在这里提供任何帮助吗?

View: 视图:

@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" />
}

(The create function was something I added in recently that didnt work, so I commented it out for now) (创建功能是我最近添加的,但没有用,所以我暂时将其注释掉了)

Model: 模型:

    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; }
    }
}

Controller: 控制器:

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
                {

Same thing with the Location_Create in the controller, I commented it out as it did not work. 与控制器中的Location_Create相同,我将其注释掉,因为它不起作用。

I stopped the controller right after the issue area, the foreach statement, where locations is null, with the error "Object reference not set to an instance of an object." 我在问题区域foreach语句之后( locations为null的情况下)立即停止了控制器,并出现错误“对象引用未设置为对象的实例”。 In the variable tax, the Locations list is also null, bringing me to believe that there arent actually any values in that list. 在可变税项中,“ Locations列表也为空,这使我相信该列表中实际上没有任何值。

EDIT/SOLUTION: 编辑/解决方案:

I ended up fixing it by adding a ClientTemplate format after all the columns, along with this function placed at the very end of my view: 我最终通过在所有列之后添加ClientTemplate格式以及在此视图末尾放置的此功能来解决此问题:

<script>

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

</script>

Try: 尝试:

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

instead of: 代替:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM