Note. Before reading this article, please read the first part of this series Combining Blazor WebAssembly And .Net MVC Application here: Combining Blazor WebAssembly And .Net MVC Application - Part 1.
Introduction
In this article, we will continue our journey of combining Blazor WebAssembly and .NET MVC applications in a single project. This is part 2 of a series of articles that explains how to use Blazor components in MVC views and vice versa. If you haven’t read part 1 yet, I recommend you to do so before continuing.
In part 1, we have seen how to create a new Blazor WebAssembly project and add it as a reference to our existing MVC project. We have also seen how to configure our MVC project to serve the Blazor files.
In this part, we will use the Blazor WebAssembly component in the .NET MVC application to create a simple CRUD data application. Continue to use the project in part 1 and the existing FetchData component in the Blazor WebAssembly project.
Add the component to the MVC application to show the list
First, create the Models folder in the Blazor WebAssemply project, then move the WeatherForecast class in the FetchData component to the folder. Add the Id field in the WeatherForecast class.
namespace BlazorApp.Models
{
public class WeatherForecast
{
public string? Id { get; set; }
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}
Then, in the HomeController.cs in the MVC project, add some code to the simulation data service.
public class HomeController : Controller
{
// Simulation data service
private static readonly List<WeatherForecast> weatherForecasts = new()
{
new WeatherForecast { Id = Guid.NewGuid().ToString(), Date = DateOnly.Parse("2022-01-06"), TemperatureC = 1, Summary = "Freezing" },
new WeatherForecast { Id = Guid.NewGuid().ToString(), Date = DateOnly.Parse("2022-01-07"), TemperatureC = 14, Summary = "Bracing" },
new WeatherForecast { Id = Guid.NewGuid().ToString(), Date = DateOnly.Parse("2022-01-08"), TemperatureC = -13, Summary = "Freezing" },
new WeatherForecast { Id = Guid.NewGuid().ToString(), Date = DateOnly.Parse("2022-01-09"), TemperatureC = -16, Summary = "Balmy" },
new WeatherForecast { Id = Guid.NewGuid().ToString(), Date = DateOnly.Parse("2022-01-10"), TemperatureC = -2, Summary = "Chilly" }
};
// The API to return data
public IActionResult WeatherForecasts()
{
return Json(weatherForecasts);
}
}
In the view Index.cshtml, replace code to like this.
<component type="typeof(BlazorApp.Pages.FetchData)" render-mode="WebAssemblyPrerendered"></component>
@section Scripts {
<script src="_framework/blazor.webassembly.js"></script>
}
Add the HttpClient to the .NET MVC application by this code in the Program.cs file.
//add httpclient
builder.Services.AddScoped(sp => new HttpClient());
This is because the FetchData component uses HttpClient.
@inject HttpClient Http
so we need to add the HttpClient to the .NET MVC project because when the .NET MVC application pre-renders the FetchData component, it must create a HttpClient instance and inject to the component.
Last, in the FetchData component in Blazor WebAssembly, replace the OnInitializedAsync method with this.
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("Home/WeatherForecasts");
// Force view update
StateHasChanged();
}
}
Run the .NET MVC project and see the result.

Add the Create function
In the .NET MVC project, add create action in HomeController as a create api.
[HttpPost]
public IActionResult WeatherForecasts([FromBody] WeatherForecast model)
{
if (ModelState.IsValid)
{
model.Id = Guid.NewGuid().ToString();
weatherForecasts.Add(model);
}
return Json(weatherForecasts);
}
Change the FetchData component like this to add the Create method on view.
@using BlazorApp.Models
@inject HttpClient Http
<PageTitle>Weather Forecast</PageTitle>
<h1>Weather Forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<EditForm Model="@createModel" OnValidSubmit="OnCreateFormSubmit" id="form-create">
<DataAnnotationsValidator />
<ValidationSummary />
</EditForm>
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<InputDate class="form-control" @bind-Value="@createModel.Date" form="form-create"></InputDate>
</td>
<td>
<InputNumber class="form-control" @bind-Value="@createModel.TemperatureC" form="form-create"></InputNumber>
</td>
<td>
@createModel.TemperatureF
</td>
<td>
<InputText class="form-control" @bind-Value="@createModel.Summary" form="form-create"></InputText>
</td>
<td>
<button type="submit" class="btn btn-primary" form="form-create">Add</button>
</td>
</tr>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
<td></td>
</tr>
}
</tbody>
</table>
}
@code {
private WeatherForecast[]? forecasts;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("Home/WeatherForecasts");
// Force view update
StateHasChanged();
}
}
WeatherForecast createModel = new();
async Task OnCreateFormSubmit()
{
using var createReq = await Http.PostAsJsonAsync("Home/WeatherForecasts", createModel);
createReq.EnsureSuccessStatusCode();
forecasts = await createReq.Content.ReadFromJsonAsync<WeatherForecast[]>();
}
}
Save the file, then run the .NET MVC application.

The Edit function
Add the Edit function is the same as the Create function. First, add the api action in HomeController.
[HttpPut]
public IActionResult WeatherForecasts(string id, [FromBody] WeatherForecast model)
{
if (ModelState.IsValid)
{
var weatherForecast = weatherForecasts.FirstOrDefault(x => x.Id == id);
if (weatherForecast != null)
{
weatherForecast.Date = model.Date;
weatherForecast.TemperatureC = model.TemperatureC;
weatherForecast.Summary = model.Summary;
}
}
return Json(weatherForecasts);
}
In the WeatherForecast class, add the IsEditting field to mark the editing entity.
public bool IsEditting { get; set; }
Then edit the FetchData component like this.



RondinelePosted Dec 1, 2023, 5:28 PM
Can you share this project?