项目,我得到一个错误问题;
“参数字典包含非可空类型'System.Int32'的参数'Id'的空条目,用于'BlogNewCMS.Controllers.HomeController'中的方法'Void Delete(Int32)'。可选参数必须是引用类型,可空类型,或声明为可选参数。 Parametreadı:参数“
控制器;
[HttpPost]
public void Delete(int Id)
{
using (var session = FluentNHibernateConnectingAdmin.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var article = session.QueryOver<Article>().Where(x => x.Id == 3).SingleOrDefault();
session.Delete(article);
transaction.Commit();
}
}
}
页;
@foreach (var active in Model)
{
using (Html.BeginForm("Delete", "home", FormMethod.Post))
{
<tr role="row" class="gradeA odd">
<td class="sorting_1">@active.UserID</td>
<td>@active.Topic</td>
<td>@active.TopicDetail</td>
<td class="text-center">
<input type="submit" class="btn btn-danger" name="name" value="Sil" />
</td>
</tr>
}
}
路由;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
答案 0 :(得分:0)
您的Delete方法需要Id参数int
类型。但是你的表格没有,因此当你提交表格时没有通过。
在表单中添加一个输入字段,其中name属性值设置为"Id"
using (Html.BeginForm("Delete", "home", FormMethod.Post))
{
<tr role="row" class="gradeA odd">
<td class="sorting_1">@active.UserID</td>
<td>@active.Topic</td>
<td>@active.TopicDetail</td>
<td class="text-center">
<input type="submit" class="btn btn-danger" name="name" value="Sil" />
<input type="hidden" name="Id" value="@active.Id" />
</td>
</tr>
}
此外,您可能希望在delete方法中添加返回类型,以便它返回响应。
public ActionResult Delete(int id)
{
// to do : Delete
return RedirectToAction("DeletedSuccessfully");
// Assuming you have an action method called DeletedSuccessfully exist
// which shows the "Success message to user
}