Inserting 1 or many rows from a datagrid to another (MVC ASP.NET C#) RRS feed

  • Question

  • User898408952 posted

    I need help in here, i do not know how to solve this situation (i'm using MVC ASP.NET and C#)

    My web application works like this: everytime it runs it shows a datagrid and allows me to EDIT/DELETE or CREATE the tuples shown in the given DATAGRID, do you get me?. Everytime i click on CREATE another page is shown and i can write the new values manually but i dont want to that anymore. I need to display a new datagrid and the user should be able to SELECT the rows he wants to add. Here's my confusion: how can i do that? using forms?. Nothing seems to be helpful for me.


     public void CreateUser (User user)
            using (SqlConnection con = new SqlConnection(connectionString))
                SqlCommand cmd = new SqlCommand("Create_user", con)
                    CommandType = System.Data.CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@NAME", user.NAME);
                cmd.Parameters.AddWithValue("@LAST", user.LAST);
                cmd.Parameters.AddWithValue("@DESCRIPTION", user.DESCRIPTION);

    Controller file (only the CREATE method is shown):

        public ActionResult Create(int id, [Bind] User user)
                if (ModelState.IsValid)
                    return RedirectToAction("Index");
                return View(dbContext);
                return View();


    @model WebApplicationNewsan.Models.User
        ViewData["Title"] = "Create";
    <hr />
    <div class="row">
        <div class="col-md-4">
            <form asp-action="Create">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="NAME" class="control-label"></label>
                    <input asp-for="NAME" class="form-control" />
                    <span asp-validation-for="NAME" class="text-danger"></span>
                <div class="form-group">
                    <label asp-for="LAST" class="control-label"></label>
                    <input asp-for="LAST" class="form-control" />
                    <span asp-validation-for="LAST" class="text-danger"></span>
                <div class="form-group">
                    <label asp-for="DESCRIPTION" class="control-label"></label>
                    <input asp-for="DESCRIPTION" class="form-control" />
                    <span asp-validation-for="DESCRIPTION" class="text-danger"></span>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
        <a asp-action="Index">Back to List</a>
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

    Can somebody please help me with this? I'm super lost.

    Sunday, February 21, 2021 2:32 AM

All replies

  • User1120430333 posted

    What you should be doing is the Create should create the new record. The Create shouldn't be on a row on the grid it should like be in the upper right hand corner of the page, if that is what you are doing with something on a grid row.

    Once the new record is created, you read the database table again on the redirect to the Index page that will pick up the new record that was added and display a new grid will all rows again including the new record added..

    You shouldn't be be doing any direct database access in a controller. You should be trying to implement some kind of SoC.

    Separation of concerns - Wikipedia

    Understanding Separation Of Concern in ASP.NET MVC (c-sharpcorner.com)

    Architectural principles | Microsoft Docs

    Maybe, you should look into Layered style.

    Chapter 3: Architectural Patterns and Styles | Microsoft Docs

    You should understand the various models used in MVC.

    Kinds of Models | DevIQ

    You should understand what a viewmodel is for the view that is strong typed to a given view.

    Understanding ViewModel in ASP.NET MVC (dotnettricks.com)

    The action is between the viewmodel object,  domain object and the persistence layer, which should be happening with objects it the Models folder.

    Understanding Models, Views, and Controllers (C#) | Microsoft Docs


    An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic. 

    A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control). Everything else should be contained in the model.

    In general, you should strive for fat models and skinny controllers. Your controller methods should contain only a few lines of code. If a controller action gets too fat, then you should consider moving the logic out to a new class in the Models folder.


    DM = domain model -- class and  VM = viewmodel -- class with classes in the Models folder.

    You can examine the example solution code in the github link if you like that implements concepts talked about. You'll also notice how a redirect to an Index action method reloads a HTML table  with rows to be selected, like any new row added to the database table.


    Sunday, February 21, 2021 8:34 AM
  • User1312693872 posted


    How do you use the datagrid, can you show me the related code?

    If you use the mvc datagrid control, and to insert new row from an existing datagrid, you can check this:


    If you use the jqgrid, you can select the rows and click  to add them to another grid:


                    zIndex: 100,
                    url: '/Demo/AddtoAnothergird',
                    closeOnEscape: true,
                    closeAfterEdit: true,
                    recreateForm: true,
                    afterComplete: function (response)
                        if (response.responseText)

    Best Regards,

    Jerry Cai

    Monday, February 22, 2021 6:25 AM