Jayraj Goswami

Jayraj Goswami

  • NA
  • 198
  • 12.5k

Insert Emp Data Web Api

Feb 20 2016 4:33 AM
/*CLS*/
ObjectParameter OutputParamValue = new ObjectParameter("output", typeof(string));
public EmpDTO SaveEmployee(EmpDTO obj)
{
using (GeesemedLocalEntities DB = new GeesemedLocalEntities())
{

DB.InsertEmployee(obj.EmpName, obj.Salary, obj.DeptName, obj.Designation, OutputParamValue);
DB.SaveChanges();
obj.Result = Convert.ToInt32(OutputParamValue.Value);
return obj;

}
}

/*API Controller*/

[Route("SaveEmployee")]
public HttpResponseMessage SaveEmployee(EmpDTO obj)
{
obj = EmpRep.SaveEmployee(obj);
var Responce = Request.CreateResponse<EmpDTO>(HttpStatusCode.Created, obj);
Responce.ReasonPhrase = Convert.ToString(obj.Result);
return Responce;
}



MVC CNtl
public ActionResult SaveEmployee(EmpDTO objDTO)
{
if (ModelState.IsValid)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:6198/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsJsonAsync("api/EmpApi/SaveEmployee", objDTO).Result;
if (response.IsSuccessStatusCode)
{
}

}

}

return RedirectToAction("GetEmployee");


/*View*/

@model SampleMapper.EmpDTO
@{
ViewBag.Title = "SaveEmployee";
}

<h2>SaveEmployee</h2>

@using (Html.BeginForm(FormMethod.Post))
{
<table>
@Html.HiddenFor(m => m.EmpNo)

<tr>
<td>
EmpName :
</td>
<td>
@Html.TextBoxFor(m => m.EmpName, new { @class = "form-control SpeCharNot" })
</td>
</tr>

<tr>
<td>
Salary :
</td>
<td>
@Html.TextBoxFor(m => m.Salary, new { @class = "form-control SpeCharNot" })
</td>
</tr>

</table>

<input type="submit" value="Save" class="btn button" formaction="/Emp/SaveEmployee" />
}

Answers (2)