C# ASP.NET MVC存储库和控制器错误
每当我尝试转到我的OrderHistory页面时,都会收到此错误消息 参数字典包含“Mis324Assignments.Controllers.MusicController”中方法“System.Web.Mvc.ActionResult History(Int32)”的不可为null类型“System.Int32”的参数“Id”的null条目。可选参数必须是引用类型、可为null的类型或声明为可选参数。 参数名称:参数 这是我的控制器:C# ASP.NET MVC存储库和控制器错误,c#,sql,asp.net-mvc,C#,Sql,Asp.net Mvc,每当我尝试转到我的OrderHistory页面时,都会收到此错误消息 参数字典包含“Mis324Assignments.Controllers.MusicController”中方法“System.Web.Mvc.ActionResult History(Int32)”的不可为null类型“System.Int32”的参数“Id”的null条目。可选参数必须是引用类型、可为null的类型或声明为可选参数。 参数名称:参数 这是我的控制器: public ActionResult History(
public ActionResult History(int Id)
{
return View(mor.GetHistory(Id));
}
这是我的存储库:
public List<MusicHistoryModel> GetHistory(int custID)
{
using (IDbConnection db = new SqlConnection(connectString))
{
string sql = @"SELECT OI.OrderId, OI.ASIN, OI.Qty, OI.Title, OI.Artist, OI.Price, O.OrderDate
FROM tblOrders as O join tblOrderItems as OI on O.OrderId = OI.OrderId
Where O.CustId = @custID";
List<MusicHistoryModel> history = db.Query<MusicHistoryModel>(sql,new { custID }).ToList();
return history;
}
}
public List GetHistory(int-custID)
{
使用(IDbConnection db=newSQLConnection(connectString))
{
字符串sql=@“选择OI.OrderId、OI.ASIN、OI.Qty、OI.Title、OI.Artist、OI.Price、O.OrderDate
从作为O的tblOrders加入作为O.OrderId=OI.OrderId上的OI的tblOrderItems
其中O.CustId=@CustId”;
List history=db.Query(sql,new{custID}).ToList();
回归历史;
}
}
我的看法是:
@model IEnumerable<Mis324Assignments.Models.MusicHistoryModel>
@{
ViewBag.Title = "History";
Layout = "~/Views/Music/_3ColLayout.cshtml";
}
<h2>Order History</h2>
@foreach (var item in Model)
{
<div class="row" style="padding:4px;">
<div class="col-md-2">
<img src="http://images.amazon.com/images//P@(item.ASIN).01._SCTHUMBZZZ_V1115763748_.jpg"
class='productImage' style="margin:0 0 5px 10px;" />
</div>
@item.OrderID
@item.OrderDate
@item.title
@item.artist
@item.price
@item.qty
</div>
}
@model IEnumerable
@{
ViewBag.Title=“历史”;
Layout=“~/Views/Music/_3ColLayout.cshtml”;
}
订单历史
@foreach(模型中的var项目)
{
@item.OrderID
@item.OrderDate
@项目名称
@艺术家
@项目.价格
@项目数量
}
这是重定向到历史视图的my ActionLink:
<form action="~/Music/History" method="post" class="enhancement">
<input type="submit" value="Order History*" /><br />
</form>
我在我的控制器中传递一个int-id,它使用该int-id作为custID从我的存储库中获取信息。我不知道为什么错误消息会说我没有向int-id参数传递任何信息当查看视图实现时,您似乎没有在历史操作中传递历史操作所需的id 您可以通过以下方式将id作为参数传递:
@using(Html.BeginForm("History", "Music",
new { Id= your_id_should_be_here }, FormMethod.Post, new{ @class="enhancement"})
{
<input type="submit" value="Order History*" /><br />
}
@使用(Html.BeginForm(“历史”、“音乐”),
new{Id=your_Id_应该在这里},FormMethod.Post,new{@class=“enhancement”})
{
}
我想出来了!!!在我的操作链接上,我没有传递CustID参数。这就是为什么它没有获得任何id参数的原因!提交的答案已经将其作为注释而不是答案。你能在调用历史操作的地方分享你的视图实现吗?我的意思是,你的操作链接从那里重定向到历史页面eI可能只是错过了它,但在您的表单中(使用action=“~/Music/History”)的位置你在传递id吗?这是你的整个表单吗?我找到了!!!在我的操作链接上,我没有传递CustID参数。这就是为什么它没有获得任何id参数!你的id传输不正确。你应该打开网络
并检查请求消息以查看传递给控制器的内容。我无法从你的代码中看到你的内容正好经过。