1

I have a list displays jobs in my view,when I click that link it should take me to different page to display complete details and in the browser url should display like below

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

To achieve this I am trying to submit form on click and pass hidden value to controller.But it Always throws 404 error. always it passes id null to jobs 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>
    }
 }

Shared View :

@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>
  }
}

Controller -

 [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);
   }

Rout config

        routes.MapRoute(
            "Jobs",
            "{controller}/{action}/{name}",
        new { contrller = "jobController", action = "Jobs", name = UrlParameter.Optional }
        );
jina
  • 115
  • 11
  • [This](https://stackoverflow.com/questions/47790317/is-is-possible-to-make-seo-friendly-urls-in-asp-net-core-like-this-one) might help you – Ammar Jun 19 '19 at 06:58

3 Answers3

0

In your controller, the id needs to be string to work

   [HttpGet]
   public ActionResult Jobs(string name)
   {
      var allDetails = _db.MyDB.Where(p => p.name == name).FirstOrDefault();
      return View(allDetails);
   }
Eduardo Molteni
  • 38,786
  • 23
  • 141
  • 206
  • That did not helped. its passes null to name – jina Jun 18 '19 at 16:23
  • There are other several errors in your code (sometimes you use the int id and sometimes a string). If your are gonna use the int, change all accordingly (in MapRoute). You are also posting a form and making a link at the same time, just choose one (I recommend using the link) – Eduardo Molteni Jun 18 '19 at 16:35
  • It's always easier to use an int id and then add the slug after (just like StackOverflow does: stackoverflow.com/questions/56652893/passing-name-in-url-instead-of-id-throws-error) – Eduardo Molteni Jun 18 '19 at 16:36
0

In your shared view change:

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

to:

<form id="Jobs-@item.id" action="~/Index/@item.id" method="post">
9Dan7
  • 43
  • 6
0

When this code runs

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

the value held by id is lost when you get to here

public ActionResult Jobs(int? id)

because in your routing table, you didn't specify id, only name.

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

May i suggest this tested quick fix:

In the controller:

[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);
}

and in your routing table:

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

There is still so much to improve from your code, but I hope this clarifies your question. Happy coding!