This is a migrated thread and some comments may be shown as answers.

After creating new record, the columnd Id (auto generated) not showing in the Grid.

2 Answers 482 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mervin
Top achievements
Rank 1
Mervin asked on 10 Oct 2017, 06:11 AM

I have a model with two field (Code and Item). The Code is set to auto increment and the Item is a required field. When I clicked the Add New Record button, the Code column was set to 0 in the Popup. After I add a new record, the Code field in the grid was set to 0 even I return the model with the new record. 

 

Model:

public partial class HousenMokutekiD
    {
        public int Code { get; set; }

        [Required]
        [StringLength(20, MinimumLength = 3)]
        public string Item { get; set; }
    }

Controller:

public ActionResult Create([DataSourceRequest]DataSourceRequest request, HousenMokutekiD model)
        {
            if (string.IsNullOrWhiteSpace(model.Item))
            {
                ModelState.AddModelError("", "Item is required.");
            }

            if (model != null && ModelState.IsValid)
            {
                var entity = new HousenMokutekiD();

                if (model.Item != null)
                {
                    entity.Item = model.Item;
                }

                _context.HousenMokutekiD.Add(entity);
                _context.SaveChanges();

                model.Code = entity.Code;
                model.Item = entity.Item;                
            }
            return Json(new[] { model }.ToDataSourceResult(request, ModelState));
        }

Razor:

@model IEnumerable<Sms.Office.Data.PmsDd.Models.HousenMokutekiD>

@(Html.Kendo().Grid(Model)
            .Name("grid")
            .Columns(columns =>
            {
                columns.Command(command =>
                {
                    command.Edit().UpdateText("Save Changes");
                    command.Destroy();
                }).Width(162);
                columns.Bound(p => p.Code);
                columns.Bound(p => p.Item);
            })
            .ToolBar(add =>
            {
                add.Create();
            })
            .Editable(editable =>
            {
                editable.Mode(GridEditMode.PopUp);
                editable.Window(w => w.Title(""));
            })
            .Events(e =>
            {
                e.Edit("onEdit");
            })
            .Navigatable()
            .Pageable()
            .Sortable()
            .Selectable()
            .DataSource(dataSource => dataSource
                .Ajax()
                .ServerOperation(false)
                .PageSize(7)
                .Events(events =>
                {
                    events.Error("error_handler");
                    events.RequestEnd("request_end");
                })
                .Model(model =>
                {
                    model.Id(p => p.Code);
                })
                .Read(read => read.Action("Read", "Purpose"))
                .Create(create => create.Action("Create", "Purpose").Type(HttpVerbs.Post))
                .Update(update => update.Action("Update", "Purpose").Type(HttpVerbs.Put))
                .Destroy(destroy => destroy.Action("Destroy", "Purpose").Type(HttpVerbs.Delete))
            )
        )

 

Please help me. Sorry I am a newbee in MVC

 

2 Answers, 1 is accepted

Sort by
0
Mervin
Top achievements
Rank 1
answered on 10 Oct 2017, 06:19 AM
What happen was the data of Code column in the grid was 0.
0
Mervin
Top achievements
Rank 1
answered on 11 Oct 2017, 01:27 AM

Problem was already solved. I just add the following code in the Startup.cs

 

Startup.cs

services.AddMvc()
                .AddJsonOptions(option => option.SerializerSettings.ContractResolver = new DefaultContractResolver()); // This code fixed my issue

 

Thanks guys.

 

 

Tags
Grid
Asked by
Mervin
Top achievements
Rank 1
Answers by
Mervin
Top achievements
Rank 1
Share this question
or