【问题标题】:When I run my code it shows 500 error with POST and GET methods当我运行我的代码时,它使用 POST 和 GET 方法显示 500 错误
【发布时间】:2015-05-14 15:38:47
【问题描述】:

我是mvvm 的新手,谁能帮我根据所选日期显示详细信息。当我运行我的代码时,它显示 500 错误与 postget 方法。

Schedule.cshtml

<div class="col-lg-8" ng-app="ScheduleApp" ng-controller="ScheduleController" ng-cloak>
                    <table align="center">
                        <tr>
                            <td>Select Date</td>
                            <td>
                                                               <input type="date" class="form-control" id="dt" name="dt">
                                <input type="button" ng-click="vm.GetDetails(dt)" value="check" class="btn btn-default"/>
                            </td>
                        </tr>
                    </table>
                    <table align="center" border="1">
                        <tr>
                            <td><b>Trip ID</b></td>
                            <td><b>Booked By</b></td>
                            <td><b>Phone</b></td>
                            <td><b>Email</b></td>
                            <td><b>Car</b></td>
                            <td><b>Driver</b></td>
                            <td><b>Time</b></td>
                        </tr>
                        <tr ng-repeat="c in vm.books">
                            <td><p id="p1">{{c.TripId}}</p></td>
                            <td><p id="p2">{{c.BookedBy}}</p></td>
                            <td><p id="p3">{{c.Phone}}</p></td>
                            <td><p id="p4">{{c.Email}}</p></td>
                            <td><p id="p5">{{c.CarId}}</p></td>
                            <td><p id="p6">{{c.DriverId}}</p></td>
                            <td><p id="p7">{{c.StartTime}}</p></td>
                        </tr>
                    </table>
                </div>

ScheduleController.cs

    public JsonResult GetData(DateTime dt)
    {
        try
        {

            babyEntities1 ctx = new babyEntities1();
            var bookings = from c in ctx.Trips
                        where c.Date == dt
                           select new
                           {
                               c.TripId,
                               c.BookingNo,
                               c.Date,
                               c.StartTime,
                               c.EndTime,
                               c.AccountId,
                               c.BookedBy,
                               c.PaxId,
                               c.PaxNo,
                               c.Phone,
                               c.SourceId,
                               c.SourceAddressId,
                               c.DestinationID,
                               c.DestAddressId,
                               c.Via,
                               c.Flight,
                               c.PaxComments,
                               c.TripEnd,
                               c.CarId,
                               c.DriverId,
                               c.PayId,
                               c.Amount,
                               c.Toll,
                               c.Parking,
                               c.FuelSur,
                               c.CardId,
                               c.AdminFee,
                               c.ExtraFee,
                               c.FeeComments,
                               c.UserId,
                               c.TotalAmount
                           };

            return Json(bookings, JsonRequestBehavior.AllowGet);
        }
        catch
        {
            return Json(new { msg = "error" }, JsonRequestBehavior.AllowGet);
        }
    }

ScheduleController.ts

module Main.Schedule {

    export interface IScheduleScope extends ng.IScope {
        vm: ScheduleController;

    }

    export class ScheduleController {
        public isBusy: boolean;
        public books: Main.Book.Book;
        //Default constructor
        constructor(private $scope: IScheduleScope, private $http: ng.IHttpService) {
            $scope.vm = this;
           // this.books = new Main.Book.Book();
           // var dt: Date;
          //  this.GetData(dt);
        }
        public GetDetails(dt: Date): void {
            this.$http.post("/Schedule/GetData", { Obj: this.GetData }).success((data: any, status: any) => {
                if (data.msg == 'success') {
                    this.GetData(dt);
                    alert("Saved");
                }
                else
                    alert('Error');
            }).error((data: any, status: any) => {
                    alert("Error");
                });
        }

        public GetData(dt: Date): void {
                this.$http.get("/Schedule/GetData").success((data: any, status: any) => {
                this.books = data;
                this.isBusy = false;
                console.log(data);
            }).error((data: any, status: any) => {
                    alert("Error");
            });
        }
    }
}

【问题讨论】:

  • 错误是什么?你知道错误是服务器端还是客户端?您是否在您选择的浏览器中使用开发工具对其进行了调试?你见过原始的 HTTP 请求和响应吗?发布您的所有代码并要求我们查找您的问题不太可能得到您希望的响应。
  • @Bill 发布 500 内部服务器错误
  • 这意味着你的服务器抛出了一个未处理的异常。您需要从服务器获取更详细的错误日志记录,或者最好调试服务器并逐步查看错误发生的位置。

标签: c# angularjs mvvm typescript


【解决方案1】:

谢谢大家。我整理了一下,更新后的代码是……

 public GetDetails(dt: string): void {
        this.$http.get("/Schedule/GetData", { params: { dt: dt } }).success((data: any, status: any) => {
            this.tripview = data;
            console.log(data);
        }).error((data: any, status: any) => {
            console.log(data);
            });
    }

    public GetData(dt: string): void {
        this.$http.get("/Schedule/GetData", { params: {} }).success((data: any, status: any) => {
            this.tripview = data;
            this.isBusy = false;
            console.log(data);
        }).error((data: any, status: any) => {
                alert("Error");
        });
    }

【讨论】:

    【解决方案2】:

    您没有将 dateTime 传递给请求。

    public GetData(dt: Date): void {
                    this.$http.get("/Schedule/GetData").suc
    

    我想这就是你的控制器返回 500 的原因。 改为考虑

    public GetData(dt: Date): void {
                        this.$http.get("/Schedule/GetData?dt=" + dt.toString()).suc
    

    但我很确定你必须以 c# 和 javascript 都能理解的方式格式化你的日期时间

    【讨论】:

      猜你喜欢
      • 2018-12-09
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2023-03-25
      • 1970-01-01
      • 2022-12-24
      • 1970-01-01
      相关资源
      最近更新 更多