Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq - Referencing a Child Table
    primarykey
    data
    text
    <p>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.</p> <p>I am using a model, goalsModel, to retrieve my data:</p> <p><code>public goals GetGoal(int id) { return qDB.goals.Single(g =&gt; g.goalID == id); } </code></p> <p>I'd like Activities to be available as well... where I could do something like:</p> <p><code>public goals GetGoalActivities(int id) { return qDB.ilpActivities.Where(g =&gt; g.goalID == id); }</code></p> <p>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.</p> <p>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: <a href="http://geekswithblogs.net/michelotti/archive/2007/12/30/118076.aspx" rel="nofollow">http://geekswithblogs.net/michelotti/archive/2007/12/30/118076.aspx</a></p> <p>[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.</p> <p>[Update]</p> <p>My tables look something like this (simplified to include relevant fields): goals: goalID, goalName, goalDescr ilpActivities: activityID, goalID, activityName</p> <p>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</p> <p>I have a goalsModel.cs file:</p> <pre><code>namespace ILP.Models { public class goalsModel { #region services public interface IGoalsService { List&lt;goals&gt; GetAllGoals(string oprID); bool CreateGoal(goals poll); List&lt;ilpGoalStatus&gt; GetStatuses(); goals GetGoal(int id); bool EditGoal(goals poll); List&lt;ilpActivity&gt; GetGoalActivities(goals goal); } public class AssetService : IGoalsService { private goalsDataClassesDataContext qDB; public AssetService() { qDB = new goalsDataClassesDataContext(); } #region IGoalsService Members public List&lt;ilpActivity&gt; GetGoalActivities (goals goal) { IEnumerable&lt;ilpActivity&gt; 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(); } </code></pre> <p>In my controller:</p> <pre><code>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(); } </code></pre> <p>In my create activity partialview I'm only getting access to the goals model:</p> <pre><code>@model ILP.Models.goals &lt;script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Create an Activity&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.goalName) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.goalName) @Html.ValidationMessageFor(model =&gt; model.goalName) &lt;/div&gt; .... etc.... </code></pre> <p>In my view I need to access activityName of ilpActivities - but the only options are coming from goals table. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload