locked
Razor pages - saving multiple record to DB with checkboxes RRS feed

  • Question

  • User1778916669 posted

    I have 2 tables:

    • Eployee: EmployeeId, Name, Departmant
    • Form: FormId, EmployeeId, Reason

     I would like to have a form where I can add reason to 1 or to multiple employees, like below:

    ID Name Yes/No
    1 Mel
    2 Pete
    2 Tom
    Reason
    Save Cancel

    If I save only one record it works fine. But if I choose more than one I’m getting:

    SqlException: New transaction is not allowed because there are other threads running in the session.

    I’m pretty new to razor pages and maybe I have approached completly wrong

     

    My Model:

    public Form Form { get; set; }

    public async Task OnPost(string[] AreChecked, string Reason)

    {

        Employee = db.Employee

        foreach (var item in Employee.ToList)

        {

            if (AreChecked.Contains(item.EmployeeId))

            {

                Form.EmployeeId = item.EmployeeId;

                Form.Reason = Reason;

                db.Form.Add(Form);

            }

            await db.SaveChangesAsync();

        }

    }

     

    I’m passing string[] AreChecked through:     

    <input name="AreChecked" type="checkbox" value="@item.EmployeeId " />

    Monday, March 15, 2021 9:25 AM

Answers

  • User1686398519 posted

    Hi DaddyCool123, 

    It is strange that I did not reproduce your error based on the code you provided.

    But I found that the code you provided did not initialize the Form.

    At the same time, I modified your code to meet your needs. You can refer to the demo below.

    Model

        public class Employee
        {
            public int EmployeeId { get; set; }
            public string Name{ get; set; }
            public string Departmant{ get; set; } 
        }
        public class Form
        {
            public int FormId { get; set; }
            public int EmployeeId { get; set; }
            public string Reason { get; set; }
        }

    CheckboxesTest.cshtml.cs

        public class CheckboxesTestModel : PageModel
        {
            public DailyRazorPageDemoContext db;
            public CheckboxesTestModel(DailyRazorPageDemoContext _db)
            {
                db = _db;
            }
            public IQueryable<Employee> Employee { get; set; }
            public Form Form { get; set; }
            public void OnGet()
            {
                Employee= db.Employee;
            }
            public async Task OnPost(string[] AreChecked, string Reason)
            {
                
                Employee = db.Employee;
                foreach (var item in Employee.ToList())
                {
                    if (AreChecked.Contains(item.EmployeeId.ToString()))
                    {
                        Form = new Form
                        {
                            EmployeeId = item.EmployeeId,
                            Reason = Reason
                        };
                        db.Form.Add(Form);
                }
                    await db.SaveChangesAsync();
                }
            }
        }

    CheckboxesTest.cshtml

    <form method="post">
        <table class="table">
            <tr>
                <td>
                    ID
                </td>
                <td>
                    Name
                </td>
                <td>
                    Yes/No
                </td>
            </tr>
            @foreach (var item in Model.Employee.ToList())
            {
                <tr>
                    <td>
                        @item.EmployeeId
                    </td>
                    <td>
                        @item.Name
                    </td>
                    <td>
                        <input name="AreChecked" type="checkbox" value="@item.EmployeeId" />
                    </td>
                </tr>
            }
        </table>
        <div class="form-group">
            <label>Reason</label>
            <input name="Reason" class="form-control" />
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-outline-primary">
        </div>
    </form>

    Here is the result. 

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 16, 2021 6:49 AM

All replies

  • User1686398519 posted

    Hi DaddyCool123, 

    It is strange that I did not reproduce your error based on the code you provided.

    But I found that the code you provided did not initialize the Form.

    At the same time, I modified your code to meet your needs. You can refer to the demo below.

    Model

        public class Employee
        {
            public int EmployeeId { get; set; }
            public string Name{ get; set; }
            public string Departmant{ get; set; } 
        }
        public class Form
        {
            public int FormId { get; set; }
            public int EmployeeId { get; set; }
            public string Reason { get; set; }
        }

    CheckboxesTest.cshtml.cs

        public class CheckboxesTestModel : PageModel
        {
            public DailyRazorPageDemoContext db;
            public CheckboxesTestModel(DailyRazorPageDemoContext _db)
            {
                db = _db;
            }
            public IQueryable<Employee> Employee { get; set; }
            public Form Form { get; set; }
            public void OnGet()
            {
                Employee= db.Employee;
            }
            public async Task OnPost(string[] AreChecked, string Reason)
            {
                
                Employee = db.Employee;
                foreach (var item in Employee.ToList())
                {
                    if (AreChecked.Contains(item.EmployeeId.ToString()))
                    {
                        Form = new Form
                        {
                            EmployeeId = item.EmployeeId,
                            Reason = Reason
                        };
                        db.Form.Add(Form);
                }
                    await db.SaveChangesAsync();
                }
            }
        }

    CheckboxesTest.cshtml

    <form method="post">
        <table class="table">
            <tr>
                <td>
                    ID
                </td>
                <td>
                    Name
                </td>
                <td>
                    Yes/No
                </td>
            </tr>
            @foreach (var item in Model.Employee.ToList())
            {
                <tr>
                    <td>
                        @item.EmployeeId
                    </td>
                    <td>
                        @item.Name
                    </td>
                    <td>
                        <input name="AreChecked" type="checkbox" value="@item.EmployeeId" />
                    </td>
                </tr>
            }
        </table>
        <div class="form-group">
            <label>Reason</label>
            <input name="Reason" class="form-control" />
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-outline-primary">
        </div>
    </form>

    Here is the result. 

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 16, 2021 6:49 AM
  • User1778916669 posted

    Thanks a lot

    Thursday, March 18, 2021 7:58 AM