1

I want to ask how to insert multiple rows of data at once. I included the textboxes for the user to input but it only inserts the first row of data and not the second line. Here are my references below. The first is my controller code and the second is my view page code. I am a new coder and i need to seek help for my school work. It would be best if I can solve this problem. Thank you

[HttpPost("Create2")]
    public async Task<IActionResult> CreateDependent2(Dependent dependentModel)
    {

        if (!ModelState.IsValid)
        {
            return BadRequest("Parameter condition(s) are not met");
        }
        else if (ModelState.IsValid)
        {

            
                _context.Dependents.Add(dependentModel);
           
            await _context.SaveChangesAsync();
            return Ok(dependentModel);
        }
        else
        {
            return BadRequest("Invalid");
        }
    }

and here is my view code

@model TTSH_ALIVE.Models.Dependent
@using TTSH_ALIVE.ModelObjects

@{ ViewData["Title"] = "Register";
Layout = null; }
<head>
<title>Welcome - TTSH SteWARdS</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/x-icon" href="~/images/icons/favicon.ico" />
<link rel="stylesheet" type="text/css" href="~/vendor/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="~/fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="~/fonts/Linearicons-Free-v1.0.0/icon-font.min.css">
<link rel="stylesheet" type="text/css" href="~/vendor/animate/animate.css">
<link rel="stylesheet" type="text/css" href="~/vendor/css-hamburgers/hamburgers.min.css">
<link rel="stylesheet" type="text/css" href="~/vendor/select2/select2.min.css">
<link rel="stylesheet" type="text/css" href="~/css/util.css">
<link rel="stylesheet" type="text/css" href="~/css/main.css">
</head>

<div class="limiter">
<div class="container-login100">
    <div class="wrap-login100 p-t-50 p-b-0">
        <span class="login100-form-error p-t-20 p-b-45">
            @ViewBag.error
        </span>
        <div class="jp-container" style="margin-top:20px">
            <h1 class="jp-jumbo"><b>Sign Up</b></h1>
            <hr style="width:80px; border:5px solid #a6192e" class="jp-round">
        </div>

        <div class="jp-row">
            <form asp-action="CreateDependent">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <input type="hidden" asp-for="UserID" value="@ViewData["UserID"]" class="form-control" />
                <div class="form-group-half">
                    <label asp-for="AgeOfDependent" class="text-white"></label>
                    <input asp-for="AgeOfDependent" class="form-control" />
                    <span asp-validation-for="AgeOfDependent" class="text-white"></span>
                </div>
                <div class="form-group-full">
                    <label asp-for="DependentType" class="text-white"></label><br />
                    <select asp-for="DependentType" class="jp-select">
                        <option selected disabled>---Please select a dependent type---</option>
                        <option>Parent</option>
                        <option>Child</option>
                        
                    </select>
                    <span asp-validation-for="DependentType" class="text-white"></span>
                </div>
                <div class="form-group-half">
                    <label asp-for="AgeOfDependent" class="text-white"></label>
                    <input asp-for="AgeOfDependent" class="form-control" />
                    <span asp-validation-for="AgeOfDependent" class="text-white"></span>
                </div>
                <div class="form-group-full">
                    <label asp-for="DependentType" class="text-white"></label><br />
                    <select asp-for="DependentType" class="jp-select">
                        <option selected disabled>---Please select a dependent type---</option>
                        <option>Parent</option>
                        <option>Child</option>
                    </select>
                    <span asp-validation-for="DependentType" class="text-white"></span>
                </div>
                <div class="text-white">
                    <b>@ViewBag.message</b>
                </div>
                <div class="form-group-half">
                    <input type="submit" value="Sign Up" class="jp-btn-86" />
                </div>
                <div class="form-group-half">
                    <a asp-area="" asp-controller="Login" asp-action="Index" style="margin-top: 7px" class="login100-form-btn">Back to Login</a>
                </div>
            </form>
        </div>
    </div>
</div>

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Olived
  • 11
  • 1
  • https://stackoverflow.com/questions/48833363/asp-net-mvc-send-listfoo-collection-from-my-view-to-a-controller/48835106 – Chetan Nov 20 '20 at 03:20
  • https://www.c-sharpcorner.com/UploadFile/pmfawas/Asp-Net-mvc-how-to-post-a-collection/ – Chetan Nov 20 '20 at 03:21

0 Answers0