Marius Vasile

Marius Vasile

  • 564
  • 1.9k
  • 142.5k

asp.net core add multiple data with one click

Aug 8 2021 7:46 AM

I have a table with Id and Status as string. The user can enter two or more text and with a click of a button they should be all written to database. So far I tried to have this

<form method="post">
        <div class="row no-gutters mt-4">
            <p class="text-left h5 ml-1">Root Causes<span class="text-left h5 ml-1" style="color:steelblue"> - Identification</span></p>
        </div>
        <div class="col-md-2">
            <div class="text-center">
                <input asp-for="RCAMain.Status" />
            </div>
        </div>
        <div class="col-md-2">
            <div class="text-center">
                <input asp-for="RCAMain.Status" />
            </div>
        </div>
        <div class="col-md-2">
            <div class="text-center">
                <input asp-for="RCAMain.Status" />
            </div>
        </div>

        <div class="row no-gutters">
            <button class="btn border-0 text-white mt-2" asp-page-handler="AddRCAMain" asp-route-idinv="@Model.IdINV" asp-route-app="@Model.APP" type="submit" style="background-color:indigo">
                Add RCA Category
            </button>
        </div>
    </form>
public async Task<IActionResult> OnPostAddRCAMainAsync(string idinv, string app)
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var orgid = await _context.UsersData.Where(a => a.Id == userId).Select(a => a.OrgID).SingleOrDefaultAsync();
            var idrca = await _context.InvestigationRCAs.Where(s => s.OrgID == orgid && s.IdINV == idinv).Select(s => s.IdRCA).FirstOrDefaultAsync();
            var newRCAMain = new RCAMain()
            {
                IdRCA = idrca
            };
            if (await TryUpdateModelAsync(newRCAMain, "RCAMain",

                s => s.Status

            ))
            {
                _context.RCAMains.AddRange(newRCAMain);
                await _context.SaveChangesAsync();
                return RedirectToPage("/SRM/Investigation/RCA", new { idinv, app });
            }
            return Page();
        }

 

but no Status is added, only the IdRCA


Answers (3)