最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net core - Dynamically Generated Blazor Form not model binding - Stack Overflow

programmeradmin21浏览0评论

I am trying to develop an app in which a quiz is generated dynamically at various instances to track a patient's progress. I am able to generate various instances of the form, have the form filled out and save the results. The problem is that the answers to the quiz are not posted upon submit - just the default values.

Generated Form

Co-Pilot tells me that user input is likely being lost due to the reinitialization of the TestFormModel. However, I am guarding against that, I thought.

OnInitializedAsync()

Here's the submit code: Submit Code

Any help in figuring this out greatly appreciated. The code for this is located at .

I am trying to develop an app in which a quiz is generated dynamically at various instances to track a patient's progress. I am able to generate various instances of the form, have the form filled out and save the results. The problem is that the answers to the quiz are not posted upon submit - just the default values.

Generated Form

Co-Pilot tells me that user input is likely being lost due to the reinitialization of the TestFormModel. However, I am guarding against that, I thought.

OnInitializedAsync()

Here's the submit code: Submit Code

Any help in figuring this out greatly appreciated. The code for this is located at https://github.com/rswetnam/SequentialTests.

Share Improve this question asked Feb 5 at 13:08 RBSRBS 35 bronze badges 2
  • Please paste the code here instead of linking to external sources – Juan Cruz Soler Commented Feb 5 at 13:38
  • You can not use for loop with callback where you use the i value. The i will be equals to the last value, so when you do (@bind-Value=MyDictionary[i]) this is equivalent to @(e => MyDictionary[i] = e) but i will always be equal to the last loop. Check this: shauncurtis.github.io/Building-Blazor-Applications/… – Bisjob Commented Feb 5 at 16:52
Add a comment  | 

1 Answer 1

Reset to default 0

Blazor generally support foreach for list render. But when using for, blazor may not correctly track changes because the index variable (i) changes during rendering. You could try create new variable inside the loop, such as following:

        @for (int i = 0; i < 6; i++)
        {
            int index=i;
            //use the "index" instead of i 
            var question = @TestFormModel.Questions[index];
            ...
发布评论

评论列表(0)

  1. 暂无评论