【问题标题】:Passing name in url instead of id throws error在 url 中传递名称而不是 id 会引发错误
【发布时间】:2019-06-18 15:52:31
【问题描述】:

我的视图中有一个显示作业的列表,当我单击该链接时,它应该将我带到不同的页面以显示完整的详细信息,并且在浏览器中的 url 应该如下所示

//mysite.com/jobs/auto-damage-adjuster-trainee-houston-and-surrounding-cities。

为了实现这一点,我尝试在单击时提交表单并将隐藏值传递给控制器​​。但它总是抛出 404 错误。总是将 id null 传递给作业 actionresult。

Index.cshtml-

@model List<JobPortal.Models.MyDB>
@{
    ViewBag.Title = "Current List";
 }

 @using (Html.BeginForm())
 {
     @Html.AntiForgeryToken()
     @foreach (var list in Model)
     {
       <div class="row">
           <div class="flexcontainer">
              @Html.Partial("_MySharedView", list)
           </div>
       </div>
    }
 }

共享视图:

@model JobPortal.Models.MyDB
@if (Model.JDetails.Count > 0)
{
   @foreach (var item in JDetails)
{
   @{ var Url = @item.title.Replace(" ", "-").Replace('/', '-').Replace('?', '-').Replace(':', '-');}
    <form id="Jobs-@item.id" action="~/Index?id=@item.id" method="post">
    <input type="hidden" name="id" id="id" value=@item.id />
    <strong><a href="~/Jobs?@Url" onclick="document.getElementById('Jobs-@item.id').submit();">@item.title</a></strong>
    </form>
  }
}

控制器 -

 [HttpPost]
 public ActionResult Index(int? id)
 {
     var newId = (int)id;
     return RedirectToAction("Jobs", new { id = newId });
  }


  [HttpGet]
  public ActionResult Jobs(int? id)
  {
      var allDetails = _db.MyDB.Where(p => p.id == id).FirstOrDefault();
      return View(allDetails);
   }

路由配置

        routes.MapRoute(
            "Jobs",
            "{controller}/{action}/{name}",
        new { contrller = "jobController", action = "Jobs", name = UrlParameter.Optional }
        );

【问题讨论】:

  • This 可能会帮助你

标签: c# asp.net-mvc razor


【解决方案1】:

在您的控制器中,id 需要为 string 才能工作

   [HttpGet]
   public ActionResult Jobs(string name)
   {
      var allDetails = _db.MyDB.Where(p => p.name == name).FirstOrDefault();
      return View(allDetails);
   }

【讨论】:

  • 这没有帮助。它通过 null 到 name
  • 您的代码中还有其他几个错误(有时您使用 int id,有时使用字符串)。如果您要使用 int,请相应地更改所有内容(在 MapRoute 中)。你也是在发表格的同时做一个链接,随便选一个(我推荐使用链接)
  • 使用 int id 然后在后面添加 slug 总是更容易(就像 StackOverflow 所做的那样:stackoverflow.com/questions/56652893/passing-name-in-url-instead-of-id-抛出错误)
【解决方案2】:

在您的共享视图中更改:

<form id="Jobs-@item.id" action="~/Index?id=@item.id" method="post">

到:

<form id="Jobs-@item.id" action="~/Index/@item.id" method="post">

【讨论】:

    【解决方案3】:

    这段代码运行时

    return RedirectToAction("Jobs", new { id = newId });
    

    到了这里,id持有的值就丢失了

    public ActionResult Jobs(int? id)
    

    因为在您的路由表中,您没有指定id,只指定name

    routes.MapRoute(
    "Jobs",
    "{controller}/{action}/{name}",
    new { contrller = "jobController", action = "Jobs", name = UrlParameter.Optional });
    

    我可以建议这个经过测试的快速修复:

    在控制器中:

    [HttpPost]
    public IActionResult Index(int id)
    {
        var name = _db.MyDB.Where(p => p.id == id).FirstOrDefault();
        return RedirectToAction(nameof(Jobs), new { id, name });
    }
    
    [HttpGet]
    public IActionResult Jobs(string name, int id)
    {
        var allDetails = _db.MyDB.Where(p => p.id == id).FirstOrDefault();
        return View(allDetails);
    }
    

    在你的路由表中:

    routes.MapRoute(
        name: "Jobs",
        template: "{controller}/{name}/{id}",
        defaults: new { controller = "jobController", action="Jobs" });
    

    您的代码仍有很多需要改进的地方,但我希望这能澄清您的问题。编码愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 2014-04-09
      • 2011-02-09
      • 2019-07-03
      • 2015-05-25
      • 2017-08-20
      相关资源
      最近更新 更多