Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now

Modal Popup Window In Blazor with Parameters

  In this article, we are going to discuss, how to add a model popup in a blazer and we will see how to pass the parameters in a popup window.

Create a new Blazor WebAsssembly project

Here we are going to make this application using VS 2022, so open VS 2022 and create a new project




Now let's give the project Name 'BlazorModelPopup' and click Next



Now give some additional info, as per your application, and click on Create.




Installing NuGet Package

 
Once the application is ready to use so now go to the NuGet package manager and install the Blazored.Modal library 
  1. Install-Package Blazored.Modal  

Search for the above library and click on install


Now we can see library installed successfully 



Register services

Now we need to register the BlazoredModal service in Program class like below.
  1. builder.Services.AddBlazoredModal();   


Add Imports
 
Add the following in _Imports.razor file
  1. @using Blazored  
  2. @using Blazored.Modal  
  3. @using Blazored.Modal.Services 



Add CascadingBlazoredModal Component

Add the <CascadingBlazoredModal/> component into our applications App.razor, 
  1. <CascadingBlazoredModal> 
  2.     <Router AppAssembly="@typeof(App).Assembly">
  3.     <Found Context="routeData">
  4.         <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
  5.         <FocusOnNavigate RouteData="@routeData" Selector="h1" />
  6.     </Found>
  7.     <NotFound>
  8.         <PageTitle>Not found</PageTitle>
  9.         <LayoutView Layout="@typeof(MainLayout)">
  10.             <p role="alert">Sorry, there's nothing at this address.</p>
  11.         </LayoutView>
  12.     </NotFound>
  13. </Router>
  14. </CascadingBlazoredModal>    

Add reference to the style sheet and JavaScript reference

Add the following CSS line in the head tag of our index.html (inside the wwwRoot folder)
  1. <link href="_content/Blazored.Modal/blazored-modal.css"rel="stylesheet"/>   
Now add the Js reference to the Blazored Modal JavaScript file in the head tag of our index.html (inside the wwwRoot folder).
  1. <script src="_content/Blazored.Modal/blazored.modal.js"></script>   


Creating a Component

First, let's create a component called ''userPopUp", and put the code below

  1. @page "/userPopUp"

  2. <h3>UserPopUp</h3>

  3. @code {

  4. }


Now utilize FetchData.razor existing component 


When we create a Blazor web assembly project it gives us a couple of pages, and one of them is FetchData.razor
so in the existing page let's add a button called 'Popup', and the FetchData.razor code like below

from this newly added button, we are calling the 'userPopUp' razor component page a popup, with the help of service.Show like below

here service is the object of IModalService 

  1. @page "/fetchdata"
  2. @inject HttpClient Http
  3. @inject IModalService service


  4. <PageTitle>Weather forecast</PageTitle>

  5. <h1>Weather forecast</h1>

  6. <p>This component demonstrates fetching data from the server.</p>

  7. @if (forecasts == null)
  8. {
  9.     <p><em>Loading...</em></p>
  10. }
  11. else
  12. {
  13.     <table class="table">
  14.         <thead>
  15.             <tr>
  16.                 <th>Date</th>
  17.                 <th>Temp. (C)</th>
  18.                 <th>Temp. (F)</th>
  19.                 <th>Summary</th>
  20.             </tr>
  21.         </thead>
  22.         <tbody>
  23.             @foreach (var forecast in forecasts)
  24.             {
  25.                 <tr>
  26.                     <td>@forecast.Date.ToShortDateString()</td>
  27.                     <td>@forecast.TemperatureC</td>
  28.                     <td>@forecast.TemperatureF</td>
  29.                     <td>@forecast.Summary</td>
  30.                     <td>
  31.                         <button @onclick="@(()=>service.Show<UserPopUp>("User PopUp"))" class="btn btn-primary">PopUp</button>
  32.                     </td>
  33.                 </tr>
  34.             }
  35.         </tbody>
  36.     </table>
  37. }

  38. @code {
  39.     private WeatherForecast[]? forecasts;

  40.     protected override async Task OnInitializedAsync()
  41.     {
  42.         forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
  43.     }

  44.     public class WeatherForecast
  45.     {
  46.         public DateTime Date { get; set; }

  47.         public int TemperatureC { get; set; }

  48.         public string? Summary { get; set; }

  49.         public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
  50.     }
  51. }
Now when you will run the application, the output will be like below 



Calling popup component using a Method

let's create a method in Fetch.razor component like below, and call it from the 'popup' button

Below is the function code

  1. void CallPopup(int ID)
  2. {
  3.    
  4. service.Show<UserPopUp>("User PopUp");

  5. }




Now call the above function on the button click, Below is the code on the button click

  1.    <button @onclick="@(() => CallPopup())" class="btn btn-primary">PopUp</button>



The output will be the same as above



Showing a modal popup with passing parameters


Let's pass the ID from a fetch component to Popup Component, 
so let's make changes to the method and call a method from the button click event


  1.    <tbody>
  2.             @foreach (var forecast in forecasts)
  3.             {
  4.                 <tr>
  5.                     <td>@forecast.Date.ToShortDateString()</td>
  6.                     <td>@forecast.TemperatureC</td>
  7.                     <td>@forecast.TemperatureF</td>
  8.                     <td>@forecast.Summary</td>
  9.                     <td>
  10.                       @*  <button @onclick="@(()=>service.Show<UserPopUp>("User PopUp"))" class="btn btn-primary">PopUp</button>*@
  11.                         <button @onclick="@(() => CallPopup(20))" class="btn btn-primary">PopUp</button>

  12.                     </td>
  13.                 </tr>
  14.             }
  15.         </tbody>

below is the code for the function

  1.     void CallPopup(int ID)
  2.     {
  3.         var parameters = new ModalParameters();
  4.         parameters.Add("userID", ID);


  5.        service.Show<UserPopUp>("User PopUp", parameters);

  6.        // we can call like below as well
  7.       // service.Show(typeof(UserPopUp), "Edit Movie", parameters);      
  8.         

  9.     }


 In the above code, we are passing UserID, with the help of ModelParameters function, and then passing it using Show Method.

Below is the code for userPopUp.razor

  1.   
  2. @page "/userPopUp"


  3. <input type="text" @bind="userID" readonly/>

  4. @code {

  5.     [CascadingParameter]
  6.    public ModalParameters? parameters { get; set; }

  7.     [Parameter]
  8.     public int userID { get; set; }

  9.     protected override async void OnInitialized()
  10.     {
  11.         userID = parameters.Get<int>("userID");
  12.        

  13.     }

  14. }

Now let's run the application, and when we will click on the Popup button, we data ID data we will see in the textbox.





You can find the source code from this GitHub Link



------------------------------------------

Share this

Related Posts

Previous
Next Post »

Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now