0

I have a problem with my code:

  • I would to redirect to an action with arguments from an cshtml file
  • but the url is not found file Admin cshtml:

    @{ Layout = "~/Views/Shared/_General.cshtml";
    }
    <table>
        <tr>
            <td><label  title= "Name " runat="server">Name</label></td>
            <td><label  title= "Email " runat="server">Email</label></td>
            <td><label  title= "Password" runat="server">Password</label></td>
            <td><label  title= "Phone" runat="server">Phone</label></td>
        </tr>
    
        @foreach (var marker in @Model)
        {
            <tr>
                 <td><label  title= "Nom " runat="server" >@marker.ContactName</label>/td>
                 <td><label  title= "mail " runat="server">@marker.ContactEmail</label>/td>
                 <td><label  title= "mot " runat="server" >@marker.Password</label>/td>
                 <td><label  title= "phone " runat="server" >@marker.ContactPhone</label></td>
                 <td><label  id="id" style="visibility:hidden">@marker.Identification</label></td>
                 <td>@Html.ActionLink("Edit", "Edit", new { Identification = @marker.Identification }) | @Html.ActionLink("Delete", "Delete", "Administration")</td>  
            </tr>
        }
    </table>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    

my action is this:

[HttpPost]
public ActionResult Edit(string Identification)
{
    DATA2.User u = c.GetUserById(Identification);
    return View(u);
}

How can I correct this code?

4

1 回答 1

0

在查看您的代码时,首先让我印象深刻的是runat="server". ASP.NET MVC 中没有这样的东西。请将其从您使用过的每个地方移除。

我可以从您的代码中看到的一个问题是您已经使用该[HttpPost]属性修饰了控制器操作。不要忘记,当您定义 ActionLink 时,它会生成一个锚标记 ( <a>),该标记又会向服务器发送一个 GET 请求。如果你用[HttpPost]属性装饰你的控制器动作,你基本上是说这个动作只能用 POST HTTP 动词调用。如果您希望可以从 ActionLink 访问该操作,则必须删除此属性:

public ActionResult Edit(string Identification)
{
    DATA2.User u = c.GetUserById(Identification);
    return View(u);
}

接下来我想我们必须关注编辑链接:

@Html.ActionLink("Edit", "Edit", new { Identification = marker.Identification })

您是说它找不到 Edit 操作,对吗?如果是这种情况,那么您还可以指定控制器操作以及区域(如果此控制器位于区域内):

@Html.ActionLink(
    "Edit", 
    "Edit", 
    "SomeContorller", 
    new { Identification = "marker.Identification" }, 
    null
)

如果您尝试访问的控制器操作未在用于呈现视图的同一控制器内定义,则这是必要的。

于 2012-09-20T12:38:26.353 回答