LoginSignup
0

More than 5 years have passed since last update.

Angular 入力フォームのデータをサーバーに送信する

Posted at

データを送信する部分

View側(HTML)
html
<input type="button" value="Save" class="btn btn-default" ng-click="save()" />

コントローラー(JS Angular)
js
$scope.save = function () {
$http.post('/Api/HogeController/', $scope.data)
.then(function (response) { $location.path("home"); });
}

サーバー側
```csharp

[ResponseType(typeof(HogeDTO))]
public async Task Post(Hoge model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

db.Hoges.Add(model);

try
{
    await db.SaveChangesAsync();
}
catch (DbUpdateException)
{
    if (HogesExists(model.ID))
    {
        return Conflict();
    }
    else
    {
        throw;
    }
}
var ret = await db.Hoges.Select(HogeItemDTO.SELECT).FirstOrDefaultAsync(x => x.ID == model.ID);
return CreatedAtRoute("DefaultApi", new { id = model.ID }, model);

}
```

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0