简体   繁体   中英

Posting from AngularJS form to Web Api

I am trying to post my Angular form to my Post method in my Api Controller :

    <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>

Whether I set ng-model to fixture.StageName or fixture.StageId, it always passes the StageName to my Post method, but I need it to pass the StageId. How do I get the Id to be passed?

Try changing ng-options as well to use StageId. See also: Working with select using AngularJS's ng-options

尝试这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM