Create Table Script :
CREATE TABLE [dbo].[tblJob](
[JobId] [int] IDENTITY(1,1) NOT NULL,
[JobName] [varchar](50) NULL,
[JobDesc] [varchar](50) NULL,
[DateFrom] [datetime] NULL,
[DateTo] [datetime] NULL,
[MID] [int] NULL,
[IsActive] [bit] NULL,
CONSTRAINT [PK_tblJob] PRIMARY KEY CLUSTERED
(
[JobId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Create Procedure
Create Proc [dbo].[Usp_CreateJob]
@JobName varchar(50),
@JobDesc varchar(50),
@DateFrom datetime,
@DateTo datetime,
@MID int
As
Begin
if not exists (select JobName from tblJob where JobName=@JobName)
begin
Insert tblJob values(@JobName,@JobDesc,@DateFrom,@DateTo,@MID,'true')
select @@IDENTITY
End
else
begin
select -1
end
end
Create Database Connection
<add name="SampleDbEntities1" connectionString="data source=VIKASH-PC\SQLEXPRESS;initial catalog=SampleDb;integrated security=True;" />
Add JobModel.cs in Models Folder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcProject.Models
{
public class JobModel
{
public string JobName { get; set; }
public string JobDesc { get; set; }
public int MID { get; set; }
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
}
}
Add Controller Methods
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcProject.Models;
namespace MvcProject.Controllers
{
public class Home3Controller : Controller
{
public ActionResult Index()
{
string connectionstring = ConfigurationManager.ConnectionStrings["SampleDbEntities1"].ToString();
using (SqlConnection con = new SqlConnection(connectionstring))
{
SqlCommand com = new SqlCommand("Usp_ReadMsets", con);
com.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader reader = com.ExecuteReader();
List<SelectListItem> mset = new List<SelectListItem>();
while (reader.Read())
{
mset.Add(new SelectListItem{
Value=(reader["MID"].ToString()),
Text=reader["MName"].ToString()
});
}
ViewBag.Msets = mset;
}
return View();
}
[HttpPost]
public ActionResult Index(JobModel job)
{
var isSuccess = false;
var message = "";
string connectionstring = ConfigurationManager.ConnectionStrings["SampleDbEntities1"].ToString();
using (SqlConnection con = new SqlConnection(connectionstring))
{
SqlCommand com = new SqlCommand("Usp_CreateJob", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@JobName", job.JobName);
com.Parameters.AddWithValue("@JobDesc", job.JobDesc);
com.Parameters.AddWithValue("@DateFrom",job.DateFrom);
com.Parameters.AddWithValue("@DateTo", job.DateTo);
com.Parameters.AddWithValue("@MID", job.MID);
con.Open();
int i = com.ExecuteNonQuery();
if (i > 0)
{
isSuccess = true;
message = "The job has been created!";
}
else
{
isSuccess = false;
message = "The job has not been created!";
}
var jsonData = new { isSuccess, message };
return Json(jsonData);
}
// return View();
}
}
}
Add Index.cshtml
@{
ViewBag.Title = "Create Job";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<!-- Load jQuery JS -->
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<!-- Load jQuery UI Main JS -->
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(document).ready(function () {
$('#txtDateFrom').datepicker({ minDate: '0' });
$('#txtDateTo').datepicker({ minDate: '0' });
$("#btnSave").click(function () {
$("#result").html("");
if ($("#txtJobName").val()!="") {
if ($("#txtJobDesc").val()!="")
{
$.ajax(
{
type: "POST",
url: "Home3/Index",
data: {
JobName: $("#txtJobName").val(),
JobDesc: $("#txtJobDesc").val(),
DateFrom: $("#txtDateFrom").val(),
DateTo: $("#txtDateTo").val(),
MID: $("#drpMID").find(":selected").val()
},
complete: function () {
},
success: function (data) {
if (data.isSuccess) {
alert("Success! " + data.message);
} else {
alert("Failed! " + data.message);
}
}
});
}
else{
$("#txtJobDesc").focus();
$("#result").html("Please enter job desc.");
}
}
else {
$("#txtJobName").focus();
$("#result").html("Please enter job name");
}
});
});
</script>
Job Name :
<br />
<input type="text" id="txtJobName" /><br />
Job Desc:
<br />
<textarea id="txtJobDesc"></textarea>
<br />
Date From
<br />
<input type="text" id="txtDateFrom" /><br />
Date To
<br />
<input type="text" id="txtDateTo" /><br />
Mtype
<br />
<select>
@foreach (var m in @ViewBag.Msets)
{
<option id="drpMID" value= "+@m.Value+">@m.Text </option>
}
</select><br />
<br />
<button type="button" id="btnSave">Save</button>
<br />
<div id="result"></div>
No comments:
Post a Comment