Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class TestController : Controller
{
//
// GET: /Test/
public ActionResult Test()
{
return View("Test");
}
[HttpPost]
public ActionResult Test(ModelTest e, string Command)
{
if (Command == "Submit")
{
e.Insert();
e.Reset();
}
return View("Test");
}
}
}
Model :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace MvcApplication1.Models
{
public class ModelTest
{
public string Name { get; set; }
public string Address { get; set; }
public int Phone_landline { get; set; }
public int Mobile { get; set; }
public string Relation { get; set; }
public string Insert()
{
//SqlConnection con = null;
string result = "";
try
{
string s = "Data Source=XXXX;Database=EmpDetails;Trusted_Connection=Yes;";
//SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["mycon"].ToString());
SqlConnection con=new SqlConnection(s);
SqlCommand cmd = new SqlCommand("spcontacts", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Flage", "I");
// i will pass zero to MobileID beacause its Primary .
cmd.Parameters.AddWithValue("@Name", Name);
cmd.Parameters.AddWithValue("@Address", Address);
cmd.Parameters.AddWithValue("@Phone", Phone_landline);
cmd.Parameters.AddWithValue("@Mobile", Mobile);
cmd.Parameters.AddWithValue("@Relaction", Relation);
con.Open();
result = cmd.ExecuteScalar().ToString();
return result;
}
catch
{
return result = "";
}
finally
{
//con.Close();
}
}
public void Reset()
{
Name = string.Empty;
Address = string.Empty;
Phone_landline = Convert.ToInt16(string.Empty);
Mobile = Convert.ToInt16( string.Empty);
Relation = string.Empty;
}
}
}
View :
@model MvcApplication1.Models.ModelTest
@{
ViewBag.Title = "Test";
}
<h2>Test</h2>
@using (Html.BeginForm())
{
<table><tr><td>Name</td><td>@Html.TextBoxFor(a=>a.Name)</td></tr>
<tr><td>Address</td><td>@Html.TextBoxFor(a=>a.Address)</td></tr>
<tr><td>LandLine No.</td><td>@Html.TextBoxFor(a=>a.Phone_landline)</td></tr>
<tr><td>Mobile/Cell</td><td>@Html.TextBoxFor(a=>a.Mobile)</td></tr>
<tr><td>Comments/Relations</td><td>@Html.TextBoxFor(a=>a.Relation)</td></tr>
<tr><td></td><td><input id="Submit1" type="submit" value="Submit" name="Command" /></td></tr>
<tr><td></td><td></td></tr></table>
}
No comments:
Post a Comment