AngularJS and ASP.NET MVC data insert issue - angularjs

I am inserting Data using AngularJS and ASP.NET MVC.
But I am facing issue that the data is inserted null.
What should I do?
This is AngularJS file:
var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope, $http) {
debugger;
$scope.InsertData = function () {
var Action = document.getElementById("btnSave").getAttribute("value");
if (Action == "Submit") {
$scope.User = {};
$scope.User.Username = $scope.Username;
alert($scope.Username);
$scope.User.Password = $scope.Password;
$scope.User.Email = $scope.Email;
$scope.User.Phone = $scope.Phone;
$scope.User.DOB = $scope.DOB;
$http({
method: "post",
url: "http://localhost:2776/Account/Insert_Employee",
datatype: "json",
data: JSON.stringify($scope.User)
}).then(function (response) {
alert("inserted");
});
}
};
});
This is HTML file:
<div class="tab-content" ng-controller="myCtrl">
<div class="tab-pane active" id="register">
<h3>Register Now !!!</h3>
<p class="text-muted">Be cool and join today. Meet millions</p>
<!--Register Form-->
<form name="registration_form" id='registration_form' class="form-inline">
<div class="row">
<div class="form-group col-xs-6">
<label for="Username" class="sr-only">User Name</label>
<input id="Username" ng-model="Username" class="form-control input-group-lg" type="text" name="Username" title="Enter User name" placeholder="User name" />
</div>
</div>
<div class="row">
<div class="form-group col-xs-12">
<label for="email" class="sr-only">Email</label>
<input ng-model="Email" id="email" class="form-control input-group-lg" type="text" name="Email" title="Enter Email" placeholder="Your Email" />
</div>
</div>
<div class="row">
<div class="form-group col-xs-12">
<label for="Phone" class="sr-only">Phone</label>
<input ng-model="Phone" id="Phone" class="form-control input-group-lg" type="text" name="Phone" title="Enter Phone" placeholder="Your Phone" />
</div>
</div>
<div class="row">
<div class="form-group col-xs-12">
<label for="password" class="sr-only">Password</label>
<input ng-model="Password" id="password" class="form-control input-group-lg" type="password" name="password" title="Enter password" placeholder="Password" />
</div>
</div>
<div class="row">
<p class="birth"><strong>Date of Birth</strong></p>
<div class="form-group col-sm-3 col-xs-6">
<label for="month" class="sr-only"></label>
<select class="form-control" id="day">
<option value="Day" disabled selected>Day</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
</div>
<div class="form-group col-sm-3 col-xs-6">
<label for="month" class="sr-only"></label>
<select class="form-control" id="month">
<option value="month" ng-model="DOB" disabled selected>Month</option>
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
<option>May</option>
<option>Jun</option>
<option>Jul</option>
<option>Aug</option>
<option>Sep</option>
<option>Oct</option>
<option>Nov</option>
<option>Dec</option>
</select>
</div>
<div class="form-group col-sm-6 col-xs-12">
<label for="year" class="sr-only"></label>
<select class="form-control" id="year">
<option value="year" disabled selected>Year</option>
<option>2000</option>
<option>2001</option>
<option>2002</option>
<option>2004</option>
<option>2005</option>
<option>2006</option>
<option>2007</option>
<option>2008</option>
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
</select>
</div>
</div>
<div class="form-group gender">
<label class="radio-inline">
<input type="radio" name="optradio" checked>Male
</label>
<label class="radio-inline">
<input type="radio" name="optradio">Female
</label>
</div>
</form><!--Register Now Form Ends-->
<p>Already have an account?</p>
<button id="btnSave" value="Submit" class="btn btn-primary" ng-click="InsertData()">Register Now</button>
</div>
</div>
This is ASP.NET controller:
public string Insert_Employee(User User)
{
if (User != null)
{
using (Database1Entities Obj = new Database1Entities())
{
Obj.Users.Add(User);
Obj.SaveChanges();
return "Employee Added Successfully";
}
}
else
{
return "Employee Not Inserted! Try Again";
}
}
I am entering data, but data is inserted as null into db. What should I do no?
I have added <body ng-app="myApp">
According to me there is no error
can anyone help?

Try to wrote in this way your ajax call:
$http({
method: "post",
url: "http://localhost:2776/Account/Insert_Employee",
datatype: "json",
data: {user:$scope.User}
}).then(function (response) {
alert("inserted");
});

Related

How to post an image with other data in the json file using angularjs form

I'm trying to create multiple form under tabs with previous tab data been used in the next tab and each form has a save button which saves the data for the next tab using angularjs. Everything is working fine with code. All of the form data is added to single field in the model which is a JSONField. Except I have a form which has a file field usually images I am stuck with the upload of these images to the json field.
var app = angular.module('app', []);
app.controller('enviromentController', function ($scope, $http) {
$scope.saveEnvironment = function () {
$http({
method: 'PUT',
"url": "{% url 'core:api:edit-environement' env_id=env_id %}",
"data": angular.copy($scope.env),
"headers": {
"accept": "application/json",
"content-type": "application/json",
"Authorization": "Token " + localStorage.getItem('token')
}
}).then(function () {
inform("Saved", "Published data");
});
this is my form
<div class="tab-content" id="myTabContent" style="min-height:375px">
<div class="tab-pane active" id="environment" role="tabpanel" aria-labelledby="environment-tab">
<div class="form-group col-lg-5">
<label class="form-control-label">Title </label>
<input type="text" class="form-control" ng-model="env.title" required />
</div>
<div class="form-group col-lg-5">
<label class="form-control-label">Department</label>
<select class="form-control" ng-model="env.department"
ng-options="c.id as c.name for c in departments" required>
</select>
</div>
<div class="form-group col-lg-5">
<label class="form-control-label" for="appname">App Name</label>
<select class="form-control" ng-model="env.app_name"
ng-options="c.id as c.name for c in app_name" required>
</select>
</div>
</div>
<div class="tab-pane" id="room" role="tabpanel" aria-labelledby="room-tab">
<div class="row">
<div class="form-group col-lg-4">
<label class="form-control-label">Name</label>
<input type="text" class="form-control" ng-model="env.json_data1.room.name" />
</div>
<div class="form-group col-lg-4">
<label class="form-control-label">Height </label>
<input type="text" class="form-control" ng-model="env.json_data1.room.height" />
</div>
<div class="form-group col-lg-4">
<label class="form-control-label">Width </label>
<input type="text" class="form-control" ng-model="env.json_data1.room.width" />
</div>
<div class="form-group col-lg-5">
<label class="form-control-label">Length </label>
<input type="text" class="form-control" ng-model="env.json_data1.room.length" />
</div>
<div class="form-group col-lg-5">
<label class="form-control-label">Texture </label>
<input type="text" class="form-control" ng-model="env.json_data1.room.texture" />
</div>
<div class="form-group col-lg-5">
<label class="form-control-label">Room Image </label>
<input type="file" class="dropify"
file-model="env.json_data1.room.room_floorplan" accept="image/*" />
</div>
<div class="form-group col-lg-5">
<label class="form-control-label">Floorplan </label>
<input type="file" class="dropify"
accept="image/*" />
</div>
<div class="form-group col-lg-2 p-4">
<label class="form-control-label"> </label>
<button type="button" ng-click="addRoom(env.json_data1.room)"
class="btn btn-primary">{{env.json_data1.room.stage||'Add'}}</button>
</div>
<table class="table">
<tr>
<th>Name</th>
<th>Texture</th>
<th>Size</th>
<th>Actions</th>
</tr>
<tr ng-repeat="room in env.json_data1.rooms">
<td>{{room.name}}</td>
<td>{{room.texture}}</td>
<td>{{room.height}} x {{room.width}} x {{room.length}}</td>
<th><button type="button" ng-click="editRoom(room)">Edit</button></th>
</tr>
</table>
</div>
</div>
$scope.addRoom = function (rec) {
if (!rec.stage) {
rec.stage = 'Update';
$scope.env.json_data1.rooms.push(rec);
}
$scope.env.json_data1.room = {};
}

null value passed from angularjs frontend to jersey backend

i am using angular front end and java jersey as back end .when i submit data from the form ,null values are passed instead of the actual form data. This is my form
<div id="main">
<h1>Create Leave</h1>
<form class="form-horizontal" >
<div class="form-group">
<label for="employeeName" class="col-sm-3 control-label">Employee Name</label>
<div class="col-sm-6">
<input type="text" id="num" class="form-control" ng-model="num" />
</div>
<div class="col-sm-3"></div>
</div>
<div class="form-group">
<label for="leaveType" class="col-sm-3 control-label">Leave Type</label>
<div class="col-sm-2">
<select id="leaveType" class="form-control" ng-model="leaveType">
<option value="hospital">Hospital</option>
<option value="l1">leave type 2</option>
<option value="l2">leave type 3</option>
<option value="l3">leave type 4</option>
<option value="l4">leave type 5</option>
<option value="l5">leave type 6</option>
</select>
</div>
<div class="col-sm-7"></div>
</div>
<div class="form-group">
<label for="leaveStartDate" class="col-sm-3 control-label">Leave Start Date</label>
<div class="col-sm-2">
<input type="date" id="startDate" class="form-control" ng-model="startDate" />
</div>
<div class="col-sm-7"></div>
</div>
<div class="form-group">
<label for="leaveEndDate" class="col-sm-3 control-label">Leave End Date</label>
<div class="col-sm-2">
<input type="date" id="endDate" class="form-control" ng-model="endDate" />
</div>
<div class="col-sm-7"></div>
</div>
<div class="form-group">
<div class="col-sm-3"></div>
<div class="col-sm-2">
<span><b>Is Half Day leave</b></span>
<div class="radio">
<label><input value="Yes" type="radio" name="halfDay" ng-model="isHalfDay" />Yes</label>
</div>
<div class="radio">
<label><input value="No" type="radio" name="halfDay" ng-model="isHalfDay" />No</label>
</div>
</div>
</div>
<button ng-click="add()" class="btn btn-primary">Submit</button>
<button ng-click="resetForm()" class="btn">Reset</button>
</form>
and this is the controller
app.controller("MyAddController", function($scope, $http) {
//$scope.test = {};
$scope.add = function() {
// console.log("------------>"+JSON.stringify($scope.test));
$http.post("webapi/blog/create", {
params: {
signum: $scope.num,
leaveType: $scope.leaveType,
startDate: $scope.startDate,
endDate: $scope.endDate,
isHalfDay: $scope.isHalfDay
}
}).success(function(data, status, headers, config) {
if (data) {
$scope.data = data;
alert("success");
}
}).error(function(data, status, headers, config) {
alert("error");
})
}
});
and the jersey code
#POST
#Path("create")
#Produces({ "application/json" })
public String create(#BeanParam LeaveDetails ld) {
System.out.println("Entered here");
System.out.println(ld.getIsHalfDay());
System.out.println(ld.getNum());
System.out.println(ld.getEndDate());
System.out.println(ld.getStartdate());
System.out.println(ld.getLeaveType());
new AddLeaveDao().addDetails(ld);
System.out.println("Returned here");
return "{}";
}
Please help

Angularjs bad argument ng:areq error

I'm getting this error when running my application. This error comes in only one controller. I have done other controllers in the same way but in this particular controller I get this following error.
Argument 'questionAddCtrl' is not a function, got undefined
Controller:
;
(function () {
'use strict';
angular.module('app').controller('questionAddCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.data = {
question: '',
ans1: '',
ans2: '',
ans3: '',
ans4: '',
ans5: '',
correct_ans: ''
};
$scope.submit = function (selectData) {
console.log("submit pressed");
var questionAddRequest = {
"question": selectData.question,
"answerOne": selectData.ans1,
"answerTwo": selectData.ans2,
"answerThree": selectData.ans3,
"answerFour": selectData.ans4,
"answerFive": selectData.ans5,
"correctAnswer": selectData.correct_ans
};
var url = 'http://localhost/AwtCW2012002/api/restApiController/question.json';
$scope.jsonData = JSON.stringify(questionAddRequest);
console.log(questionAddRequest);
console.log();
$http({
method: 'POST',
url: url,
data: questionAddRequest
}).then(function (response) {
console.log(response);
$scope.data = {
question: '',
ans1: '',
ans2: '',
ans3: '',
ans4: '',
ans5: '',
correct_ans: ''
};
});
}
}]);
})();
Views:
<div class="container" ng-controller="questionAddCtrl">
<form class="form-horizontal" role="form" name='quizAdd' ng-submit="submit(data)">
<div class="form-group">
<label class="control-label col-sm-2" for="question">Question:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="question" ng-model="data.question" placeholder="Enter Question">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer1">Answer 1:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer1" ng-model="data.ans1" id="answer1" placeholder="Enter Answer 1">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer2">Answer 2:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer2" ng-model="data.ans2" id="answer2" placeholder="Enter Answer 2">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer3">Answer 3:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer3" ng-model="data.ans3" id="answer4" placeholder="Enter Answer 3">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer4">Answer 4:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer4" id="answer4" ng-model="data.ans4" placeholder="Enter Answer 4">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer5">Answer 5:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer5" id="answer5" ng-model="data.ans5" placeholder="Enter Answer 5">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="sel1">Select Correct Answer:</label>
<div class="col-sm-10">
<select class="form-control" ng-model="data.correct_ans" id="sel1">
<option>{{data.ans1}}</option>
<option>{{data.ans2}}</option>
<option>{{data.ans3}}</option>
<option>{{data.ans4}}</option>
<option>{{data.ans5}}</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
Try to set the array of dependencies that your module requires, like so:
angular.module('app', []);
instead of
angular.module('app');

passing data from controller to placeholders of the view in angularjs

I'm getting set of data by calling a GET method in angularjs.
controller
$scope.edit = function (id) {
var edit_url = 'http://localhost/AwtCW2012002/api/restApiController/editQuestion.json?question_id=' + id;
$http.get(edit_url)
.success(function (data) {
console.log(data);
})
.error(function () {
})
};
Data from the GET method is like follows
How can I pass the data into respective fields in my view (into placeholders, as this view is used to edit existing data)
view
<div class="container" ng-controller="questionEditCtrl">
<form class="form-horizontal" role="form" name='quizAdd' ng-submit="submit(data)">
<div class="form-group">
<label class="control-label col-sm-2" for="question">Question:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="question" ng-model="data.question" placeholder="Enter Question">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer1">Answer 1:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer1" ng-model="data.ans1" id="answer1" placeholder="Enter Answer 1">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer2">Answer 2:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer2" ng-model="data.ans2" id="answer2" placeholder="Enter Answer 2">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer3">Answer 3:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer3" ng-model="data.ans3" id="answer4" placeholder="Enter Answer 3">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer4">Answer 4:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer4" id="answer4" ng-model="data.ans4" placeholder="Enter Answer 4">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="answer5">Answer 5:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="answer5" id="answer5" ng-model="data.ans5" placeholder="Enter Answer 5">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="sel1">Select Correct Answer:</label>
<div class="col-sm-10">
<select class="form-control" ng-model="data.correct_ans" id="sel1">
<option>{{data.ans1}}</option>
<option>{{data.ans2}}</option>
<option>{{data.ans3}}</option>
<option>{{data.ans4}}</option>
<option>{{data.ans5}}</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
You are getting the response in array. Use ng-repeat to render you fields. What you perform ng-repeat, you can add placeholder like,
placeholder="Write Answer {{$index}}"
Firstly, populate a $scope variable with the data from the api for it to be available for binding in the template:
$http.get(edit_url)
.success(function (data) {
$scope.data = data; //populate a scope variable
})
.error(function () {
});
Then, you can use one time expressions :: for your placeholders as follows:
placeholder="{{::data.ans1}}"
This will ensure only the default value of data.ans1 which was fetched from the api will be used as the placeholder. All subsequent changes to data.ans1 will change the value, but the placeholder will remain the same.

Backbone ModelBinder, not filling when model is fetched

Maybe I'm using model binder incorrectly or maybe marionette interferes, but my view and model don't seem to be communicating and therefore not pre-filling my template fields
View
define([
'marionette',
'underscore',
'text!app/views/templates/user/form.html',
'app/models/user'
],
function (Marionette, _, Template, Model) {
"use strict"
return Marionette.ItemView.extend({
events: {
'submit .edit-user-form': 'onClickSave'
},
initialize: function(options) {
/* initiate model binder */
Backbone.ModelBinder.bind(Model, this.$el)
/* create empty model in case its a create request */
this.model = new Model()
/* if the options.id is passed then lets load an instance of the model */
if (options && options.id) {
this.model = new Model({id: options.id})
this.model.set('id', options.id)
/* set that to this so its acceptable inside the fetch */
var that = this
this.model.fetch({
/* fetch request successful */
success: function (response) {
/* set the model instance trigger a re-render */
that.model = response
that.render()
},
/* we couldn't load the model so we go back to the users list */
error: function () {
alert('User could not be loaded, redirecting you to the users list')
window.location.hash = 'users'
}
})
}
},
/* save button triggered so prevent default and trigger the model to save */
onClickSave: function (ev) {
ev.preventDefault()
this.model.save({}, {
success: function (response) {
console.log(response, 'response')
}
})
},
/* render the form */
render: function () {
var html = _.template($(Template).html(), this.model.toJSON())
this.$el.html(html)
return this
}
})
}
)
Template
<script type="text/template" id="userFormTemplate">
<div id="userForm">
<h2><img src="/img/icons/32/update.png" /> Update User</h2>
<h2><img src="/img/icons/32/create.png" /> Create New User</h2>
<form class="edit-user-form">
<fieldset name="personal" class="halfWidth left">
<legend>Personal Details:</legend>
<div class="control-group">
<label class="control-label">First name:</label>
<div class="controls">
<input type="text" name="first_name" id="first_name">
</div>
</div>
<div class="control-group">
<label class="control-label">Last name:</label>
<div class="controls">
<input type="text" name="last_name" id="last_name">
</div>
</div>
<div class="control-group">
<label class="control-label">Birthdate:</label>
<div class="controls">
<input type="date" name="birthdate" id="birthdate">
</div>
</div>
</fieldset>
<fieldset name="job" class="halfWidth right">
<legend>Job Details:</legend>
<div class="control-group">
<label class="control-label">Job Title</label>
<div class="controls">
<input type="text" name="job_title" id="job_title">
</div>
</div>
<div class="control-group">
<label class="control-label">Start Date:</label>
<div class="controls">
<input type="date" name="job_start_date" id="job_start_date">
</div>
</div>
<div class="control-group">
<label class="control-label">Probation Ends:</label>
<div class="controls">
<input type="date" name="job_probation_ends" id="job_probation_ends">
</div>
</div>
</fieldset>
<div class="clearfix"></div>
<br />
<fieldset name="personal" class="halfWidth left">
<legend>Work Details:</legend>
<div class="control-group">
<label class="control-label">Work Email</label>
<div class="controls">
<input type="email" name="work_email" id="work_email">
</div>
</div>
<div class="control-group">
<label class="control-label">Work Address:</label>
<div class="controls">
<input type="text" name="work_address" id="work_address">
</div>
</div>
<div class="control-group">
<label class="control-label">Work Phone Number:</label>
<div class="controls">
<input type="text" name="work_phone_number" id="work_phone_number">
</div>
</div>
</fieldset>
<fieldset name="personal" class="halfWidth right">
<legend>Personal Details:</legend>
<div class="control-group">
<label class="control-label">Personal Email</label>
<div class="controls">
<input type="email" name="personal_email" id="personal_email">
</div>
</div>
<div class="control-group">
<label class="control-label">Home Address:</label>
<div class="controls">
<input type="text" name="personal_address" id="personal_address">
</div>
</div>
<div class="control-group">
<label class="control-label">Home Phone Number:</label>
<div class="controls">
<input type="text" name="personal_phone_number" id="personal_phone_number">
</div>
</div>
</fieldset>
<div class="clearfix"></div>
<br />
<div class="control-group button">
<button class="btn save-form btn-success" type="submit">Create User</button>
</div>
</form>
</div>
</script>
You have to define the bindings, its not automatic.
var bindings = {
first_name: '#first_name',
last_name: '#last_name',
birthdate: '#birthdate',
job_title: '#job_title',
job_start_date: '#job_start_date',
job_probation_ends: '#job_probation_ends',
work_email: '#work_email',
work_address: '#work_address',
work_phone_number: '#work_phone_number',
personal_email: '#personal_email',
personal_address: '#personal_address',
personal_phone_number: '#personal_phone_number'
}

Resources