my view file - Index.cshtml
---this is just a portion---
<form asp-controller="Getjob" asp-action="viewJob">
<select name="category" class="form-control">
<option value="0">Choose Category</option>
@foreach (var category in Model.ListB)
{
// loop through each product
<option value="category.Name">@Html.DisplayFor(modelItem => category.Name)</option>
}
</select>
<br><br>
<input type="submit" class="btn btn-lg btn-success" value="FIND">
</form>
my controller class
---this method i am using---
[HttpPost]
public IActionResult viewJob(int cid)
{
using (var db = new JobportalContext())
{
jobs = db.Jobs.ToList();
}
foreach (Job item in jobs)
{
if (item.Cid == cid)
{
jobsFiltered.Add(item);
}
}
return View(jobsFiltered);
}
my category class
using System;
using System.Collections.Generic;
#nullable disable
namespace JobPortal.ProjModel
{
public partial class Category
{
public int Id { get; set; }
public string Name { get; set; }
}
}
i am creating a filter method. so what i am trying to do is when i select a value from drop down list(select) and click on FIND button i want the form to pass a parameter which is -> id. but i cant figure out how to do that. It is an asp.net MVC project. this category class was created using EF core.
Help....
CodePudding user response:
Change the view ->
<form asp-controller="Getjob" asp-action="viewJob">
<select name="category" class="form-control">
<option value="0" selected disabled>Choose Category</option>
@foreach (var category in Model.ListB)
{
// loop through each product
<option value="@category.Id">@Html.DisplayFor(modelItem => category.Name)</option>
}
</select>
<br><br>
<input type="submit" class="btn btn-lg btn-success" value="FIND">
</form>
Controller code ->
[HttpPost]
public IActionResult viewJob(int cid)
{
cid = Convert.ToInt32(Request.Form["category"]);
using (var db = new JobportalContext())
{
jobs = db.Jobs.ToList();
}
foreach (Job item in jobs)
{
if (item.Cid == cid)
{
jobsFiltered.Add(item);
}
}
return View(jobsFiltered);
}
CodePudding user response:
Just change view to
change the name property in select to cid
<form asp-controller="Getjob" asp-action="viewJob">
<select name="cid" class="form-control">
<option value="0">Choose Category</option>
@foreach (var category in Model.ListB)
{
// loop through each product
<option value="category.Id">@Html.DisplayFor(modelItem => category.Name)</option>
}
</select>
<br><br>
<input type="submit" class="btn btn-lg btn-success" value="FIND">
</form>