Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.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
C# ASP.NET MVC重定向_C#_Asp.net_.net_Asp.net Mvc_Razor - Fatal编程技术网

C# ASP.NET MVC重定向

C# ASP.NET MVC重定向,c#,asp.net,.net,asp.net-mvc,razor,C#,Asp.net,.net,Asp.net Mvc,Razor,目前我正在开发我的第一个ASP MVC程序。 程序应该向我显示一个产品列表,在产品名称下面有一个链接,可以编辑产品。到目前为止没有问题 @model MVC3Demo.Product @{ ViewBag.Title = "Edit"; } <h2>Edit</h2> @using (Html.BeginForm("Save", "Product")) { <div> <input type="hidden" id="ID" name

目前我正在开发我的第一个ASP MVC程序。 程序应该向我显示一个产品列表,在产品名称下面有一个链接,可以编辑产品。到目前为止没有问题

@model MVC3Demo.Product

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>
@using (Html.BeginForm("Save", "Product"))
{
<div>
    <input type="hidden" id="ID" name="ID" value="@Model.ID" />
    ProduktID @Model.ID
</div>
<div>
    Produktname <input id="Name" name="Name" type="text" value=@Model.Name />
</div>
<div>
    Preis <input id="Price" name="Price" type="text" value=@Model.Price />
</div>
<div>
    <input type="submit" value="Speichern"/>
</div>

}
在列表视图中,我可以通过编辑链接查看所有产品。 问题是,如果我按下保存按钮,它会将我重定向到旧列表,而不是更新的列表。我调试了我的项目,我确信更新方法工作正常并更新了产品

我的行动清单是:

@model IEnumerable<MVC3Demo.Product>

@{
    ViewBag.Title = "List";
}

<h2>List</h2>

<ul>
@foreach (MVC3Demo.Product p in Model)
{
    <li>@p.Name @Html.ActionLink("bearbeiten", "Edit", "Product", p, null)</li>  //new{ ID = p.id}
}
</ul>
那么我的错误在哪里呢

尝试在Save controller方法中包含HttpPost属性

[HttpPost]
public ActionResult Save(Product p)
    {
        ProductRepository rep = new ProductRepository();
        rep.Update(p);
        return RedirectToAction("List");
    }

看起来您正在调用更新,但实际上并没有提交事务本身,您的存储库是否有SubmitChanges、AcceptChanges或Commit之类的功能?与DataTables一样,在调用AcceptChanges之前,您的更改实际上不会生效并保存到数据库。

我猜更新的数据不会保存到数据库中。您是否在数据库级别验证了数据?@Harald-列表是您的问题。它不会在请求之间持久化。在ASP.NET MVC应用程序中传递数据-如果ProductRepository没有保存到数据库中,它会做什么?您需要向我们显示您的存储库代码。我认为您的保存方法没有将结果保存到数据库中。因此,在这种情况下,它不会更新列表。请在您自己的帖子和您编辑其他人的内容时,对内联代码使用回勾。如果Get和Post方法的名称相同,您将只需要HttpPost。+1我认为您是对的,它应该是HttpPost。通过GET完成的请求不应具有采取除检索以外的其他操作的意义。HTTP规范的一部分!第9.1.1节
    public ActionResult List()
    {
        ProductRepository rep = new ProductRepository();
        return View(rep.GetAll());
    }
[HttpPost]
public ActionResult Save(Product p)
    {
        ProductRepository rep = new ProductRepository();
        rep.Update(p);
        return RedirectToAction("List");
    }