Bread
Bread
Explore posts from servers
CC#
Created by Bread on 12/22/2022 in #help
❔ Add a new item to a models collection in razor form
This is the code I have:
<div id="items">
@for (var i = 0; i < Model.ImageHighlights.Count; i++)
{
<div class="form-group">
<label asp-for="@Model.ImageHighlights[i].Title"></label>
<input maxlength="250" asp-for="@Model.ImageHighlights[i].Title" class="form-control bg-peach-light" required type="text"/>
<span asp-validation-for="@Model.ImageHighlights[i].Title" class="form-text text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.ImageHighlights[i].Description"></label>
<textarea maxlength="250" asp-for="@Model.ImageHighlights[i].Description" class="form-control bg-peach-light" required type="text"></textarea>
<span asp-validation-for="@Model.ImageHighlights[i].Description" class="form-text text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.ImageHighlights[i].ImageSource"></label>
<textarea maxlength="250" asp-for="@Model.ImageHighlights[i].ImageSource" class="form-control bg-peach-light" required type="file"></textarea>
<span asp-validation-for="@Model.ImageHighlights[i].ImageSource" class="form-text text-danger"></span>
</div>
}
</div>

<button type="button" onclick="addItem()">Add new Feature</button>

<script>
function addItem() {
var itemsDiv = document.getElementById('items');
var newDiv = document.createElement('div');
var index = itemsDiv.childElementCount;
newDiv.innerHTML = '<div class="form-group"><label for="title_' + index + '">Title:</label><textarea id="title_' + index + '" maxlength="250" name="Title" class="form-control bg-peach-light" required type="text"></textarea><span class="form-text text-danger"></span></div>';
itemsDiv.appendChild(newDiv);
}
</script>
<div id="items">
@for (var i = 0; i < Model.ImageHighlights.Count; i++)
{
<div class="form-group">
<label asp-for="@Model.ImageHighlights[i].Title"></label>
<input maxlength="250" asp-for="@Model.ImageHighlights[i].Title" class="form-control bg-peach-light" required type="text"/>
<span asp-validation-for="@Model.ImageHighlights[i].Title" class="form-text text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.ImageHighlights[i].Description"></label>
<textarea maxlength="250" asp-for="@Model.ImageHighlights[i].Description" class="form-control bg-peach-light" required type="text"></textarea>
<span asp-validation-for="@Model.ImageHighlights[i].Description" class="form-text text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.ImageHighlights[i].ImageSource"></label>
<textarea maxlength="250" asp-for="@Model.ImageHighlights[i].ImageSource" class="form-control bg-peach-light" required type="file"></textarea>
<span asp-validation-for="@Model.ImageHighlights[i].ImageSource" class="form-text text-danger"></span>
</div>
}
</div>

<button type="button" onclick="addItem()">Add new Feature</button>

<script>
function addItem() {
var itemsDiv = document.getElementById('items');
var newDiv = document.createElement('div');
var index = itemsDiv.childElementCount;
newDiv.innerHTML = '<div class="form-group"><label for="title_' + index + '">Title:</label><textarea id="title_' + index + '" maxlength="250" name="Title" class="form-control bg-peach-light" required type="text"></textarea><span class="form-text text-danger"></span></div>';
itemsDiv.appendChild(newDiv);
}
</script>
The javascript here for adding an item feels wrong. Is there any other alternative ways of allowing the user to "add an item" to the form?
2 replies
CC#
Created by Bread on 11/12/2022 in #help
❔ EF Core duplicating many-to-many?
After saving, the references are duplicated!
var entityShowcase = new Models.Showcase
{
AuthorId = showcase.AuthorId,
Description = showcase.Description,
Title = showcase.Description,
Summary = showcase.Summary,
ImageSource = showcase.ImageSource,
MajorVersion = showcase.MajorVersion,
MinorVersion = showcase.MinorVersion,
PatchVersion = showcase.PatchVersion,
};

foreach (var feature in showcase.Features)
{
var dbFeatures = _context.Feature.FirstOrDefault(x => x.Value == feature);
if (dbFeatures is not null)
{
entityShowcase.Features.Add(dbFeatures);
continue;
}

entityShowcase.Features.Add(new Feature { Value = feature });
}

// after the loop above, enitityShowcase features is 2 (as it should be)

var entityEntry = await _context.AddAsync(entityShowcase, cancellationToken);
// after adding, it's actually 4...

await _context.SaveChangesAsync(cancellationToken);
return entityEntry.Entity.Id;
var entityShowcase = new Models.Showcase
{
AuthorId = showcase.AuthorId,
Description = showcase.Description,
Title = showcase.Description,
Summary = showcase.Summary,
ImageSource = showcase.ImageSource,
MajorVersion = showcase.MajorVersion,
MinorVersion = showcase.MinorVersion,
PatchVersion = showcase.PatchVersion,
};

foreach (var feature in showcase.Features)
{
var dbFeatures = _context.Feature.FirstOrDefault(x => x.Value == feature);
if (dbFeatures is not null)
{
entityShowcase.Features.Add(dbFeatures);
continue;
}

entityShowcase.Features.Add(new Feature { Value = feature });
}

// after the loop above, enitityShowcase features is 2 (as it should be)

var entityEntry = await _context.AddAsync(entityShowcase, cancellationToken);
// after adding, it's actually 4...

await _context.SaveChangesAsync(cancellationToken);
return entityEntry.Entity.Id;
4 replies
CC#
Created by Bread on 9/29/2022 in #help
Possible to 'intercept' a successful sign in attempt with .net core identity?
I was curious if it was possible to add some middleware that would detect if someone has signed in successfully with Identity, I'd like to raise my own events and handle them as I need
7 replies
CC#
Created by Bread on 9/8/2022 in #help
Get longest consecutive date 'streak' [Answered]
I'm trying to figure out a nice way of getting a date-streak from a list of objects which will have activities on them. The premise to the problem is, each user records their 'excercise' which can be multiples per day. I don't care about anything other than some acceptence criteria (namely, duration of the exercise) but I can group these effectively into some structure I assume of:
[date] : [true/false]
[date] : [true/false]
This would indicate if the user has completed an activity on that date. Where I'm struggling is to then calculate the streak of dates (the largest streak) in that full list, ie:
1st : true
2nd : false
3rd : true
4th : true
5th : false
6th : true
7th : false
1st : true
2nd : false
3rd : true
4th : true
5th : false
6th : true
7th : false
Here the users top 'streak' is going to be 2, as completed on the 3rd and 4th
18 replies
CC#
Created by Bread on 8/20/2022 in #help
How do I 'build' a QUERY with EF Core?
18 replies