那里出错了? 怎样修改成正确代码?
时间: 2023-10-10 19:08:43 浏览: 47
这段代码存在一些语法错误和逻辑错误,需要进行修改。以下是修改后的正确代码:
```csharp
using DongSuWei01.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace DongSuWei01.Controllers
{
public class HomeController : Controller
{
private Service st = new Service();
// GET: Home
public ActionResult Index()
{
return View();
}
public ActionResult GetAll(int fId = 0, string flightNo = null, string departureAirport = null, string arriveAirport = null)
{
// 查询所有学生的控制器
string sql = "select * from flights where 1=1";
if (fId != 0)
{
sql += " and fId=" + fId;
}
if (!string.IsNullOrEmpty(flightNo))
{
sql += " and flightNo like '%" + flightNo + "%'";
}
if (!string.IsNullOrEmpty(departureAirport))
{
sql += " and departureAirport like '%" + departureAirport + "%'";
}
if (!string.IsNullOrEmpty(arriveAirport))
{
sql += " and arriveAirport like '%" + arriveAirport + "%'";
}
var flights = st.getAllFlights(sql);
return Json(new { code = 0, msg = "执行成功", count = flights.Count, data = flights });
}
public ActionResult Add(string flightNo, string departureAirport, string arriveAirport, string arriveTime, string price, string meals)
{
// 添加控制器
string sql = "INSERT INTO flights (flightNo, departureAirport, arriveAirport, arriveTime, price, meals) " +
"VALUES ('" + flightNo + "', '" + departureAirport + "', '" + arriveAirport + "', '" + arriveTime + "', " + price + ", '" + meals + "')";
st.add(sql);
return RedirectToAction("Index");
}
}
}
```
修改后的代码解决了以下问题:
1. `GetAll` 方法的参数默认值应该使用 `= null` 的形式,而不是 `= 0` 的形式。
2. `GetAll` 方法中的 SQL 查询语句需要使用 `flights` 表名,而不是 `stuInfo`。
3. `GetAll` 方法的 SQL 查询语句需要在每个条件的前面加上空格。
4. `Add` 方法中的 SQL 语句应该使用字段名指定插入的列,而不是省略字段名。
5. `Add` 方法中的价格(`price`)应该是一个数字类型,而不是字符串类型。