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

Basic Grid post not working

3 Answers 124 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ekta
Top achievements
Rank 1
Ekta asked on 01 Sep 2016, 04:55 PM

I have written code for so many grids. But this one grid is giving me trouble. Not sure what the problem is. Its a basic master table grid with read/create/delete. Read and Delete get called but for some reason create and update don't get invoked. Any ideas?

 

View file Code:

@using Kendo.Mvc.UI;
@{
ViewBag.Title = "Import FileNames";
}
<h4>Maintain Import FileNames</h4>
@(Html.Kendo().Grid<CaseManagement.Models.ImportFileModel>()
.Name("importFileGrid")
.Columns(columns =>
{
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
columns.ForeignKey(p => p.InventoryType, (System.Collections.IEnumerable)ViewData["InventoryTypeList"], "InventoryCode", "Title").HeaderTemplate("<b>Inventory</b>").Width(150);
columns.Bound(o => o.FileName).Title("File Name");
//columns.Bound(o => o.importId);
//columns.Bound(o => o.InventoryType);
})
.ToolBar(toolbar =>
{
toolbar.Template(@<text>
<div class="toolbar">
<div style="float: right;">
<a class="k-button k-button-icontext k-grid-add" href="#"><span class="k-icon k-add"></span>Add new row</a>
</div>
</div>
</text>);
})
.Editable(editable => editable.Mode(GridEditMode.InLine))
//.Events(events => events.DataBound("onImportFileGridDataBound"))
.HtmlAttributes(new { style = "width: 1000px;" })
.Scrollable()
.Sortable()
.Groupable()
.Pageable()
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(100000)
.Model(model =>
{
model.Id(p => p.importId);
//model.Field(p => p.ImportFileId).DefaultValue("");
})
.Read(read => read.Action("ImportFile_Read", "ImportFile"))
.Create(create => create.Action("ImportFile_Create", "ImportFile"))
.Update(update => update.Action("ImportFile_Update", "ImportFile"))
.Destroy(destroy => destroy.Action("ImportFile_Destroy", "ImportFile"))
)
)
<script type="text/javascript">
function onImportFileGridDataBound() {
this.table.find(".k-grid-edit").hide();
}
</script>

 

Controller code:

public class ImportFileController : Controller
{
public ImportFileController()
{
ViewData["InventoryTypeList"] = InventoryType.GetInventoryTypes().Select(t => new InventoryType
{
InventoryCode = t.InventoryCode,
Title = t.Title,
DisplayOrder = t.DisplayOrder
}).OrderBy(t => t.DisplayOrder);
}
public ActionResult ImportFileName()
{
return View();
}
public ActionResult ImportFile_Read([DataSourceRequest]DataSourceRequest request)
{
return Json(GetImportFiles().ToDataSourceResult(request));
}
private static IEnumerable<ImportFileModel> GetImportFiles()
{
List<ImportFile> objList;
objList = ImportFile.GetImportFileAll();
int ctr = 1;
List<ImportFileModel> modelList = new List<ImportFileModel>();
foreach(ImportFile item in objList)
{
ImportFileModel m = new ImportFileModel();
m.importId = ctr;
m.InventoryType = item.InventoryType;
m.FileName = item.FileName;
modelList.Add(m);
ctr++;
}
return modelList;
//return objList.Select(obj => new ImportFileModel
//{
// importId =
// ImportFileId = obj.InventoryType + "_" + obj.FileName,
// InventoryType = obj.InventoryType,
// FileName = obj.FileName,
//});
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ImportFile_Create([DataSourceRequest] DataSourceRequest request, ImportFileModel model)
{
if (model != null && ModelState.IsValid)
{
var obj = new ImportFile();
CopyModelToObject(model, obj);
obj.Insert();
}
return Json(new[] { model }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ImportFile_Update([DataSourceRequest] DataSourceRequest request, ImportFileModel model)
{
if (model != null && ModelState.IsValid)
{
var obj = new ImportFile();
CopyModelToObject(model, obj);
//obj.Save();
}
return Json(new[] { model }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ImportFile_Destroy([DataSourceRequest] DataSourceRequest request, ImportFileModel model)
{
if (model != null)
{
var obj = new ImportFile();
CopyModelToObject(model, obj);
//obj.Delete();
}
return Json(new[] { model }.ToDataSourceResult(request, ModelState));
}
private void CopyModelToObject(ImportFileModel model, ImportFile obj)
{
obj.InventoryType = model.InventoryType;
obj.FileName = model.FileName;
obj.RecordAddedBy = User.Identity.Name.Substring(User.Identity.Name.IndexOf(@"\") + 1);
}
}

3 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 05 Sep 2016, 11:17 AM
Hi Ekta,

I examined the provided code and as far as I can see it looks correctly configured. Nevertheless, could you please let me know which version of MVC you are using. Also could you please verify that no exception is thrown which might prevent calling the methods? Additionally you can try removing the [AcceptVerbs(HttpVerbs.Post)] attribute from your methods and let me know about the results.

Regards,
Kostadin
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Ekta
Top achievements
Rank 1
answered on 06 Sep 2016, 12:56 PM

Hello Kostadin,

I am using MVC 4. This page has me baffled. This is a huge application with lots of pages and lots of grids. I didn't have any problem with any of the grids, not just in this application but in another application using the same MVC 4 framework and same Telerik versions.

 

I did try completely removing the Post attribute but the code does not reach there. I have a breakpoint setup in the create and update method. I have been running the project in debug mode to check if any errors/exceptions occur but nothing pops up.

 

Any help is appreciated!

0
Kostadin
Telerik team
answered on 08 Sep 2016, 08:25 AM
Hello Ekta,

Could you please try isolate the grid in a small runnable sample and check whether the same configuration is working? If you are able to replicate the issue we will be glad to assist you further. Additionally I noticed that you are not having a column with buttons that put the grid in edit mode. Could you please let me know how you are doing that.

I am looking forward to your reply.

Regards,
Kostadin
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Ekta
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Ekta
Top achievements
Rank 1
Share this question
or