【问题标题】:How to Save an id for a dropdownlist using angular js?如何使用 Angular js 为下拉列表保存 id?
【发布时间】:2015-02-11 04:45:40
【问题描述】:

我在使用 Angularjs 的项目中遇到问题。这是我的代码:

控制器

     // GET: api/CustomerLists/5


    [ResponseType(typeof(CustomerList))]
    public IHttpActionResult GetCustomerList(int id)
    {

        var data = (from cust in db.CustomerLists
            join call24 in db.CallingList4
                on cust.CustID equals call24.CustID

            where cust.CustID == id

            select new CustomerVM
            {
                CustID = cust.CustID,
                Customer = cust.Customer,
                Call4ID = call24.Call4ID,
                Wk1WebID = call24.Wk1WebID,
                Wk1OrdID = call24.Wk1OrdID



            }).First();

        return Ok(data);

    }



  // PUT: api/CustomerLists/5
    [ResponseType(typeof(void))]
    public IHttpActionResult PutCustomerList(CustomerVM vm)
    {

        if (ModelState.IsValid)
        {
            var cust = db.CustomerLists.Find(vm.CustID);
            var call4 = db.CallingList4.Find(vm.Call4ID);

            cust.Customer = vm.Customer;
            cust.ContactName = vm.ContactName;

            call4.Wk1OrdID = vm.Wk1OrdID;
            call4.Wk1WebID = vm.Wk1WebID;


            db.CustomerLists.Attach(cust);
            db.Entry(cust).State = EntityState.Modified;
            db.CallingList4.Attach(call4);
            db.Entry(call4).State = EntityState.Modified;

            db.SaveChanges();
        }


        return StatusCode(HttpStatusCode.NoContent);


    }

JS

  var EditCtrl = function ($scope, $routeParams, $location, data, $http) {

var id = $routeParams.editId;
$scope.lis = data.get({ id: id });

$scope.selectTest = null;
$scope.testTest = [];

$http({
    method: 'GET',
    url: '/api/OrderStatus/',
    data: { OrderStatID: 1 }
}).success(function(result) {
    $scope.testTest = result;
});


$scope.selectTest1 = null;
$scope.testTest1 = [];

$http({
    method: 'GET',
    url: '/api/WebStatus/',
    data: { WebStatID: 1 }
}).success(function (result1) {
    $scope.testTest1 = result1;
});


$scope.save = function () {
    data.update({ id: id }, $scope.lis, function () {
        $location.path('/');
    });
};

模板

<select class="form-control" ng-model="selectTest" ng-model="lis.OrderStatID">
                <option ng-repeat="s in testTest" value="{{s.OrderStatID}}">{{s.OrderStatus}}</option>
            </select>

查看模型

public class CustomerVM
{
    public int CustID { get; set; }
    public string Customer { get; set; }
    public int? Priority { get; set; }
    public string ContactName { get; set; }
    public string ContactNumber { get; set; }
    public int Call4ID { get; set; }
    public int? Wk1WebID { get; set; }
    public int? Wk1OrdID { get; set; }
    public int? OrderStatId { get; set; }
    public string OrderStatus { get; set; }
}

}

当我尝试更新时,CustomerList 详细信息更新得很好,但是当我选择状态并尝试保存 ID 时,CallingList4 详细信息返回 null。如何将 ID 从另一个表保存到调用List24 的主表中?请帮忙

更新

看看这个屏幕截图,它确实从表中选择,但问题是它不想将 id 保存到另一个表。

【问题讨论】:

  • 从您的描述和代码中并不清楚问题出在哪里。例如,您的角度控制器根本不保存任何东西。将保存数据的 JS 和 JSON 的示例以及它反序列化到您的控制器中。
  • 查看 AngularJS 网站上的选择示例:docs.angularjs.org/api/ng/directive/select
  • 它确实保存了.. js 中的保存方法进行了保存.. 它保存了 customerList 但是当我尝试保存 CallingList4 时,下拉列表中的 ID 保存为空...跨度>
  • @BrianMains 请检查我更新的问题

标签: javascript c# asp.net angularjs api


【解决方案1】:

我认为这是设计问题而不是代码问题

您需要重新设计 ViewModel 以包含 Status 和 CallingList 等属性的子类

然后使用 {{s.Status.ID}} 等表达式为所有组合属性传递值

【讨论】:

  • @mGounda 你能协助设计吗
  • @ninjaXnado,我建议您查看this useful hands-on lab。这在一个简单的示例中结合了 ASP.NET MVC、WebAPI 和 AngularJS,涵盖了您的案例等等
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多