project, I get an error the problem is;
"The parameters dictionary contains a null entry for parameter 'Id' of non-nullable type 'System.Int32' for method 'Void Delete(Int32)' in 'BlogNewCMS.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parametre adı: parameters"
Controller;
[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();
}
}
}
Page;
@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>
}
}
Routing;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Your Delete method expects an Id parameter of int
type. But your form does not have one ,hence not passing one when you submit the form.
Add one input field to your form with name attribute value set to "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>
}
Also you probably want to add a return type to your delete method so that it returns a response.
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
}