Hello,
Below is my snippet . My data doesn't save in database. Please help.
<div class="container-fluid" ng-controller="myCtrl">
<form name="FileEntryForm">
<div class="container">
<div class="row col-md-12 col-md-offset-2">
<div>
<div class="Header">
<h2>File Details</h2>
</div>
<div class="form-horizontal">
<div class="form-group">
<span class=" control-label col-sm-2">FileNo:</span>
<div>
<input type="text" class="form-control col-sm-6" placeholder=" Enter FileNo" ng-model="FileNo" required>
</div>
</div>
<div class="form-group">
<span class="control-label col-sm-2">FirstName:</span>
<input type="text" class="form-control col-sm-6" ng-model="FirstName" placeholder="Enter FirsrtName" required>
</div>
<div class="form-group">
<span class="control-label col-sm-2">LastName:</span>
<input type="text" class="form-control col-sm-6" placeholder="Enter LastName" ng-model="LastName">
</div>
<div class="input-group-btn text-center" style="width: 280px;">
<input type="submit" class="btn btn-success buttonPadding" value="Submit" />
</div>
</div>
</div>
</div>
</div>
</form>
</div>
AngularCode.js :
var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope, RegistrationService) {
$scope.Submit = "Save";
$scope.submitted = false;
$scope.message = '';
$scope.isFormValid = false;
$scope.File = {
FileId: '',
FileNo: '',
FirstName: '',
LastName: ''
};
//Check form Validation
$scope.$watch('FileEntryForm.$valid', function (newValue) {
$scope.isFormValid = newValue;
});
//Save Data
$scope.SaveData = function (data) {
if ($scope.Submit === 'Save') {
$scope.submitted = true;
$scope.message = '';
if ($scope.isFormValid) {
$scope.Submit = 'Please Wait...';
$scope.File = data;
RegistrationService.SaveFormData($scope.File).then(function (d) {
alert(d);
if (d === 'Success') {
//have to clear form here
ClearForm();
}
$scope.Submit = "Save";
});
}
else {
$scope.message = 'Please fill required fields value';
}
}
};
//Clear Form (reset)
function ClearForm() {
$scope.File = {};
$scope.FileEntryForm.$setPristine();
$scope.submitted = false;
}
})
.factory('RegistrationService', function ($http, $q) {
var fact = {};
fact.SaveFormData = function (data) {
var defer = $q.defer();
$http({
url: '/FileEntry/AddFileData',
method: 'POST',
data: JSON.stringify(data),
headers: { 'content-type': 'application/json' }
}).success(function (d) {
// Success callback
defer.resolve(d);
}).error(function (e) {
//Failed Callback
alert('Error!');
defer.reject(e);
});
return defer.promise;
};
return fact;
});
Controller.cs :
namespace FMusingAngularjs.Controllers
{
public class FileController : Controller
{
// GET: File
public ActionResult Index()
{
return View();
}
[HttpPost]
public JsonResult AddFileData(File details)
{
string message = "";
if (ModelState.IsValid)
{
using (FileManagementEntities db = new FileManagementEntities())
{
var user = db.Files.Where(a => a.FileNo == details.FileNo).FirstOrDefault();
if (user == null)
{
//Save here
db.Files.Add(details);
db.SaveChanges();
message = "Success";
}
else
{
message = "This Name is already saved in Database";
}
}
}
else
{
message = "Model State is not valid";
}
return new JsonResult { Data = message, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
}
}