Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
行动方法';在asp.net MVC中不能使用[HttpPost]属性_Asp.net_Asp.net Mvc_Model_Compiler Errors_Asp.net Mvc 5 - Fatal编程技术网

行动方法';在asp.net MVC中不能使用[HttpPost]属性

行动方法';在asp.net MVC中不能使用[HttpPost]属性,asp.net,asp.net-mvc,model,compiler-errors,asp.net-mvc-5,Asp.net,Asp.net Mvc,Model,Compiler Errors,Asp.net Mvc 5,为了使用Razor引擎删除asp.netmvc5中的数据,我编写了这段代码,效果很好。但是,我想赋予它[HttpPost]属性,但是如果我添加了该属性,则该操作将不起作用 有人能帮我吗?有什么问题吗 我只有一个名为delete的操作,我不需要另一个具有[HttpGet]属性的delete操作。 我怎样才能修好它 Repositories.cs public bool Delete(int id, bool autoSave = true) { try {

为了使用
Razor引擎删除
asp.netmvc5
中的数据,我编写了这段代码,效果很好。但是,我想赋予它
[HttpPost]
属性,但是如果我添加了该属性,则该操作将不起作用

有人能帮我吗?有什么问题吗

我只有一个名为
delete
的操作,我不需要另一个具有
[HttpGet]
属性的
delete
操作。 我怎样才能修好它

Repositories.cs

public bool Delete(int id, bool autoSave = true)
    {
        try
        {
            var entity = db.Carousels.Find(id);
            db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
            if (autoSave)
                return Convert.ToBoolean(db.SaveChanges());
            else
                return false;
        }
        catch
        {
            return false;
        }
    }
管理员控制器

public ActionResult DeleteCarousel(int id)
    {
        CarouselRepositories blCarousel = new CarouselRepositories();
        blCarousel.Delete(id);
        return View("ShowCarousel", blCarousel.Select());
    }
ShowCarousel.cshtml

@model IEnumerable<NP1.Models.Carousel>

@{
ViewBag.Title = "ShowCarousel";
Layout = "~/Views/Admin/AdminLayout.cshtml";
}


 <table class="table">
 <tr>
    <th>
        @Html.DisplayNameFor(model => model.CarouselSubject)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.CarouselInfo)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.CarouselImage)
    </th>
    <th></th>
 </tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.CarouselSubject)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.CarouselInfo)
    </td>
    <td>
        @*@Html.DisplayFor(modelItem => item.CarouselImage)*@
        <img style="width:300px; height:200px;" src="~/Images/Carousel/@item.CarouselImage" />
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.CarouselID }) |
        @Html.ActionLink("Delete", "DeleteCarousel", new { id = item.CarouselID })
    </td>
</tr>
 }

</table>
@model IEnumerable
@{
ViewBag.Title=“ShowCarousel”;
Layout=“~/Views/Admin/AdminLayout.cshtml”;
}
@DisplayNameFor(model=>model.CarouselSubject)
@DisplayNameFor(model=>model.CarouselInfo)
@DisplayNameFor(model=>model.CarouselImage)
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.CarouselSubject)
@DisplayFor(modelItem=>item.CarouselInfo)
@*@DisplayFor(modelItem=>item.CarouselImage)*@
@ActionLink(“编辑”,“编辑”,新的{id=item.CarouselID})|
@ActionLink(“Delete”,“DeleteCarousel”,new{id=item.CarouselID})
}

使用
[HttpPost]
属性标记您的方法

[HttpPost]
public ActionResult DeleteCarousel(int id)
{
  ....
}
并将视图更改为使用表单而不是
ActionLink()


默认情况下,您的
Delete()
方法是
[HttpGet]
。如果你想在提交表单时点击它,你需要用
[HttpPost]
标记它,但它不起作用,我应该改变我的删除方法吗@StephenMueckeNo,一个
Delete()
方法应该是一个POST(它的变化数据)。如果它没有被击中,你的观点是不正确的。你需要展示它。我更新了我的帖子,我没有任何删除数据的视图,这是真的@Stephenmueckey您需要用表单和提交按钮替换
@Html.ActionLink(“Delete”,…)
,但从您上次的评论中注意到,您还需要确认消息-我稍后会用jquery代码更新答案以处理此问题。是的,您是对的,我也需要确认消息。再次感谢你
@foreach (var item in Model)
{
  <tr>
    <td>@Html.DisplayFor(m => item.CarouselSubject)</td>
    ....
    <td>
        @using (Html.BeginForm("DeleteCarousel", "yourControllerName", new { id = item.CarouselID }, FormMethod.Post))
        {
          <input type="submit" value="Delete" /> // style it look like a link if you want
        }
    </td>
  </tr>
}
$('form').submit(function() {
  return confirm('Are you sure to delete it?');
})