ASP.NET-MVC Linq2Sql logic to get linked items on condition - html

I have a subcontract table with a company field. On the company page, I do not want the company to be able to be deleted if it is attached to an active subcontract. I am currently using the following expression to display the delete button. (Doesn't actually delete, just sets company to inactive.)
<% if (item.company1.subcontracts.Count == 0) { %>
This works for excluding all companies which are attached to subcontracts. However, my subcontract table also has an active_status field. What I really want is to be able to delete companies which are either not attached to a subcontract or are attached to an inactive subcontract (active_status == 0).

How about the following:
<% var subcontracts = item.company1.subcontracts;
if (subcontracts.Count == 0 || subcontracts.Any(x => x.active_status == 0)) { %>
This solves your problem if the active_status is accessible through subcontracts

Maybe I am misunderstanding you, but it seems adding just an OR to the IF should do the trick:
<% if (item.company1.subcontracts.Count == 0 || item.company1.active_status == 0) { %>

Related

how to create only one chip from the same word in Angular

I'm wondering if there is away to make my input to create only one chip from the same word instead of creating duplicate.
Right now user seem like they can create "apple" "APPLE" "apPPle" "aPpLe" so I'm figuring out a way not do that and always return with lowercase "apple" even if they type something with all Uppercase or Lowercase.
I just want to create only one chip for one word. any help or suggestion will be really appreciated.
I have tried all this but still not working
1. this.value = $event.target.value.toUpperCase()
this.ngModelChange.emit(this.value)
2. event.value=event.value.toLowerCase()
3. {{ value | lowercase }}
If I can fix this, the user might able to create "apple" "apple" multiple time but later I will prevent user from imputing the same word if "apple" is already existed/created.
Here is the project that is very similiar with mine
https://stackblitz.com/edit/angular-occkra
You need to add filtering logic to select and add methods:
if ((value || '').trim()) {
if (this.allFruits.includes(value.toLowerCase()) && !this.fruits.includes(value.toLowerCase())) {
this.fruits.push(value.trim().toLowerCase());
}
}
without include
if ((value || '').trim()) {
if (this.allFruits.find((f) => f.toLowerCase() === value.toLowerCase()) && !this.fruits.find((f) => f.toLowerCase() === value.toLowerCase())) {
this.fruits.push(value.trim().toLowerCase());
}
}

Get several values from option to assign to Request.Form[]

I need to store both the name and the id of the option the user chooses in my SQL Database. Right now I am only storing the Id, by setting the value of the option to the current Id in the iteration.
#foreach (var item in selection)
{
if (item.Id.ToString() == contentId)
{
//dont mind this, just for displaying purposes
}
else
{
<option value="#item.Id" #(Request.Form["førstePrio"] != null && Request.Form["førstePrio"].ToString().Equals(item.Name.ToString()) ? " selected" : "")>#item.Name</option>
}
}
How can I store the #item.Name value as well in a separate Request.Form?
Edit:
#{
var selection = Model.Content.Site().FirstChild("Sprog").FirstChild("sommerhuse").Children()
.Where(x => x.IsVisible());
}
At the moment, the item.id is the value of the selection.
What you'd actually want to receive on the surface controller at this point would be the item / model itself
There's an example of such a controller here;
https://our.umbraco.org/documentation/Reference/Templating/Mvc/forms#creating-the-surfacecontroller-action
So you would want to post the whole object to this controller. From there you would be able to get whatever properties you needed from the model, in order to save them to the DB.
It would be something like
<option value="#item" #(Request.Form["førstePrio"] != null && Request.Form["førstePrio"].ToString().Equals(item.Name.ToString()) ? " selected" : "")>#item.Name</option>

Rails, collection_check_boxes, maximum picked

I have a form which uses collection_check_boxes for an input.
= f.collection_check_boxes :color_ids, group.colors, :id, :name
I would like to prevent users from checking more than n boxes in the form.
I know how to do it in the model, I just need the views part. Thanks!
In the end, I decided to use jquery.
1 In the view I put the maximum amount at the end of the class like this:
= f.collection_check_boxes :color_ids, group.colors, :id, :name, item_wrapper_class: "bla bla2 #{group.maximum.to_s}"
2 When an input[type=checkbox] is clicked I find how many others are marked
$(this).parent().parent().siblings().children().children(':checked').length
3 I find the maximum allowed value from step 1 by the following line
max = parseInt($(this).parent().parent().attr('class').split(' ').pop());
4 I compare the maximum allowed with the checked and prevent from checking if the first one is less. The whole code looks like this
$('input[type=checkbox]').on('change', function(evt) {
var max;
max = parseInt($(this).parent().parent().attr('class').split(' ').pop());
if ($(this).parent().parent().siblings().children().children(':checked').length + 1 > max) {
this.checked = false;
}
});

Entity framework updates several records when FirstOrDefault() is used

I have a rather strange problem which is scratching my head at the moment. I am using EF together with MySQL for a project and when I want to update a record on the database like this
using (var context = new MyDbContext())
{
var record = (from d in context.Dictionary where d.CompanyName == companyName && d.Name == "Logo" select d).FirstOrDefault();
record.Value = _path;
context.SaveChanges();
}
Then every record that has d.Name == "Logo" gets updated for some reason, which means that it ignores the d.CompanyName == companyName part. Anyone who has experienced the same problem or know how to solve it?
Thanks in advance.
Actually I don't use EF Provider for MySQL often but I suppose it works.
The problem can be splitted in 2 parts, a select and an update. The select is fine, if you need to see what is the query you can split the select statement like this
var query = (from d in context.Dictionary where d.CompanyName == companyName && d.Name == "Logo" select d);
var record = query.FirstOrDefault();
and have a look to query variable (just point it in debug mode).
About your issue the only reason that I can immagine is that you did not configure the Dictionary class in the right way.
I mean, when you run context.SaveChanges an Update query is generated using the Key definition of Dictionary (the Where part of the query). If the key definition is wrong, the where clause will be wrong.
Use this I hope this will help you.
using (var context = new MyDbContext())
{
var record = context.Dictionary.Where(x=> x.CompanyName == companyName && x.Name == "Logo").FirstOrDefault();
record.Value = _path;
context.SaveChanges();
}

Is there a way I can check to see if the "go to page based on answer" is checked in Google Forms

When using .setChoices() It looks for an array of created choices. If it is a multiple choice question it can be formatted either createChoice(value) or createChoice(value, navigationType).
When looking at a choice you put in something like: var cPage = chkItem.getChoices()[j].getGotoPage();
This will produce either the page object or a Null value.
The problem is when a question is set to "go to page based on answer" and they have not set a page for every entry, and left the default to continue it also reads it (in my case cPage), as null. This means if I want to go through each choice to capture it, modify it, then push it back out to the question, it my ending Array that I push out consists of both Null and Objects, which produces an error.
My workaround for questions without pages is for the script to forcibly change the question to handle page navigation but set everyone to CONTINUE.
I would like to find a way to check if the question has "go to page based on answer" checked and if not then be able to create choices using just the value.
Figured it out: I need to check the first choice if it has a PageNavigationType(). If it does then the box is checked. If null then it is not checked. I have not written the code to fully test this but the theory should work. (I was checking against just the goToPage type which is why it didn't work before. )
var choicecount = chkItem.getChoices().length-1;
var hasNavType = chkItem.getChoices()[0].getPageNavigationType(); //if null
for (var j = 0; j <= choicecount; ++j) {
var cValue = chkItem.getChoices()[j].getValue();
var cPage = chkItem.getChoices()[j].getGotoPage();
var cNav = chkItem.getChoices()[j].getPageNavigationType();
// Logger.log(j+" "+cPage);
if (cValue != reValues[0]){
if (hasNavType == null){
newChoices.push(chkItem.createChoice(cValue));
} else {
if (cPage == null){
newChoices.push(chkItem.createChoice(cValue,cNav));
} else {
newChoices.push(chkItem.createChoice(cValue,cPage));
}
}
}
}