Angular.js CRUD操作

时间:2016-10-21 12:59:55

标签: angularjs model-view-controller crud

我正在学习使用angular js和asp.net MVC开发crud orations。我目前开发了这个解决方案。我的插入功能工作正常但文本字段中的显示数据(GetEmployeeById)无法正常工作,任何人都可以指出我在哪里做错了谢谢。

我的观点

address2

我的控制器

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<table  style="margin-bottom: 10px;">
    <tr>
        <th>
            First Name
        </th>
        <th>
            Last Name
        </th>
        <th>
            Address
        </th>
        <th>
            City
        </th>
        <th>
            Region
        </th>
        <th>
            Postal Code
        </th>
    </tr>
    <tr>
        <td>
            <input type="text" name="employee.FirstName" ng-model="employee.FirstName" value="{{employee.FirstName}}" />
        </td>
        <td>
            <input type="text" name="employee.LastName" ng-model="employee.LastName" value="{{employee.LastName}}" />
        </td>
        <td>
            <input type="text" name="employee.Address" ng-model="employee.Address" value="{{employee.Address}}" />
        </td>
        <td>
            <input type="text" name="employee.City" ng-model="employee.City" value="{{employee.City}}" />
        </td>
        <td>
            <input type="text" name="employee.Region" ng-model="employee.Region" value="{{employee.Region}}" />
        </td>
        <td>
            <input type="text" name="employee.PostalCode" ng-model="employee.PostalCode" value="{{employee.PostalCode}}" />
        </td>
    </tr>
    <tr>
        <td>
            <input type="submit" value="Save" name="btnSave" ng-click="savedata(employee)" class="btn btn-primary" />
        </td>
    </tr>
</table>



<div class="row" >
    <table class="table table-bordered table-condensed">
        <tr>
            <th>
                Employee ID
            </th>
            <th>
                First Name
            </th>
            <th>
                Last Name
            </th>
            <th>
                Address
            </th>
            <th>
                City
            </th>
            <th>
                Region
            </th>
            <th>
                Postal Code
            </th>
            <th>
                Select Employee
            </th>
        </tr>
        <tr ng-repeat="e in employees">
            <td>{{e.EmployeeID}}</td>
            <td>{{e.FirstName}}</td>
            <td>{{e.LastName}}</td>
            <td>{{e.Address}}</td>
            <td>{{e.City}}</td>
            <td>{{e.Region}}</td>
            <td>{{e.PostalCode}}</td>
            <td>
                <a href="#" ng-click="selectEmployee(e.EmployeeID)">Select</a>
            </td>
        </tr>
    </table>
</div>  

我的角度Js app控制器

public class EmployeeController : Controller
    {
        NorthwindEntities _db=new NorthwindEntities();
        // GET: Employee
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetEmployee()
        {


            return Json(_db.Employees.Select(x=>new
            {
                x.EmployeeID,x.FirstName,x.LastName,x.Address,x.City,x.Region,x.PostalCode
            }), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult SaveEmployee(Employee employee)
        {
            _db.Employees.Add(employee);
            _db.SaveChanges();

            return Json(_db.Employees.Select(x => new { x.EmployeeID,x.FirstName, x.LastName, x.Address, x.City, x.Region, x.PostalCode }), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult GetEmployeeById(int EmployeeID)
        {
            Employee employee = _db.Employees.Find(EmployeeID);
            return Json(employee, JsonRequestBehavior.AllowGet);
        }
    }

0 个答案:

没有答案