I have created a stored procedure to get corresponding results from sql server db which contains only the chosen date from the datepicker directive in my Angular App. I don't know how to access from WebAPI to the stored procedure in my SQL Server DB. In the following code I show you my approach:
Created stored Procedure in SQL Server:
USE [SAMPLE_DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE getDatum
@Dt datetime
AS
BEGIN
SET NOCOUNT ON;
SELECT b.Id, b.Datum, b.num_aucts
FROM dbo.Beispiels AS b
WHERE b.Datum = @Dt
END
GO
Ctrl in Angular:
//Resource from WebAPI
var Resource = $resource(baseUrl + '/api/beispiels/:Id', {
Id: '@Id'
}, {});
$scope.list = Resource.query();
//definitions of Datepicker (UIbootstrap)
$scope.open = function ($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
$scope.dateOptions = {
formatYear: 'yy',
startingDay: 1
};
View in my Angular App:
<div class="row">
<div class="col-md-6">
<p class="input-group">
<input type="text"
class="form-control"
datepicker-popup
ng-model="dt"
is-open="opened"
min-date=""
max-date=""
datepicker-options="dateOptions"
ng-required="true"
close-text="Close"
/>
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open($event)">
<i class="glyphicon glyphicon-calendar"></i>
</button>
</span>
</p>
</div>
</div>
<div class="row">
<div class="col-md-6">
<table class="table table-bordered">
<th>Id</th>
<th>Datum</th>
<th>Num Aucts</th>
<tr ng-repeat="liste in list">
<td>{{ liste.Id }}</td>
<td>{{ liste.Datum }}</td>
<td>{{ liste.num_aucts }}</td>
</tr>
</table>
</div>
</div>
WebAPI Controller:
...
public class BeispielsController : ApiController
{
private EasContext db = new EasContext();
// GET: api/Beispiels
[HttpGet]
public IQueryable<Beispiel> GetBeispiels()
{
return db.Beispiels.AsQueryable();
}
...
}
Db Context:
public partial class EasContext : DbContext
{
public EasContext()
: base("name=EasContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual ObjectResult<getDatum> getDatum(Nullable<System.DateTime> dt)
{
var dtParameter = dt.HasValue ?
new ObjectParameter("Dt", dt) :
new ObjectParameter("Dt", typeof(System.DateTime));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<getDatum>("getDatum", dtParameter);
}
...
I want to synchronize the ngModel value dt
with the value from the stored procedure @Dt
, that means, when the user click on the datepicker and chooses a date. the date will displayed in the ngModel input field and then it should be insert to the stored procedure and returns the result only for example all data which date have "2013/01/03" or something else.