Passing data from a Razor page to an MVC controller using Blazor web app

Diwakar Devangam 75 Reputation points
2023-12-30T07:54:45.5733333+00:00

I have a Blazor web app with one Razor file (Values.razor), an API controller (ValuesController), and a model (ValuesClass). In Values.razor, I have one button that calls the Get method in ValuesController. I'm trying to assign data to a static list in the Values.razor page, but the static list is getting null values when I try to use it in ValuesController. I have included my code files as images. Is there any logic I am missing to populate the data?

User's image

User's image

User's image

User's image

User's image

Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Blazor
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2023-12-30T15:24:32.4733333+00:00

    I think the following is what you are trying to do if you have the RDLC report working from your previous thread.

    Blazor page in the client project uses a standard form to post user values to an API controller.

    @page "/apiclient"
    @using BlazorWebAssembly.Shared
    
    <PageTitle>ApiClient</PageTitle>
    <h3>ApiClient</h3>
    
    <div>
        <form action="/api/values" method="post">
    
            @for (int i = 0; i < values?.Count(); i++)
            {
                <div>
                    <input name="@($"[{i}].Id")" value="@values[i].Id" />
                    <input name="@($"[{i}].Name")" value="@values[i].Name" />
                </div>
            }
    
            <div>
                <input type="submit" name="submit" value="submit" />
            </div>
        </form>
    </div>
    
    @code {
        private List<ValuesModel>? values { get; set; }
        protected override async Task OnInitializedAsync()
        {
            //Mock populating values
            values = PopulateValues();
        }
    
        private List<ValuesModel> PopulateValues()
        {
            List<ValuesModel> vals = new List<ValuesModel>();
            for (int i = 0; i < 5; i++)
            {
                vals.Add(new ValuesModel() { Id = i, Name = $"Name {i}" });
            }
            return vals;
        }
    }
    

    The API Controller returns a text file to simulate returning a report file.

        [Route("api/[controller]")]
        [ApiController]
        public class ValuesController : ControllerBase
        {
            // POST api/<ValuesController>
            [HttpPost]
            public ActionResult Post([FromForm] List<ValuesModel> values)
            {
                string content = string.Empty;
                foreach(ValuesModel v in values)
                {
                    content += $"{v.Id}\t{v.Name}\r";
                }
                byte[] buff = Encoding.ASCII.GetBytes(content);
                return File(buff, "application/octet-stream", "myfile.txt");
            }
    
        }
    
    
        ```
    
    Model
    
    
    ```csharp
        public class ValuesModel
        {
            public int Id { get; set; }
            public string Name { get; set; } = string.Empty;
        }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.