如何使用@ Html.ActionLink将电子邮件ID作为参数传递给action方法?(How can I pass email id to action method as parameter u

编程入门 行业动态 更新时间:2024-10-10 15:18:06
如何使用@ Html.ActionLink将电子邮件ID作为参数传递给action方法?(How can I pass email id to action method as parameter using @Html.ActionLink?)

Index.chtml页面本身我有这个链接

@Html.ActionLink("Edit","EditUserProfile",new {vchUserID = item.vchUserID})

内部用户控制器本身

// GET: /User/ViewUserProfile/1 public ActionResult EditUserProfile(string userID = "abdul@kareems.com") { LIVE_USER objUserFind = db.LIVE_USER.Find(userID); if (objUserFind == null) { return HttpNotFound(); } return View(objUserFind); } // //POST: /Admin/EditAdminProfile/1 [HttpPost] public ActionResult EditUserProfile(LIVE_USER objUserFind) { if (ModelState.IsValid) { db.Entry(objUserFind).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(objUserFind); }

RouteConfig.cs文件本身我有像bellow的url结构

public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{vchUserID}", defaults: new { controller = "Admin", action = "Index", vchUserID = UrlParameter.Optional /*kareem removed vchUserID = UrlParameter.Optional*/ } );

我得到的结果是: * HTTP错误404.0 - 未找到您要查找的资源已被删除,名称已更改或暂时不可用。 *

Index.chtml page itself i have this link

@Html.ActionLink("Edit","EditUserProfile",new {vchUserID = item.vchUserID})

Inside user Controller itself

// GET: /User/ViewUserProfile/1 public ActionResult EditUserProfile(string userID = "abdul@kareems.com") { LIVE_USER objUserFind = db.LIVE_USER.Find(userID); if (objUserFind == null) { return HttpNotFound(); } return View(objUserFind); } // //POST: /Admin/EditAdminProfile/1 [HttpPost] public ActionResult EditUserProfile(LIVE_USER objUserFind) { if (ModelState.IsValid) { db.Entry(objUserFind).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(objUserFind); }

RouteConfig.cs file itself i have url structure like bellow.

public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{vchUserID}", defaults: new { controller = "Admin", action = "Index", vchUserID = UrlParameter.Optional /*kareem removed vchUserID = UrlParameter.Optional*/ } );

Result i got is: *HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.*

最满意答案

控制器方法中参数的名称需要与路径中定义的参数名称匹配。 目前,该参数在路由中名为vchUserID ,但在您的控制器方法中,您已将其命名为userId 。

这意味着控制器方法永远不会从Url获​​取值,并且您将始终获得默认值“abdul@hibrise.com”。 (我想这不是你的一个在线用户,所以你要返回HttpNotFound结果)

尝试在控制器方法中重命名参数,如下所示:

public ActionResult EditUserProfile(string vchUserID = "abdul@hibrise.com") { ... }

The name of the parameter in your controller method needs to match the name of the parameter defined in your route. Currently the parameter is named vchUserID in the route but in your controller method you have named it userId.

That means the controller method will never get the value from the Url, and you will always get the default value "abdul@hibrise.com". (I guess that is not one of your live users so you are returning the HttpNotFound result)

Try renaming the parameter in the controller method as in:

public ActionResult EditUserProfile(string vchUserID = "abdul@hibrise.com") { ... }

更多推荐

本文发布于:2023-04-29 01:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   电子邮件   参数   方法   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!