Я застрял в коде ниже.Я могу сохранить имя студента, адрес электронной почты и номер мобильного телефона.Но не могу сохранить StateId и CityId.Я не понимаю почему?Пожалуйста, помогите мне здесь.Я использую angularjs с asp.net mvc
Вот мой код.
using AngularJSCrud.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AngularJSCrud.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public JsonResult GetStudents()
{
using (sampleDBContext db = new sampleDBContext())
{
db.Configuration.LazyLoadingEnabled = false;
List<Student> list = db.Students.ToList();
return Json(list, JsonRequestBehavior.AllowGet);
}
}
public JsonResult GetStates()
{
using (sampleDBContext db = new sampleDBContext())
{
db.Configuration.LazyLoadingEnabled = false;
List<State> list = db.States.ToList();
return Json(list, JsonRequestBehavior.AllowGet);
}
}
public JsonResult GetCities(int? StateId)
{
using (sampleDBContext db = new sampleDBContext())
{
db.Configuration.LazyLoadingEnabled = false;
List<City> list = db.Cities.Where(x => x.StateId == StateId).ToList();
return Json(list, JsonRequestBehavior.AllowGet);
}
}
public JsonResult saveStudent(Student model)
{
using (sampleDBContext db = new sampleDBContext())
{
db.Students.Add(model);
db.SaveChanges();
return Json("Saved", JsonRequestBehavior.AllowGet);
}
}
}
}
Вот мой script.js
/// <reference path="angular.min.js" />
var myApp = angular
.module("myApp", [])
.controller("HomeController", function ($scope, $http) {
//load the data
$scope.loadStudents = function () {
$http.get("/Home/GetStudents/").then(function (data) {
$scope.students = data.data;
});
} //end of loadStudent()
$scope.loadStudents();
//loadStates
$http.get("/Home/GetStates/").then(function (data) {
$scope.allStates = data.data;
})
//loadCities
$scope.loadCities = function () {
alert("State Id: " + $scope.StateId);
$http.get("/Home/GetCities?StateId=" + $scope.StateId).then(function (data) {
$scope.allCities = data.data;
})
}
$scope.loadCities();
//Save Student
$scope.saveIt = function () {
$http({
method: 'POST',
url: '/Home/saveStudent',
data: $scope.register
}).then(function (success) {
alert("Record Saved");
$scope.loadStudents();
}, function (error) {
alert("Record Not Saved");
});
}
});
И вот мой индекс.cshtml, где я создал представление
<html ng-app="myApp">
<head>
<title></title>
</head>
<body ng-controller="HomeController">
<div class="container page-header">
<div class="row">
<div class="col-md-8">
<div class="alert alert-success">
Student Data
</div>
<table class="table table-bordered table-responsive">
<tr>
<th>Roll Number</th>
<th>Student Name</th>
<th>State Id</th>
<th>City Id</th>
<th>Email</th>
<th>Mobile Number</th>
</tr>
<tr ng-repeat="student in students">
<td>{{student.sId}}</td>
<td>{{student.sName}}</td>
<td>{{student.StateId}}</td>
<td>{{student.CityId}}</td>
<td>{{student.email}}</td>
<td>{{student.MNO}}</td>
</tr>
</table>
</div>
<div class="col-md-4">
<div class="alert alert-danger">
New Registration Form
</div>
<div class="form-group">
<label>Student Name</label>
<input type="text" ng-model="register.sName" class="form-control" />
<input type="text" ng-model="register.sName" />
</div>
<div class="form-group">
<label>State</label>
<select class="form-control" ng-model="StateId" ng-change="loadCities()">
<option value="">SELECT YOUR STATE</option>
<option ng-repeat="states in allStates" value="{{states.StateId}}">
{{states.StateName}}</option>
</select>
</div>
<div class="form-group">
<label>City</label>
<select ng-model="CityId" class="form-control" >
<option value="">SELECT YOUR CITY</option>
<option ng-repeat="cities in allCities" value="{{cities.CityId}}">
{{cities.CityName}}</option>
</select>
</div>
<div class="form-group">
<label>Email</label>
<input type="text" ng-model="register.email" class="form-control" />
</div>
<div class="form-group">
<label>Mobile Number</label>
<input type="text" ng-model="register.MNO" class="form-control" />
</div>
<div class="form-group">
<input type="Button" class="btn btn-primary" value="Register" ng-click="saveIt()" />
<input type="Reset" class="btn btn-danger" />
</div>
</div>
</div>
</div>
</body>
</html>
Здесь, когда я нажимаю кнопку «Регистрация», сохраняются значения имени ученика, адреса электронной почты и номера мобильного телефона.Но я хочу сохранить StateId и CityId, которые я не смог сделать.