Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi All,

Basically I am learning angular and C#.Net.. Which I am beginning to understand a little bit now.

I have crated a database in which I am adding records from the users input like so:

C#
[HttpPost]

       public HttpResponseMessage PostDs72(Persons p)
       {

           if (p == null)
               return  new HttpResponseMessage(HttpStatusCode.BadGateway);
           PeopleContext db = new PeopleContext();
           db.Persons.Add(p);
           db.SaveChanges();
           return  new HttpResponseMessage(HttpStatusCode.Created);

       }


in the code above is the c# code that is adding or creating a new person in the table Persons.

And in angular side of things this is what I have done:

PHP
$scope.createPerson = function () {
           $http({
               method: 'POST',
               url: '/api/Persons',
               data: JSON.stringify($scope.newperson),
               headers: { 'Content-Type': 'application/JSON' }

           }).success(function (data) {

               $scope.status = " Saved";
               Getinfo();
           }).error(function (error) {

               $scope.status = 'Unabale to save ' + error.message;
           });
       };


this is the angular that code that is creating a new person. or function creating a new person

and in my HTML this is what I have done
HTML
<input type="text" name="FirsName" data-ng-model="newperson.FirstName" placeholder="Enter First Name" required />
           <input type="text" name="LastName" data-ng-model="newperson.Last" placeholder="Enter Last Name" required />
           <input class="search" type="button" value="Add Person" data-ng-show="DisplaySave" data-ng-click="createPerson()"



And that is how I am adding people onto my database.

Now I am stuck! How do I go on in about search for record by their last name provided in a search textbox? can some one plsease help?

If this was windows forms I would code something like

C#
SELECT * FROM Persons WHERE LastName = '"textbox1.text"'


now ofcourse this is not winsforms but is there anyone that can help?

Thank you
Posted

1 solution

I think below code will help you:

C# code is for Web API

C#
[HttpGET]
public HttpResponseMessage Get([FromUri]string name)
{
    PeopleContext db = new PeopleContext();
    Person person = db.Persons.FirstOrDefault(x => x.Name == name); //Get the person by name
    return  new HttpResponseMessage(person);
 }


Below code is for AngularJS you have to bind the search textbox with your angularJS field i.e. data-ng-model="searchName" and you can retrive this value by using $scope.searchName.

JavaScript
$scope.getPerson = function (string name) {
           $http({
               method: 'GET',
               url: '/api/Persons?name=' + name,
               headers: { 'Content-Type': 'application/JSON' }
 
           }).success(function (data) {
 //Your Code to displaying Person details on WEb Page
           }).error(function (error) {
 
               $scope.status = 'Unabale to save ' + error.message;
           });
       };


Please don't ask how to bind this with HTML page because that you have to do it from your own.

Also read EntityFramework and AngularJS
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900