Add (Insert) record to table from other page (View) in ASP.Net MVC

acne.star52
 
on Dec 04, 2022 10:09 PM
443 Views

This is my table Emp_Table.

Emp_Id    Emp_Name    Emp_Code     Emp_Country

    A              B                  C                    D

    C              D                  E                    F

One Button (Go to next page)

Next page on 3 TextBox

Emp_Name  TextBox

Emp_Code   TextBox

Emp_Country  TextBox

One Button (Save) add new Data to table Emp_Table

I want to do all this in MVC, can you make a sample and send it to me?

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Shivam.M
 
on Dec 06, 2022 12:52 AM

Hi acne.star52,

Please refer below sample.

Model

public class Employees
{
    public string Emp_Id  { get; set; }
    public string Emp_Name { get; set; }
    public string Emp_Code { get; set; }
    public string Emp_Country { get; set; }
}

Namespaces

using System.Configuration;
using System.Data.SqlClient;

Controller

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        List<Employees> employee = new List<Employees>();
        string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(conString))
        {
            string query = "SELECT * FROM Emp_Table";
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        employee.Add(new Employees
                        {
                            Emp_Id = sdr["Emp_Id"].ToString(),
                            Emp_Name = sdr["Emp_Name"].ToString(),
                            Emp_Code = sdr["Emp_Code"].ToString(),
                            Emp_Country = sdr["Emp_Country"].ToString()
                        });
                    }
                }
                con.Close();
            }
        }

        return View(employee);
    }

    [HttpPost]
    public ActionResult Submit()
    {
        return RedirectToAction("InsertData", "Home");
    }

    [HttpGet]
    public ActionResult InsertData()
    {
        return View(new Employees());
    }

    [HttpPost]
    public ActionResult InsertData(Employees employees)
    {
        string query = "INSERT INTO Emp_Table VALUES(@Emp_Id, @Emp_Name, @Emp_Code, @Emp_Country)";
        string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                con.Open();
                cmd.Parameters.AddWithValue("@Emp_Id", employees.Emp_Id);
                cmd.Parameters.AddWithValue("@Emp_Name", employees.Emp_Name);
                cmd.Parameters.AddWithValue("@Emp_Code", employees.Emp_Code);
                cmd.Parameters.AddWithValue("@Emp_Country", employees.Emp_Country);
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

        return RedirectToAction("Index", "Home");
    }
}

View

Index

@using BindData_And_ShowTo_NextPage_AndUpdate.Models;
@model List<Employees>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Submit", "Home", FormMethod.Post))
    {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th>Emp_Id</th>
                <th>Emp_Name</th>
                <th>Emp_Code</th>
                <th>Emp_Country</th>
            </tr>
            @foreach (Employees employees in Model)
            {
                <tr>
                    <td>@employees.Emp_Id</td>
                    <td>@employees.Emp_Name</td>
                    <td>@employees.Emp_Code</td>
                    <td>@employees.Emp_Country</td>
                </tr>
            }
            <tr>
                <td colspan="4" align="center"><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    }
</body>
</html>

InsertData

@model BindData_And_ShowTo_NextPage_AndUpdate.Models.Employees
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>InsertData</title>
</head>
<body>
    @using (Html.BeginForm("InsertData", "Home", FormMethod.Post))
    {
        <table>
            <tr>
                <td>Id</td>
                <td>@Html.TextBoxFor(m => m.Emp_Id)</td>
            </tr>
            <tr>
                <td>Name</td>
                <td>@Html.TextBoxFor(m => m.Emp_Name)</td>
            </tr>
            <tr>
                <td>Code</td>
                <td>@Html.TextBoxFor(m => m.Emp_Code)</td>
            </tr>
            <tr>
                <td>Country</td>
                <td>@Html.TextBoxFor(m => m.Emp_Country)</td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    }
</body>
</html>

Screenshot