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