我在使用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保存到另一个表。
答案 0 :(得分:0)
我认为这是一个设计问题,而不是代码问题
您需要重新设计ViewModel以包含Status和CallingList等属性的子类
然后使用像{{s.Status.ID}}这样的表达式传递值,等等所有组合的属性