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
Related
In an ASP.NET Core 1.1 Web API, I am trying to map an entity model to a DTO using AutoMapper.
The entity model:
namespace InspectionsData.Models
{
[Table("property")]
public class Property
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("property_id")]
public int? Id { get; set; }
[Column("show_inventory")]
public bool ShowInventory { get; set; }
[Column("latitude")]
public double? Latitude { get; set; }
[Column("longitude")]
public double? Longitude { get; set; }
[Column("property_type_id")]
public int? PropertyTypeId { get; set; }
[ForeignKey("PropertyTypeId")]
[Display(Name = "Property Type")]
public PropertyType PropertyType { get; set; }
[Column("inspection_frequency_id")]
public int? InspectionFrequencyId { get; set; }
[ForeignKey("InspectionFrequencyId")]
[Display(Name = "Inspection Frequency")]
public InspectionFrequency InspectionFrequency { get; set; }
[Column("group_id")]
public int? GroupId { get; set; }
[ForeignKey("GroupId")]
[Display(Name = "Group")]
public Group Group { get; set; }
[Column("added_by_id")]
public int? AddedById { get; set; }
[ForeignKey("AddedById")]
[Display(Name = "Added By")]
public virtual User AddedBy { get; set; }
[Column("added_date")]
[Display(Name = "Added Date")]
public DateTime AddedDate { get; set; }
[Column("deleted_by_id")]
public int? DeletedById { get; set; }
[ForeignKey("DeletedById")]
[Display(Name = "Deleted By")]
public virtual User DeletedBy { get; set; }
[Column("deleted_date")]
[Display(Name = "Deleted Date")]
public DateTime? DeletedDate { get; set; }
}
and the DTO:
namespace InspectionsData.DTOs
{
public class PropertyDto
{
public int? Id { get; set; }
public bool ShowInventory { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public PropertyType PropertyType { get; set; }
public InspectionFrequency InspectionFrequency { get; set; }
public DateTime NextInspectionDate { get; set; }
}
}
The mapping is done in a configuration file:
public class AutoMapperProfileConfiguration : Profile
{
public AutoMapperProfileConfiguration()
{
// Add as many of these lines as you need to map your objects
var map = CreateMap<Property, PropertyDto>();
map.ForAllMembers(opt => opt.Ignore());
map.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id));
map.ForMember(dest => dest.ShowInventory, opt => opt.MapFrom(src => src.ShowInventory));
map.ForMember(dest => dest.Latitude, opt => opt.MapFrom(src => src.Latitude));
map.ForMember(dest => dest.Longitude, opt => opt.MapFrom(src => src.Longitude));
map.ForMember(dest => dest.PropertyType, opt => opt.MapFrom(src => src.PropertyType));
map.ForMember(dest => dest.InspectionFrequency, opt => opt.MapFrom(src => src.InspectionFrequency));
}
}
And the setting up of AutoMapper in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
var config = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.AddProfile(new AutoMapperProfileConfiguration());
});
var mapper = config.CreateMapper();
services.AddSingleton(mapper);
}
In my controller action, I execute the mapping:
[HttpGet]
public async Task<IActionResult> GetProperty()
{
var properties = _context.Property
.Include(t => t.PropertyType)
.Include(f => f.InspectionFrequency)
.Where(a => a.DeletedDate == null && a.GroupId == 1);
var propertiesDto = _mapper.Map<IEnumerable<PropertyDto>>(properties);
return Ok(propertiesDto);
}
It doesn't give an error, but all the properties in all the objects in the propertiesDto list are default values (NULL for objects and nullable types, FALSE for booleans, 0 for integers, etc.) Any ideas where I'm going wrong?
It's because the following line
map.ForAllMembers(opt => opt.Ignore());
is letting AM ignore all member mappings, including the ones you have configured explicitly.
Simply use ForAllOtherMembers instead:
map.ForAllOtherMembers(opt => opt.Ignore());
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
i created a dropdown list in MVC page,i want when changed id,new data pass to Action with below code:
#if (ViewBag.IdType==1)
{
#Html.DropDownListFor(m => m.Citycode, new SelectList(ViewBag.ResultList as System.Collections.IEnumerable, "Citycode", "CityName","1"))
#Html.ValidationMessageFor(model => model.Citycode)
}
else
{
#Html.DropDownListFor(m => m.IDReshteh, new SelectList(ViewBag.ResultList as System.Collections.IEnumerable, "IDReshteh", "ReshteName",Model.IDReshteh),"Fselect")
#Html.ValidationMessageFor(model => model.IDReshteh)
}
<input type="hidden" id="SelValue" value="1"/>
<input type="submit" value="Submit" />
</p>
/*<input type="hidden" name="HDcity" id="HDcity" />*/
<div id="LoadData">
#Html.Action("selVahedListAjax", new { IdReshte = ViewBag.ChooseItem, IdCity = Model.IDReshteh })
</div>
Model:
public class TInfo
{
[Key]
public int Id { get; set; }
[Required]
public string Scode { get; set; }
[Required]
public string SName { get; set; }
[Required]
public string TName { get; set; }
public string ModirName { get; set; }
public string FZamineh { get; set; }
public string TAddress { get; set; }
public string PcodeSh { get; set; }
public string TelcodeSh { get; set; }
public string FaxcodeSh { get; set; }
public string DAddress { get; set; }
public string PcodeDa { get; set; }
public string TelcodeDa { get; set; }
public string FaxcodeDa { get; set; }
public string WebsiteAddress { get; set; }
public string EmailAdd { get; set; }
public string TablighatPic { get; set; }
[Required]
public int Citycode { get; set; }
[ForeignKey ("Citycode")]
public Lcity City { get; set; }
[Required]
public int IDReshteh { get; set; }
[ForeignKey("IDReshteh")]
public RFaaliat Reshteh { get; set; }
}
My controller:
//If IdType=1 then user clicked on Reshteh But if IdType=2 then user clicked on city
public ActionResult selVahedList(int IdType, int IdChoose)
{
ViewBag.ChooseItem = IdChoose;
ViewBag.IdType = IdType;
if (IdType == 1)
ViewBag.ResultList = Dbcon.Lcitys.ToList();
else
ViewBag.ResultList = Dbcon.RFaaliats.ToList();
return View();
}
public ActionResult selVahedListAjax(int IdReshte,int IdCity )
{
ViewBag.Reshteh = IdReshte;
var res = Dbcon.TaavoniInfos.Where(m => m.IDReshteh == IdReshte && m.Citycode ==?);//Idont know what should put here
return PartialView(res);
}
my problem is i cant get and pass selected item in drop down and update data in page. please help me.
thanks a lot
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
Previously, I was using this in my model
public List<SelectListItem> StatusList { get; set; }
public string Status { get; set; }
and this in my view to bind to the Dropdownlist
#Html.DropDownListFor(model => model.Status, Model.StatusList, "")
but if I want to use
public List<ICode> StatusList { get; set; }
where ICode is as below
public interface ICode
{
int Id { get; set; }
ICodeGroup CodeGroup { get; set; }
string ShortDescription { get; set; }
string LongDescription { get; set; }
}
What should I do in my view and model?
Cybernate's solution works. You could also do:
#Html.DropDownListFor(model => model.Status,
new SelectList(Model.StatusList, "Id", "ShortDescription"))
Cybernate's version will do stronger type checking, but SelectList is more encapsulated.
Try this:
#Html.DropDownListFor(model => model.Status,
Model.StatusList.Select(a=>
new SelectListItem(){
Text=a.ShortDescription, Value=a.Id.ToString()
}),
"")