EnderWolf
EnderWolf
CC#
Created by EnderWolf on 1/21/2024 in #help
✅ MVC to Blazor
Hello! so awhile ago, I made an mvc. I am now attempting to port the code over into a new blazor project but am unsure on how to do this, as with the mvc I had 1 view called Events, which would get opened via clicking a button in a drop down menu that would open the view with different data based on which drop down button you clicked. but with Blazor, through all my research I don't think it is possible to return a page as such. here is some of my code from the mvc, any help would be greatly apricated! The event controller:
public async Task<IActionResult> Index(int id)
{
List<Event> events = _context.Event.AsNoTracking()
.Where(c => c.EventTribe == id && c.EventStatus == 1 || c.EventTribe == 11 && c.EventStatus == 1)
.OrderBy(c => c.EventYear)
.ToList();

if (events == null)
{
return RedirectToAction("Index", "Home");
}

return View(events);
}
public async Task<IActionResult> Index(int id)
{
List<Event> events = _context.Event.AsNoTracking()
.Where(c => c.EventTribe == id && c.EventStatus == 1 || c.EventTribe == 11 && c.EventStatus == 1)
.OrderBy(c => c.EventYear)
.ToList();

if (events == null)
{
return RedirectToAction("Index", "Home");
}

return View(events);
}
16 replies
CC#
Created by EnderWolf on 12/7/2023 in #help
✅ Nested views failing
Hello! so, I've got a small issue. I have this view called Edit which has the following button:
<div class="form-group">
<input type="submit" id="SaveEdit" value="Save" class="btn btn-primary" />
</div>
<div class="form-group">
<input type="submit" id="SaveEdit" value="Save" class="btn btn-primary" />
</div>
I am trying to get it so that when this button is clicked, it takes the user back to having the details view opened in this part of the dashboard view (note the page with the submit button is currently opened in there)
<div class="dash-content col py-3" id="contentArea">

</div>
<div class="dash-content col py-3" id="contentArea">

</div>
this is my current js
$('#SaveEdit').click(function() {
preventDefault(); // Prevent the default action of the link
var submissionId = $(this).attr('value'); // Get the ID of the submission
$.ajax({
url: '/Submission/Edit/' + submissionId,
type: 'GET',
success: function(result) {
$('#contentArea').html(result);
}
});
});
$('#SaveEdit').click(function() {
preventDefault(); // Prevent the default action of the link
var submissionId = $(this).attr('value'); // Get the ID of the submission
$.ajax({
url: '/Submission/Edit/' + submissionId,
type: 'GET',
success: function(result) {
$('#contentArea').html(result);
}
});
});
here's the end point it hits:
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Edit(int id, Event @event)
{
if (id != @event.EventId)
{
return NotFound();
}
ModelState.Remove("");
if (ModelState.IsValid)
{
try
{
var @submission = await _context.Event.FirstOrDefaultAsync(m => m.EventId == id);
@submission.EventEditedDate = @event.EventEditedDate;
_context.Update(@submission);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!EventExists(@event.EventId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction("Details", new { id = id });
}
return RedirectToAction("Details", new { id = id });
}
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Edit(int id, Event @event)
{
if (id != @event.EventId)
{
return NotFound();
}
ModelState.Remove("");
if (ModelState.IsValid)
{
try
{
var @submission = await _context.Event.FirstOrDefaultAsync(m => m.EventId == id);
@submission.EventEditedDate = @event.EventEditedDate;
_context.Update(@submission);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!EventExists(@event.EventId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction("Details", new { id = id });
}
return RedirectToAction("Details", new { id = id });
}
and this is the Details end point:
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Edit(int? id)
{
if (id == null || _context.Event == null)
{
return NotFound();
}
var @event = await _context.Event.FindAsync(id);
if (@event == null)
{
return NotFound();
}
return View(@event);
}
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Edit(int? id)
{
if (id == null || _context.Event == null)
{
return NotFound();
}
var @event = await _context.Event.FindAsync(id);
if (@event == null)
{
return NotFound();
}
return View(@event);
}
I have some almost identical code that works fine, but this one seems to just open the view as a whole view instead of nested inside the first
51 replies
CC#
Created by EnderWolf on 12/3/2023 in #help
mvc not finding blazor item, 404 not found
Hello! so, I've got a question if you don't mind answering. I made an MVC in .net 6 which I then later updated to .net 8. a few days ago I added a new blazor page to it, which works completely fine. they can be rendered inside views/other pages completely fine. however, I'm now trying to get it so that another blazor page opens when a button is clicked. this seems to just give me a 404 error the page that's open is:Progect\Components\Dash.razorthe page I am trying to open is:Progect\Components\Counter.razor
@inject NavigationManager NavigationManager
@using Project.Components

<div class="container-fluid">
<div class="row flex-nowrap">
class="col-auto col-md-3 col-xl-2 px-sm-2 px-0 bg-dark">
<div class="d-flex flex-column align-items-center align-items-sm-start px-3 pt-2 text-white min-vh-100">
<a href="/" class="d-flex align-items-center pb-3 mb-md-0 me-md-auto text-white text-decoration-none">
<span class="fs-5 d-none d-sm-inline">Menu</span>
</a>

<ul class="nav nav-pills flex-column mb-sm-auto mb-0 align-items-center align-items-sm-start" id="menu">
<li class="nav-item">
<a href="#" class="nav-link align-middle px-0">
<i class="fs-4 bi-house"></i> <span class="ms-1 d-none d-sm-inline">Home</span>
</a>
</li>
<li>
<a href="#" class=" px-0 align-middle">
<i class="fs-4 bi-table"></i> <span @onclick="NavigateToCounter" class="ms-1 d-none d-sm-inline">View Submissions</span>
</a>
</li>
</ul>
</div>
</div>
<div class="col py-3">
<a class="nav-link" asp-controller="Submission" asp-action="Index">View Submissions</a>
Content area...
</div>
</div>
</div>
@code {
private void NavigateToCounter()
{
NavigationManager.NavigateTo("/counter", forceLoad: true);
}
}
@inject NavigationManager NavigationManager
@using Project.Components

<div class="container-fluid">
<div class="row flex-nowrap">
class="col-auto col-md-3 col-xl-2 px-sm-2 px-0 bg-dark">
<div class="d-flex flex-column align-items-center align-items-sm-start px-3 pt-2 text-white min-vh-100">
<a href="/" class="d-flex align-items-center pb-3 mb-md-0 me-md-auto text-white text-decoration-none">
<span class="fs-5 d-none d-sm-inline">Menu</span>
</a>

<ul class="nav nav-pills flex-column mb-sm-auto mb-0 align-items-center align-items-sm-start" id="menu">
<li class="nav-item">
<a href="#" class="nav-link align-middle px-0">
<i class="fs-4 bi-house"></i> <span class="ms-1 d-none d-sm-inline">Home</span>
</a>
</li>
<li>
<a href="#" class=" px-0 align-middle">
<i class="fs-4 bi-table"></i> <span @onclick="NavigateToCounter" class="ms-1 d-none d-sm-inline">View Submissions</span>
</a>
</li>
</ul>
</div>
</div>
<div class="col py-3">
<a class="nav-link" asp-controller="Submission" asp-action="Index">View Submissions</a>
Content area...
</div>
</div>
</div>
@code {
private void NavigateToCounter()
{
NavigationManager.NavigateTo("/counter", forceLoad: true);
}
}
The page to open
@page "/counter"
<h1>Counter</h1>
<p role="status">CurrentCount: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click to increment</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
@page "/counter"
<h1>Counter</h1>
<p role="status">CurrentCount: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click to increment</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
3 replies