Entity
Web Api :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using WebApplication22.Models;
namespace WebApplication22.Controllers
{
public class QuestionmasterController : ApiController
{
[HttpGet]
public List<QMASTER> Gets()
{
using(Database1Entities obj=new Database1Entities())
{
return obj.QUESTIONs.Select(m => new QMASTER { QID = m.QID, QUESTION = m.QUESTION1, CONTROLTYPEID = m.CONTROLTYPEID.Value, VLIST = obj.QUESTIONMAPs.Where(n => n.QID == m.QID).Select(p => p.VALUE).ToList() }).ToList();
}
}
[HttpGet]
public QMASTER Get(int QID,int j)
{
using (Database1Entities obj = new Database1Entities())
{
return obj.QUESTIONs.Select(m => new QMASTER { QID = m.QID, QUESTION = m.QUESTION1, CONTROLTYPEID = m.CONTROLTYPEID.Value, VLIST = obj.QUESTIONMAPs.Where(n => n.QID == m.QID).Select(p => p.VALUE).ToList() }).FirstOrDefault(x=>x.QID==QID);
}
}
[HttpPost]
public string Post(QMASTER qm)
{
using (Database1Entities obj = new Database1Entities())
{
obj.QUESTIONs.Add(new QUESTION { QUESTION1 = qm.QUESTION, CONTROLTYPEID = qm.CONTROLTYPEID });
obj.SaveChanges();
int QID = obj.QUESTIONs.OrderByDescending(p => p.QID).First().QID;
obj.QUESTIONMAPs.AddRange(qm.VLIST.Select(p=>new QUESTIONMAP{ QID=QID, VALUE=p}));
obj.SaveChanges();
return "Data Saved.";
}
}
[HttpPut]
public string Put(QMASTER qm)
{
using (Database1Entities obj = new Database1Entities())
{
QUESTION qs = obj.QUESTIONs.SingleOrDefault(p => p.QID == qm.QID);
qs.QUESTION1 = qm.QUESTION;
qs.CONTROLTYPEID = qm.CONTROLTYPEID;
obj.SaveChanges();
obj.QUESTIONMAPs.RemoveRange(obj.QUESTIONMAPs.Where(m=>m.QID==qm.QID));
obj.SaveChanges();
obj.QUESTIONMAPs.AddRange(qm.VLIST.Select(p => new QUESTIONMAP { QID = qm.QID, VALUE = p }));
obj.SaveChanges();
return "Data Updated.";
}
}
[HttpGet]
public string Delete(int QID)
{
using (Database1Entities obj = new Database1Entities())
{
QUESTION qs = obj.QUESTIONs.SingleOrDefault(p => p.QID == QID);
obj.QUESTIONs.Remove(qs);
obj.SaveChanges();
obj.QUESTIONMAPs.RemoveRange(obj.QUESTIONMAPs.Where(m => m.QID == QID));
obj.SaveChanges();
return "Data Deleted.";
}
}
}
public class QMASTER
{
public int QID{get;set;}
public string QUESTION{get;set;}
public int CONTROLTYPEID{get;set;}
public List<string> VLIST{get;set;}
}
}
View code :
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link href="Content/bootstrap.css" rel="stylesheet" />
<link href="Content/bootstrap-theme.css" rel="stylesheet" />
<script src="Scripts/jquery-1.9.1.js"></script>
<script src="Scripts/angular.js"></script>
</head>
<body ng-app="app" ng-controller="ctrl" style="padding-top:10px">
<div class="container">
<form name="frmEmp" novalidate class="form-horizontal">
<div class="form-group">
<label class="control-label col-lg-4">QUESTION</label>
<div class="col-lg-4">
<input type="text" class="form-control" ng-model="QMASTER.QUESTION" />
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4">Control Type</label>
<div class="col-lg-4">
<select class="form-control" ng-change="ddl()" ng-options="c.cid as c.name for c in listc" ng-model="QMASTER.CONTROLTYPEID">
<option value="">Select</option>
</select>
</div>
</div>
<div class="form-group" ng-show="checkv==1">
<div class="col-md-4"> </div>
<div class="col-md-4">
<table >
<tr>
<td>Value</td>
<td> </td>
<td style="padding-left:140px">
<a ng-click="addp()" id="btnPlus"><img class="img-responsive" src="Content/plusicon.png"></a>
</td>
</tr>
</table>
</div>
</div>
<div class="form-group" ng-show="checkv==1">
<div class="col-md-4"> </div>
<div class="col-md-4">
<table id="ValueDiv">
<tr class="c">
<td>
<input type="text" class="form-control txtValue" />
</td>
<td> </td>
<td>
<a name="del"><img class="img-responsive" src="Content/minus-icon.png"></a>
</td>
</tr>
</table>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4"></label>
<div class="col-lg-4">
<input type="button" ng-click="save()" class="btn btn-primary" value="Save" style="width:80px" />
<input type="button" ng-click="update()" class="btn btn-primary" value="Update" style="width:80px" />
<input type="button" ng-click="reset()" class="btn btn-primary" value="Reset" style="width:80px" />
</div>
</div>
<div class="row">
<table class="table table-bordered table-condensed table-hover table-responsive table-striped">
<thead class="bg-primary">
<tr>
<th>QID</th>
<th>QUESTION</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="c in liste">
<td>{{c.QID}}</td>
<td>{{c.QUESTION}}</td>
<td><a ng-click="edit(c.QID)">Edit</a>|<a ng-click="del(c.QID)">Delete</a></td>
</tr>
</tbody>
</table>
</div>
</form>
</div>
</body>
</html>
<script type="text/javascript">
angular.module("app", [])
.controller("ctrl", function ($scope, EmpService) {
function CLR()
{
$scope.QMASTER = new qMASTER();
} CLR();
function fill()
{
EmpService.Gets().then(function (d) {
$scope.liste = d.data;
});
} fill();
$scope.listc = [
{ cid: 1, name: "Textbox" },
{cid:2,name:"Multilinetextbox"},
{ cid: 3, name: "Dropdown" }
];
$scope.checkv = 0;
$scope.ddl = function ()
{
if ($scope.QMASTER.CONTROLTYPEID == 3)
$scope.checkv = 1;
else
$scope.checkv = 0;
}
var row;
row = $('.c').clone();
$scope.addp = function ()
{
var crow = row.clone();
$('#ValueDiv').append(crow);
}
$('body').on('click', "a[name='del']", function () {
var row = $(this).parents('.c');
if ($('.c').length > 1)
row.remove();
});
$scope.edit = function (s) {
EmpService.Get(s).then(function (d) {
if (d.data.CONTROLTYPEID == 3)
{
$scope.checkv = 1;
$('.c').remove();
for (var i = 0; i < d.data.VLIST.length; i++)
{
$('#ValueDiv').append("<tr class='c'><td><input type='text' class='form-control txtValue' value='"+d.data.VLIST[i]+"' /></td><td> </td><td><a name='del'><img class='img-responsive' src='Content/minus-icon.png'></a></td></tr>");
}
}
else
$scope.checkv = 0;
$scope.QMASTER = d.data;
});
}
$scope.save = function ()
{
var m = [];
if($scope.QMASTER.CONTROLTYPEID==3)
{
$('.c').find(".txtValue").each(function (i, j) {
m.push( $(j).val());
});
$scope.QMASTER.VLIST = m;
}
EmpService.Save($scope.QMASTER).then(function (d) {
alert(d.data);
CLR();
fill();
$scope.checkv = 0;
});
}
$scope.update = function () {
var m = [];
if ($scope.QMASTER.CONTROLTYPEID == 3) {
$('.c').find(".txtValue").each(function (i, j) {
m.push($(j).val());
});
$scope.QMASTER.VLIST = m;
}
EmpService.Update($scope.QMASTER).then(function (d) {
alert(d.data);
CLR();
fill();
$scope.checkv = 0;
});
}
$scope.reset = function ()
{
CLR();
}
$scope.del = function (s)
{
if (confirm('Do you want to delete it ?'))
{
EmpService.Delete(s).then(function (d) {
alert(d.data);
fill();
});
}
}
})
.factory("EmpService", function ($http) {
var fac = {};
fac.Save = function (emp)
{
return $http.post("http://localhost:50628/api/Questionmaster/Post", emp);
}
fac.Update = function (emp)
{
return $http.put("http://localhost:50628/api/Questionmaster/Put", emp);
}
fac.Delete = function (EID) {
return $http.get("http://localhost:50628/api/Questionmaster/Delete?QID="+EID);
}
fac.Get = function (EID) {
return $http.get("http://localhost:50628/api/Questionmaster/Get?QID=" + EID+"&j="+2);
}
fac.Gets = function (EID) {
return $http.get("http://localhost:50628/api/Questionmaster/Gets");
}
return fac;
});
function qMASTER()
{
return {
QID: null,
QUESTION: null,
CONTROLTYPEID: null,
VLIST:[]
};
}
</script>
No comments:
Post a Comment