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

Grid can firing Update,Create,but can Get Data,can firing Delete.

2 Answers 81 Views
Grid
This is a migrated thread and some comments may be shown as answers.
dcs
Top achievements
Rank 1
dcs asked on 17 Jan 2019, 01:45 AM

.Net Core 2.1,the cshtml

@using Kendo.Mvc.UI
@(Html.Kendo().Grid<Liwb.Entities.Travel>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.ID);
        columns.Bound(p => p.Name);
        columns.Bound(p => p.Pos);
        columns.Bound(p => p.IsDelete);
        columns.Bound(p => p.From);
        columns.Bound(p => p.To);
        columns.Bound(p => p.StartDate).Format("{0:MM/dd/yyyy}");
        columns.Bound(p => p.EndDate).Format("{0:MM/dd/yyyy}");
        columns.Bound(p => p.IntervalDays);
        columns.Bound(p => p.TrafficType);
        columns.Bound(p => p.PhotographDirectory);
        columns.Bound(p => p.ProblemDocument);
        columns.Command(commands =>
        {
            commands.Edit();
            commands.Destroy(); // The "destroy" command removes data items.
        }).Title("Commands").Width(200);
    })
    .Editable(editMode => editMode.Mode(GridEditMode.InLine))
    .Pageable()
    //.Sortable()
    .Scrollable()
    //.Filterable()
    .HtmlAttributes(new { style = "height:100%; width:100%" })
    .DataSource(dataSource => dataSource
        .WebApi()
        .Model(Model => { Model.Id(p => p.ID);})
        .Events(events=>events.Error("error_handler"))
        .Read(read => read.Action("Get", "Travel"))
        .Create(create => create.Action("Post", "Travel"))
        .Update(update => update.Action("Put", "Travel", new { id = "{0}" }))
        .Destroy(destory => destory.Action("Delete", "Travel", new { id = "{0}" }))
    )
)

the api controller
namespace PlantManagement.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TravelController : ControllerBase
    {
        private readonly PlantManagementDbContext _context;
        public TravelController(PlantManagementDbContext context)
        {
            _context = context;
        }
        //MVC return Value: ActionResult
        // GET: api/Travel
        [HttpGet]
        //public IEnumerable<Travel> GetTravels()
        public DataSourceResult Get([DataSourceRequest]DataSourceRequest request)
        {
            return _context.Travels.ToDataSourceResult(request);//.Where(p => p.PID == null)
        }
        // GET: api/Travel/5
        [HttpGet("{id}")]
        public async Task<IActionResult> Get([FromRoute] long id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var travel = await _context.Travels.FindAsync(id);
            if (travel == null)
            {
                return NotFound();
            }
            return Ok(travel);
        }
        // PUT: api/Travel/5
        [HttpPut("{id}")]
        public async Task<IActionResult> Put([FromRoute] long id, [FromBody] Travel travel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            if (id != travel.ID)
            {
                return BadRequest();
            }
            _context.Entry(travel).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TravelExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return NoContent();
        }
        // POST: api/Travel
        [HttpPost]
        public async Task<IActionResult> Post( Travel travel)//[FromBody]
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            _context.Travels.Add(travel);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (TravelExists(travel.ID))
                {
                    return new StatusCodeResult(StatusCodes.Status409Conflict);
                }
                else
                {
                    throw;
                }
            }
            return CreatedAtAction("GetTravel", new { id = travel.ID }, travel);
        }
        // DELETE: api/Travel/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete([FromRoute] long id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var travel = await _context.Travels.FindAsync(id);
            if (travel == null)
            {
                return NotFound();
            }
            travel.IsDelete = true;
            //_context.Travels.Remove(travel);
            await _context.SaveChangesAsync();
            return Ok(travel);
        }
        private bool TravelExists(long id)
        {
            return _context.Travels.Any(e => e.ID == id);
        }
    }
}

2 Answers, 1 is accepted

Sort by
0
dcs
Top achievements
Rank 1
answered on 17 Jan 2019, 03:23 AM
Sorry, Grid can not firing Update,Create event , but can Get data, can Delete data. Please help me , thanks!
0
Tsvetomir
Telerik team
answered on 21 Jan 2019, 02:27 PM
Hello,

I assume that the provided Grid for ASP.NET Core has been built following the Web API Data binding demo, am I correct? If that is the case, I have noticed that the Attribute Routing is present in the grid's declaration:

[Route("api/[controller]")]
[ApiController]
public class TravelController : ControllerBase
{
    private readonly PlantManagementDbContext _context;

It there is not a URL which ends with /api/Travel, the request will not be sent at all. Can you check out the Network tab of the DevTool developer tools and examine the URLs that are requested? 

Also, the Create Action method will be hit only if an "Add New Record" button is present. It could be enabled via the Toolbar configuration of the grid:

.ToolBar(toolbar => {
    toolbar.Create();
})

If the issue persists after examining the requested URLs and ensuring that the correct ones are invoked, share a sample project in which the faulty behavior can be reproduced and send it back to me. This would give me the opportunity to investigate the case locally and provide accurate suggestions.


Kind regards,
Tsvetomir
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
dcs
Top achievements
Rank 1
Answers by
dcs
Top achievements
Rank 1
Tsvetomir
Telerik team
Share this question
or