简体   繁体   English

重定向错误asp.net

[英]redirection error asp.net

I have a problem with my code: 我的代码有问题:

  • I would to redirect to an action with arguments from an cshtml file 我将使用cshtml文件中的参数重定向到操作
  • but the url is not found file Admin cshtml: 但找不到网址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? 如何更正此代码?

The first thing that strikes me when looking your code is runat="server" . 查找您的代码时,令我runat="server"的第一件事是runat="server" There's no such thing in ASP.NET MVC. ASP.NET MVC中没有这样的东西。 Please remove it from every place you have used it. 请从使用过的所有地方将其删除。

One problem I can see with your code is the fact the you have decorated your controller action with the [HttpPost] attribute. 我的代码可以看到的一个问题是,您已经用[HttpPost]属性装饰了控制器动作。 Don't forget that when you define an ActionLink this generates an anchor tag ( <a> ) which in turn sends a GET request to the server. 别忘了,当您定义ActionLink时,它会生成一个定位标记( <a> ),该标记又将GET请求发送到服务器。 If you decorate your controller action with the [HttpPost] attribute you ar ebasically saying that this action can only be invoked with the POST HTTP verb. 如果用[HttpPost]属性装饰控制器动作,则基本上是说只能用POST HTTP动词调用此动作。 If you want the action to be accessible from an ActionLink you will have to remove this attribute: 如果希望从ActionLink访问该动作,则必须删除此属性:

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

Next I guess we have to focus on the Edit link: 接下来,我想我们必须关注“编辑”链接:

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

You are saying that it cannot find the Edit action, am I right? 您是说它找不到“编辑”操作,对吗? If this is the case then you could also specify the controller action and also the area (if this controller is located inside an area): 如果是这种情况,那么您还可以指定控制器动作以及区域(如果此控制器位于区域内):

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

This is necessary if the controller action you are trying to reach is not defined inside the same controller that was used to render the view. 如果您要达到的控制器操作未在用于渲染视图的同一控制器中定义,则这是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM