Trying to insert data from a DATAGRID in MVC ASP.NET using c# (creating a new tuple)

Viewed 78

Somebody good at MVC ASP.NET and C#?. Need a little help.

I'm creating a web application and i want to display a datagrid when clicking on CREATE BUTTON and it should allow me to select 1 or n tuples from that datagrid.

Take a look, i made this image:

image

How can i do that? I tried different thing but no one really worked :(

This is my code:

CONTEXT FILE:

 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);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }

    }

Controller file (only the CREATE method is shown):

 [HttpPost]
       [ValidateAntiForgeryToken]
    public ActionResult Create(int id, [Bind] User user)
    {
        try
        {
            if (ModelState.IsValid)
            {
                dbContext.UpdateAprobador(user);
                return RedirectToAction("Index");
            }
    
            return View(dbContext);
        }
        catch
        {
            return View();
        }
    }

My VIEW FILE:

@model WebApplicationNewsan.Models.User
@{
    ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>User</h4>
<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>
            <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>
            <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>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>
<div>
    <a asp-action="Index">Back to List</a>
</div>
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
0 Answers
Related