ASP.net MVC 4.0: Insert data through LINQ to Sql classes Model using Textbox, Dropdown and Radio button html helper

1. Create Database

dbstruct

2. Add new item LINQ to SQL classes in Model

linq

Drag your Database table here….

drag

3. Add controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mvctest.Models;

namespace mvctest.Controllers
{
    public class emprecordController : Controller
    {
        //
        // GET: /emprecord/
        DataClasses1DataContext d = new DataClasses1DataContext();
        public ActionResult Index()
        {

            softemp s = new softemp();
            
            s.name = Request["txt2"];
            s.dept = Request["txt3"];
            s.type = Request["txt4"];
            s.salary = Convert.ToDecimal(Request["txt5"]);
            d.softemps.InsertOnSubmit(s);
            ViewData["mes"] = "Inserted";
            d.SubmitChanges();
            return View();
           
        }
        public ActionResult eform()
        {
            return View();
        }

    }
}

4. Add view

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>eform</title>
</head>
<body>
    <div>
 @using (Ajax.BeginForm("index","emprecord", new AjaxOptions { UpdateTargetId = "result" }))
{
    <fieldset style="width:400px;margin:auto">
        <legend>Insert Data</legend>
        <table>
           
            <tr>
                <td>
                    @Html.Label("Employee Name")
                </td>
                <td>
                    @Html.TextBox("txt2")
                </td>
            </tr>
            <tr>
                <td>
                    @Html.Label("Department")
                </td>
                <td>
                    @Html.DropDownList("txt3", new SelectList(new[] { "Account","Sales","Purchase"}))
                </td>
            </tr>
            <tr>
                <td>
                    @Html.Label("Department Type")
                </td>
                <td>
                    @Html.RadioButton("txt4", "Temparory", true)Temporary
                    @Html.RadioButton("txt4", "Permanent", false)Permanent

                   
                </td>
            </tr>
            <tr>
                <td>
                    @Html.Label("Salary")
                </td>
                <td>
                    @Html.TextBox("txt5")
                </td>
            </tr>
            <tr>
                <td><button>Insert</button></td>
            </tr>
        </table>
        <div id="result">@ViewData["mes"]</div>

    </fieldset>
     }
    </div>
</body>
</html>



5. Run the Application & insert data & Click insert Button

Capture

6. Now Check your database.

database