I keep getting "No items to display" in my child grids.
I have 2 view models, Master and Name. A Master can have multiple Names bound.
When I execute the page, it shows me Master information as expected. But its child, Name, has no items displayed although when I breakpoint on the JSON it contains 2 Names that it is returning.
Index.cshtml
#(
Html.Kendo().Grid(Model).Name("MainGrid")
.Columns(col =>
{
col.Bound(m => m.ListingId).Width(100);
col.Bound(m => m.Gender).Width(100);
})
.ClientDetailTemplateId("NameGridTemplate")
.Reorderable(reorder => reorder.Columns(true))
.Sortable()
.Scrollable()
.Filterable()
.Groupable()
.Pageable()
.DataSource(ds => ds
.Ajax()
.Model(m => m.Id(i => i.ListingId))
.Read(read => read.Action("HierarchyBinding_Master", "Main"))
)
.Events(events => events.DataBound("dataBound"))
)
<script id="NameGridTemplate" type="text/kendo-tmpl">
#(Html.Kendo().Grid<NameViewModel>()
.Name("grid_#=ListingId#") // template expression, to be evaluated in the master context
.Columns(columns =>
{
columns.Bound(o => o.PersonName).Width(110);
columns.Bound(o => o.Type).Width(110);
})
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Read(read => read.Action("HierarchyBinding_Name", "Main", new { listingId = "#=ListingId#" }))
)
.Pageable()
.Sortable()
.ToClientTemplate()
)
</script>
<script>
function dataBound() {
this.expandRow(this.tbody.find("tr.k-master-row").first());
}
</script>
Master ViewModal
public class MasterViewModal
{
public int ListingId { get; set; }
public String Gender { get; set; }
public int? Swn { get; set; }
public DateTime? SwnExpDate { get; set; }
public String Source { get; set; }
public Boolean PrStatus { get; set; }
public String PrCountry { get; set; }
public Boolean PermanentBan { get; set; }
public String BanIssueAgency { get; set; }
}
NameViewModal
public class NameViewModel
{
public int ListingId { get; set; }
public String PersonName { get; set; }
public String Type { get; set; }
public DateTime DateLastUpd { get; private set; }
public String UserLastUpd { get; private set; }
}
Main controller
public class MainController : Controller
{
ListingContext db = new ListingContext();
public ActionResult Index()
{
List<Master> dataList = null;
dataList = db.Masters.ToList();
List<MasterViewModal> mvmList = Logic.GetMasterViewModels(dataList);
return View(mvmList);
}
public ActionResult HierarchyBinding_Master([DataSourceRequest] DataSourceRequest request)
{
List<Master> masterList = null;
masterList = db.Masters.ToList();
return Json(Logic.GetMasterViewModels(masterList));
}
public ActionResult HierarchyBinding_Name(int listingId, [DataSourceRequest] DataSourceRequest request)
{
List<Name> nameList = null;
nameList = db.Names.ToList();
JsonResult j = Json(nameList
.Where(name => name.ListingId == listingId)
.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
return j;
}
}
Please try this
public ActionResult HierarchyBinding_Name(int listingId, [DataSourceRequest] DataSourceRequest request)
{
List<Name> nameList = null;
nameList = db.Names.ToList();
return Json(nameList
.Where(name => name.ListingId == listingId)
.ToDataSourceResult(request));
}
Remove the Jsonrequestbehaviour.Allowget.
I think it ll work. I dont see anything wrong with your razor code
Related
In my asp.net mvc project I have a model and a filled list.
I want to view this list with gridview but I get this error Value cannot be null Parameter name: source how can I solve this?
this my model:
namespace MalEkBilgiTanim.Models
{
public class EkBilgiModel
{
public string Mal_ekbilgi_adi { get; set; }
public string Mal_ekbilgi_no { get; set; }
public string Mal_grup_no { get; set; }
public string Mal_grup_adi { get; set; }
public string Mal_altgrup_no { get; set; }
public string Mal_altgrup_adi { get; set; }
public string Mal_sinif_no { get; set; }
public string Mal_sinif_adi { get; set; }
public string Calisir { get; set; }
public string Mal_ekbilgi_detay_no { get; set; }
public string Mal_ekbilgi_detay_adi { get; set; }
}
}
public ActionResult SinifEslestirme(string ekbilgidetay)
{
var liste = Classes.SinifEslestirme.SinifEkBilgiDetayGetirmeListesi(ekbilgidetay);
return View(liste);
}
and grid code in cshtml
#Html.Grid(Model).Columns(col =>
{
col.Add(x => x.Mal_grup_no);
col.Add(x => x.Mal_grup_adi);
col.Add(x => x.Mal_altgrup_no);
col.Add(x => x.Mal_altgrup_adi);
col.Add(x => x.Mal_sinif_no);
col.Add(x => x.Mal_sinif_adi);
col.Add(x => x.Calisir);
})
I want to integrate mvc6.grid, I followed the steps but getting error in view. Error "'HtmlHelper< IEnumerable< SchemeMaster>>' does not contain a definition for 'Grid'".
Model:-
public partial class SchemeMaster
{
public int SchemeID { get; set; }
public string SchemeName { get; set; }
public Nullable<int> Createdby { get; set; }
public Nullable<System.DateTime> Createddate { get; set; }
public string CompanyName { get; set; }
public string city { get; set; }
public Nullable<bool> Married { get; set; }
}
View :-
#model IEnumerable< MVC6_Grid_with_filters.Models.SchemeMaster>
#using NonFactors.Mvc.Grid;
#{
Layout = null;
}
< link href="#Url.Content("~/Content/MvcGrid/mvc-grid.css")" rel="stylesheet" />
< script src="~/Scripts/jquery-2.1.4.min.js">< /script>
< script src="~/Scripts/MvcGrid/mvc-grid.js" type="text/javascript">< /script>
#(Html
.Grid(Model)
.Build(columns =>
{
columns.Add(c => c.SchemeID).Titled("SchemeID");
columns.Add(c => c.SchemeName).Titled("SchemeName");
columns.Add(c => c.city).Titled("City Name");
columns.Add(o => o.CompanyName).Titled("Company Name");
columns.Add(c => c.Createddate, "Createddate").Titled("Date");
columns.Add(c => c.Married).Titled("Married");
})
.Filterable()
.Sortable()
.Pageable()
)
<script>
$('.mvc-grid').mvcgrid();
</script>
My fix was to download mvc5.grid not 6 from nuget
try adding #using NonFactors.Mvc.Grid; to _ViewImports.cshtml
mvc6-grid.azurewebsites.net/installation
I have implemented a KendoUI Grid.
Here is the Grid implementation in the View.
#(Html.Kendo().Grid<Example.Web.UI.ViewModels.ExampleItem>()
.Name("ExampleGrid")
.Columns(columns =>
{
columns.Bound(p => p.Name);
columns.Bound(p => p.Label);
columns.Bound(p => p.Type);
columns.Bound(p => p.InputType);
columns.ForeignKey(p => p.ParentId, (System.Collections.IEnumerable)ViewData["items"], "Id", "Name").Title("Parent");
columns.Command(command => { command.Edit(); command.Destroy(); });
})
.Scrollable()
.Groupable()
.Sortable()
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Events(events => events.Error("error_handler"))
.Model(model =>
{
model.Id(p => p.Id);
model.Field(p => p.Id).DefaultValue(Guid.NewGuid());
model.Field(p => p.ParentId).DefaultValue(null);
})
.Create(update => update.Action("EditingPopup_Create", "Example"))
.Read(read => read.Action("EditingPopup_Read", "Example"))
.Update(update => update.Action("EditingPopup_Update", "Example"))
.Destroy(update => update.Action("EditingPopup_Destroy", "Example"))
)
)
Here is the ExampeItem Model:
public class ExampleItem
{
[ScaffoldColumn(false)]
public Guid Id { get; set; }
public string Name { get; set; }
public string Label { get; set; }
public string Type { get; set; }
[DisplayName("Input Type")]
public string InputType { get; set; }
public ExampleItem Parent { get; set; }
public Guid ParentId { get; set; }
}
In my Controller I set the foreign key items like this:
ViewData["items"] = exampleItems; // This is a List<ExampleItem>
But the Parent column is empty when the Grid is loaded. This is because I used a Guid as the datatype for the Id's.
So I changed the view Model to:
public class ExampleItem
{
[ScaffoldColumn(false)]
public int Id { get; set; }
public string Name { get; set; }
public string Label { get; set; }
public string Type { get; set; }
[DisplayName("Input Type")]
public string InputType { get; set; }
public ExampleItem Parent { get; set; }
public int? ParentId { get; set; }
}
And now everything works. So the question is:
Why does Kendo UI Grid not like Guid datatypes for foreign key fields? Is there a way fix this?
I even tried using a string and that didn't work either.
Is there perhaps additional libraries that needs to be installed so the UI components works better with Guids?
I need the Id's to be of type Guid because the records could be migrated to a different server and the ids needs to be unique.
I have pretty simple linq expression:
session.Query<Order>().Where(x => x.States.OrderByDescending(z => z.Date).FirstOrDefault().Name == "2").ToList();
Result: InvalidCastException Unable to cast object of type 'Antlr.Runtime.Tree.CommonTree' to type 'NHibernate.Hql.Ast.ANTLR.Tree.IASTNode'.
Same query with LinqPad works as expected: selects orders, which last state is OnTheWay. How can I circumvent this and get desired result?
Code to try yourself:
class Program
{
static void Main(string[] args)
{
ISessionFactory sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(x => x.FromConnectionStringWithKey("defaultConnectionStringForNhibernate")))
.Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetEntryAssembly()))
.BuildSessionFactory();
var session = sessionFactory.OpenSession();
var res2 =
session.Query<Order>().Where(x => x.States.OrderByDescending(z => z.Date).FirstOrDefault().Name == "2").ToList();
}
}
public class Order
{
public virtual int Id { get; set; }
public virtual IList<OrderState> States { get; set; }
public virtual string Name { get; set; }
}
public class OrderState
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual DateTime Date { get; set; }
public virtual Order Order { get; set; }
}
public class OrderMap : ClassMap<Order>
{
public OrderMap()
{
Id(x => x.Id)
.GeneratedBy.Identity();
HasMany(x => x.States)
.Inverse()
.AsBag()
.KeyColumn("OrderId");
Map(x => x.Name)
.Not.Nullable();
Table("Orders");
}
}
public class OrderStateMap : ClassMap<OrderState>
{
public OrderStateMap()
{
Id(x => x.Id)
.GeneratedBy.Identity();
References(x => x.Order)
.Column("OrderId");
Map(x => x.Name)
.Not.Nullable();
Map(x => x.Date)
.Not.Nullable();
Table("OrderStates");
}
}
After some time research I found solution:
var res = (from i in session.Query<Order>()
where ((from s in i.States
orderby s.Date descending
select s.Name).First()) == "2"
select i).ToList();
I cant get to work my grid using telerik
here's my code:
MODEL
public class Office
{
public int OfficeID { get; set; }
public string OfficeName { get; set; }
public string OfficeAddress { get; set; }
}
VIEWMODEL
public class OfficeViewModel
{
public int OfficeID { get; set; }
public string OfficeName { get; set; }
public string OfficeAddress { get; set; }
}
VIEW
#(Html.Telerik().Grid<Office>()
.Name("Offices")
.ToolBar(tb => tb.Insert())
.DataBinding(binding => binding.Ajax()
.Select("GetOffice", "Office")
.Update("UpdateOffice", "Office")
.Insert("InsertOffice", "Office")
.Delete("DeleteOffice", "Office"))
.DataKeys(keys => keys.Add(o => o.OfficeID))
.Columns(cols =>
{
cols.Bound(c => c.OfficeID).ReadOnly();
cols.Bound(c => c.OfficeName).Width(20);
cols.Bound(c => c.OfficeAddress).Width(70);
cols.Command(cmd =>
{
cmd.Edit();
cmd.Delete();
});
})
)
CONTROLLER
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
[GridAction]
public ActionResult GetOffices()
{
return View(new GridModel(GetOfficeViewModels()));
}
private IQueryable<OfficeViewModel> GetOfficeViewModels()
{
return db.Offices
.Select(
c => new OfficeViewModel
{
OfficeID = c.OfficeID,
OfficeName = c.OfficeName,
OfficeAddress = c.OfficeAddress
});
}
LASTLY, THE LAYOUT.
<li>#Html.ActionLink("Office", "Index", "Office")</li>
please help me solve this problem i have already spent many hours on this. I'm only a beginner :(
Thanks
It's fixed now, it should be
.Select("GetOffices", "Office")
I missed the 's', sorry I'm new at this telerik thing
Thanks