I have a few tables I’m dealing with regarding a “Goals” application (screen grab is below). I’m using MVC 3, razor and linq.
I am using a model, goalsModel, to retrieve my data:
public goals GetGoal(int id)
{
return qDB.goals.Single(g => g.goalID == id);
}
I’d like Activities to be available as well… where I could do something like:
public goals GetGoalActivities(int id)
{
return qDB.ilpActivities.Where(g => g.goalID == id);
}
However this returns an error. The smart tags for don’t show any columns available for ilpActivities. I’ve seen several methods of writing code in the controller where it’s a linq query – but I’d really like to use the format above if possible for consistency.
Here’s a link that I’ve been trying to follow that may do what I want. Keep in mind, however, I’ll eventually be editing existing activities, creating new ones, and deleting activities. The link: http://geekswithblogs.net/michelotti/archive/2007/12/30/118076.aspx
[Edit]
I couldn’t post an image as a new user. My tables consist of goalID as the primary key of goals and foreign key in ilpActivities. ilpActivities has a primary key of activityID.
[Update]
My tables look something like this (simplified to include relevant fields):
goals: goalID, goalName, goalDescr
ilpActivities: activityID, goalID, activityName
I have a dbml file: employeeDataClasses.dbml that was generated for me after dragging the tables in on design mode. It recognized ilpActivities as a child of goals
I have a goalsModel.cs file:
namespace ILP.Models
{
public class goalsModel
{
#region services
public interface IGoalsService
{
List<goals> GetAllGoals(string oprID);
bool CreateGoal(goals poll);
List<ilpGoalStatus> GetStatuses();
goals GetGoal(int id);
bool EditGoal(goals poll);
List<ilpActivity> GetGoalActivities(goals goal);
}
public class AssetService : IGoalsService
{
private goalsDataClassesDataContext qDB;
public AssetService()
{
qDB = new goalsDataClassesDataContext();
}
#region IGoalsService Members
public List<ilpActivity> GetGoalActivities (goals goal)
{
IEnumerable<ilpActivity> activities = from g in qDB.goals
join a in qDB.ilpActivities on g.goalID equals a.goalID
where a.goalID == goal.goalID
select a;
return activities.ToList();
}
In my controller:
namespace ILP.Controllers
{
public class HomeController : Controller
{
private goalsModel.IGoalsService qService;
public HomeController()
{
qService = new goalsModel.AssetService();
}
[Authorize]
public ActionResult _CreateActivity(int goalID)
{
//var status = qService.GetStatuses();
ViewBag.Status = new SelectList(qService.GetStatuses().ToList(), "goalStatusID", "goalStatus");
// not sure what to do here to get activity model in....
return PartialView();
}
In my create activity partialview I’m only getting access to the goals model:
@model ILP.Models.goals
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Create an Activity</legend>
<div class="editor-label">
@Html.LabelFor(model => model.goalName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.goalName)
@Html.ValidationMessageFor(model => model.goalName)
</div>
.... etc....
In my view I need to access activityName of ilpActivities – but the only options are coming from goals table.
Can you change:
to:
because
qDB.ilpActivities.Where(g => g.goalID == id);returns an IEnumerable of activites, not a single goal like the previous method.