I may be going about this the wrong way but I’m selecting a list from the many side of a category which works fine but I also want the category name from the one side of the category. I’m trying to get that and put it into ViewBag below.
public ActionResult Search(int id)
{
//ViewBag.CoastName = …
var beaches = (from c in db.Beaches
where c.CoastLineFK == id
orderby c.BeachSuburb
select c);
return View(beaches);
}
If this is the/a correct way to do it, can someone help fill in the gaps here?
Edit:
Iv'e got it working like this,
public ActionResult Search(int id)
{
var metainfo = (from c in db.CoastLines
where c.CoastLineID == id
select c).First();
ViewBag.Title = metainfo.CoastLineName.ToString();
var beaches = (from c in db.Beaches
where c.CoastLineFK == id
orderby c.BeachSuburb
select c);
return View(beaches);
}
Is this a good way to do it, or is there a better way?
you are both right, I’ve only worked with Database first models in MVC so have been relying entirely on the modals generated automatically be the EF. but in the last week my world has been opened up to ViewModals. Now MVC is much easier and makes more
sense!
bojangles
Participant
974 Points
642 Posts
Selecting Category name from the one side while selecting a list from the many side
Apr 25, 2012 02:31 AM|LINK
Hi,
I may be going about this the wrong way but I’m selecting a list from the many side of a category which works fine but I also want the category name from the one side of the category. I’m trying to get that and put it into ViewBag below.
public ActionResult Search(int id) { //ViewBag.CoastName = … var beaches = (from c in db.Beaches where c.CoastLineFK == id orderby c.BeachSuburb select c); return View(beaches); }If this is the/a correct way to do it, can someone help fill in the gaps here?
Edit:
Iv'e got it working like this,
public ActionResult Search(int id) { var metainfo = (from c in db.CoastLines where c.CoastLineID == id select c).First(); ViewBag.Title = metainfo.CoastLineName.ToString(); var beaches = (from c in db.Beaches where c.CoastLineFK == id orderby c.BeachSuburb select c); return View(beaches); }Is this a good way to do it, or is there a better way?
Cheers,
Mike.
ossprologix
Member
392 Points
127 Posts
Re: Selecting Category name from the one side while selecting a list from the many side
Apr 27, 2012 11:37 AM|LINK
use smaller models instead
public class BeachModel { public string Title {get;set;} public IEnumerable<string> beaches {get;set;} }it's a good practice
bojangles
Participant
974 Points
642 Posts
Re: Selecting Category name from the one side while selecting a list from the many side
Apr 27, 2012 11:56 PM|LINK
Thanks, but i'm using database first so the modal is generated by the EF.
Young Yang -...
All-Star
21344 Points
1818 Posts
Microsoft
Re: Selecting Category name from the one side while selecting a list from the many side
May 02, 2012 02:08 AM|LINK
Hi
You can put the ConastLines and Beaches into a ViewModel. Like this:
public class TitleViewModel { public IEnumerable<Beache> Beaches { get; set; } public IEnumerable<ConastLine> ConastLines { get; set; } }Hope this helpful
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store
bojangles
Participant
974 Points
642 Posts
Re: Selecting Category name from the one side while selecting a list from the many side
May 03, 2012 05:54 AM|LINK
Thanks Guys,
you are both right, I’ve only worked with Database first models in MVC so have been relying entirely on the modals generated automatically be the EF. but in the last week my world has been opened up to ViewModals. Now MVC is much easier and makes more sense!
Thanks again,
Mike.