Entity framework in MVC 4 - Include returning false result - entity-framework

I have two tables:
Products:
ProductID, ProductName
Compounds:
CompoundID, ComponentID, Qty
bound as next:
1 Products.ProductID --> many Compounds.CompoundID
1 Products.ProductID --> many Compounds.ComponentID
A Products item can be a compound, a components, or both.
I need that a view returns for each compound its components names and Quatities.
MyController method :
public ActionResult CompoundProduct()
{
var Compounds = db.Compounds.Include(s => s.Products);
return View(Compounds.ToList());
}
MyView code :
#model IEnumerable
<table>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.CompoundID)
</td>
<td>
#Html.DisplayFor(modelItem => item.ComponentID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Products.ProductName)
</td>
</tr>
}
</table>
With this method I get a list of repeated Compound name instead of distinct Component names per Compound as shown in the image below:
any help please ?

When you created the model from the database EF should have created two navigation properties Products and Products1 related to the two foreign keys CompoundID and ComponentID. You are probably using the wrong navigation property that belongs to CompoundID, not the one that belongs to ComponentID. Instead of Products try to use Products1 in the Include...
var Compounds = db.Compounds.Include(s => s.Products1);
...and in the view:
#Html.DisplayFor(modelItem => item.Products1.ProductName)

Related

How can I extract an attribute from multiple elements in Playwright?

I'm trying to extract some attributes from multiple elements using playwright.
( this is F# but you should be able to figure out what is wrong )
let! pagelinkRows = page.EvalOnSelectorAllAsync("tr[title]","(tr) => tr.getAttribute("title")") |> Async.AwaitTask
I get the error: tr.getAttribute is not a function. I have also tried tr.attr .
What is the simplest way to get the attributes from a collection of elements
<tr title="xxx1"></tr>
<tr title="xxx2"></tr>
<tr title="xxx3"></tr>
<tr title="xxx4"></tr>

LINQ expression to get the whole content of right table

In my database, I have these two tables with a relationship :
I would like to get the whole content of the "assignee_kanban" column in "assigneekanban" table (which for now contains six names).
Here is the expression I use (in my "Kanban" controller using entity framework) :
public List<Kanban> LoadCard() {
return _context.Kanbans.Include(c => c.IdAssigneeKanbanNavigation).ToList();}
When I try to get the result in my razor page with :
#foreach (var name in Kanbans)
{
<tr>
<td>#name.IdAssigneeKanbanNavigation.AssigneeKanban1</td>
</tr>
}
unfortunatly, the result I get contains only three names : the three names corresponding to the three "id_assignee_kanban" that exist in the "kanban" table.
How can I get all the names contained in the "assignee_kanban" column ?
(I succeed to get the values using assigneekanban controller but for some reason, I want to use kanban controller with the navigation statement)
(project webassembly blazor using .net 6 aspnet hosted)
Thank you for your help.
Here is the content of the "kanban" table (I removed some no important columns for this problem) :
Here is the content of the "assigneekanban" table :
Here is the output :
As we can see, the name "Robert" corresponding to the id n°5 doesn't appaer in the result and the other names appear as many time they exist in the kanban table (of course, I can add a distinct close to avoid duplicate names but it doesn't bring the missing name !).
You need a second query to get all entries from the assigneekanban table. Your current implementation will only get those values from the assigneekanban that have a foreign key entry in the kanban table (Basically doing an INNER JOIN).
public async Task<List<Kanban>> LoadCardAsync()
{
return _context.Kanbans.ToList();
}
public async Task<List<AssigneeKanban>> LoadAssigneesAsync()
{
return _context.AssigneeKanbans.ToList();
}
public async Task LoadEverythingAsync()
{
Kanbans = await LoadCardAsync();
Assignees = await LoadAssigneesAsync();
}
Then in your Razor Page
#foreach (var assignee in Assignees)
{
<tr>
<td>#assignee.AssigneeKanban1</td>
</tr>
}

jQuery complex selector : Hierarchy string variable

assume in #main div the table have id foo_userId which represent contents of user have id userId
I have to select table with userId 1234 with given id as variable
<div id="main">
<table id="foo_1234_m"></table> //select this
<table id="f0006458_m"></table>
<table id="f0001234_n"></table>
<table id="foo_9999_k"></table>
</div>
$('div#main>table#foo_1234_m')
// foo : string , 1234 : variable , m variable
//////////i'm trying something like :
var userId='1234'
var tableID='foo_' + userId // from this
$("div#main>table[id^='foo_1234']") // how to make this
It appears you have the correct selector syntax. So you can write the jQuery selector using the same string concatenation as in your example, e.g.:
var userId = '1234'
var tableId = 'foo_' + userId
var element = $("div#main>table[id^='" + tableId + "']")

Web2py orderby, limitby

My database layer (MongoDB):
db.define_table('news',
Field('title', label='Title'),
Field('link', label='Link'),
Field('date', label='Date'),
Field('summary',label='Summary'))
My controller:
def news():
if len(request.args): page=int(request.args[0])
else: page=0
items_per_page=5
limitby=(page*items_per_page,(page+1)*items_per_page+1)
orderby="~date"
qset = db(db['news'])
grid = qset.select(orderby=orderby, limitby=limitby)
return dict(grid=grid,page=page,items_per_page=items_per_page)
My view:
<table class="news-stories">
<th>Date</th>
<th>Title</th>
<th>Summary</th>
{{for i, row in enumerate(grid):}}
{{if i==items_per_page:break}}
<tr>
<td>{{=row.date.strftime("%m/%d/%Y")}}</td>
<td>{{=row.title}}</td>
<td>{{=row.summary}}</td>
</tr>
{{pass}}
</table>
{{if page:}}
previous
{{pass}}
{{if len(grid)>items_per_page:}}
next
{{pass}}
When I set orderby="~date" or orderby="date desc", as in the example above, my dates are not ordered appropriately in desc order; some dates in November come after dates in December (and vice-versa). However, when I set orderby="date", the dates are ordered perfectly in asc order. Am I missing something here?
Dates are inserted as datetime.datetime.strptime(date,"%m/%d/%Y").
It should be:
orderby = ~db.news.date
See details in the documentation.

Loop over the Mongodb objects to get the values in Meteorjs

I am first Time working With Meteorjs and MongoDB. I am working on a Q&A application. where I have a list of objects which i got using Meteor database call. I want to looper over the list and want to split one of its attribute to the length 50 and then want to send a list of these splitted objects to the database.what I have is this
Template.questions.questions = function () {
questions= Meteor.questions.find({topic_id: Session.get("currentTopicId")});
return questions
}
Every question object in the questions list has an attribute question_text. Using loop i want to split this attribute to length of fifty and then want to push that to an empty list and return that list . Like
questions_list=[]
for(i=0;i<questions.length;i++){
question=questions[i].question_text[0:50] // python syntex to get first 50 char of a string
questions_list.push(question
}
return questions_list
and My HTML is like
<tbody>
{{#each questions}}
<tr>
<td class="span2" style="overflow:hidden;width:50px">
{{question_text}}
</td>
</tr>
{{/each}}
</tbody>
suggest me guys how can i acieve this in meteorjs. my problem is when i try to loop over this questions list there are many attributes like Collection, Results, Queries. here i am unable to iterate this list of objects.
In the same way how can i get the error message thrown back by meteorjs
This will give you list of shortened texts of all questions matching a query:
var questions = Questions.find({...}).map(function(question) {
return question.text.substr(0,50);
});
You could use it directly in a helper:
Template.questions.questions = function () {
return Questions.find({...}).map(function(question) {
return question.text.substr(0,50);
});
};
By the way, Questions.find({...}) does not return a list of questions, but a cursor object that you can use to manipulate query data in an effective way (like that map method above). To get raw array, you need to use fetch on that cursor: Questions.find({...}).fetch().
If you're just displaying the data you could create a handlebars helper:
Template.questions.helpers({
first50: function(question) {
return question.text.substr(0,50);
}
});
Then add the helper:
Template.questions.questions = function() {
return Questions.find({...});
};
Then in your template you could do:
<tbody>
{{#each questions}}
<tr>
<td class="span2" style="overflow:hidden;width:50px">
{{first50 question_text}}
</td>
</tr>
{{/each}}
</tbody>