Wednesday, 16 May 2012

Insert and validate into the database in MVC using json and linq to sql

Design the page like this
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication35.Models.Test>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript" src="../../Scripts/MicrosoftMvcValidation.js"></script>
<script type="text/javascript" src="../../Scripts/MicrosoftMvcAjax.js"></script>
<script type="text/javascript" src="../../Scripts/jquery-1.4.1.js"></script>
<script language="javascript" type="text/javascript">
    $(document).ready
(
function () {
    $("#tb").click(
function () {
    if ($("#EID").val() == "") {

        alert('EID should not blank.')
        $('#EID').focus()
        return false
    }
    else if ($('#NAME').val() == "") {

        alert('Name should not blank.')
        $('#NAME').focus()
        return false
    }
    var m = y()
    $.post('/Home/xx', m, function (data)
    {
        alert('One record saved.')
    }
    )

}
)
function y()
    {
        var eid = $("#EID").val();
        var name = $("#NAME").val();
        return (name == "") ? null : { EID: eid, NAME: name };
    }
}
)
</script>
<%Html.EnableClientValidation(); %>
<%using (Html.BeginForm())
  { %>
  <form id="f">
  <table align="center">
  <tr><th colspan="2"> <h2><%: ViewData["Message"] %></h2></th></tr>
  <tr><td><label for="EID">EID</label></td><td><%=Html.TextBoxFor(Model=>Model.EID) %></td></tr>
  <tr><td><label for="EID">NAME</label></td><td><%=Html.TextBoxFor(Model=>Model.NAME) %></td></tr>
  <tr><td>&nbsp;</td><td><input type="button" value="Save" name="tb" id="tb" /></td></tr>
  <tr><td colspan="2"><div id="gv"></div></td></tr>
  </table>
  </form>
<%} %>
  
   
</asp:Content>
Then go to the MODEL and create a class.class name is Test

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace MvcApplication35.Models
{
    public class Test
    {
       
        public int EID
        { get; set; }
      
        public string NAME
        { get; set; }
    }
}
then go to the Controller page

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

namespace MvcApplication35.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        DataClasses2DataContext o = new DataClasses2DataContext();
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
        [HttpPost]
        public JsonResult xx(Test p)
        {
           
                EMP q = new EMP();
                q.EID = p.EID;
                q.NAME = p.NAME;
                o.EMPs.InsertOnSubmit(q);
                o.SubmitChanges();
                return Json(q);
        }
    }
}

No comments:

Post a Comment