asp.net - How to get a specific row from database in MVC 4 -
i have model called pagemodels
public class pagemodels { public int id { get; set; } public string title { get; set; } [datatype(datatype.multilinetext)] [allowhtml] public string content { get; set; } public int parent { get; set; } [datatype(datatype.date)] public datetime created_at { get; set; } [datatype(datatype.date)] public datetime updated_at { get; set; } [datatype(datatype.date)] public datetime published_at { get; set; } }
and based on model have controller called pagescontroller. here have method:
public actionresult showpage(string mytitle) { var query = in db.pages a.title.contains(mytitle) select a; return view(db.pages.firstordefault()); }
i want able in return value content property of page has title given in mytitle. haven't been able find way of doing that.
//edit
that error kept getting fault ... between trying fix problem , implementing solution changed methods return type pagemodels , that's why kept throwing me error. works view doesn't show anything. i'll try fix myself help.
you getting result of filter variable query
in first line returning else (reading pages db , getting first item collection) view.
you need item variable query
has result of filter.
public actionresult showpage(string mytitle) { var query = in db.pages a.title.contains(mytitle) select a; return view(query.firstordefault()); }
make sure check null after firstordefault
, otherwise run exception when there no records in collection.
public actionresult showpage(string mytitle) { var query = in db.pages a.title.contains(mytitle) select a; var item=query.firstordefault(); if(item!=null) return view(item); else return view("notfound"); //let's show view item not found }
Comments
Post a Comment