从AngularJS表单发布到Web Api

时间:2014-02-02 12:42:46

标签: javascript angularjs asp.net-web-api

我想在我的Api控制器中将我的Angular表单发布到我的Post方法:

    <div data-ng-app="app" ng-controller="FixtureAddController">
    <form name="form" class="col-xs-2" id="form" class="form-horizontal">
        <div class="control-group" ng-class="{error: form.StageName.$invalid}">
            <label class="control-label" for="StageName">Stage Team</label>
            <div class="controls">
                <select class="form-control" ng-model="fixture.StageName" ng-options="stage.StageName as stage.StageName for stage in stages" required>
                    <option style="display:none" value="">Select</option>
                </select>
                <span ng-show="form.StageName.$dirty && form.StageName.$error.required">Stage required</span>
            </div>
        </div>

....other fields....

        <div class="form-actions">
            <button ng-show="form.$valid" ng-click="save()" class="btn btn-primary">{{action}}</button>
            <a href="/Admin/Fixtures/List" class="btn btn-danger">Cancel</a>
        </div>
    </form>
</div>

无论我将ng-model设置为fixture.StageName还是fixture.StageId,它总是将StageName传递给我的Post方法,但我需要它来传递StageId。我如何获得ID?

2 个答案:

答案 0 :(得分:1)

尝试更改ng-options以使用StageId。 另见:Working with select using AngularJS's ng-options

答案 1 :(得分:0)

试试这个:

ng-options="stage.StageId as stage.StageName for stage in stages"