Hi, I am first time using Asp.Net Core MVC 3.1 and I am not sure how to implement below requirement in Core MVC.
I have 3 dropdowns in my application in as a filter. User selects DDL items and click on button, data related to selected DDL items is displayed in grid.
Requirement: Upon selecting dropdown items, the selected item shall remain throughout the application (like a session in Asp.Net) until user re-selects or closes the application
Startup.cs:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(1);//You can set Time
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSession();
}
}
ViewModel.cs
namespace SLB.ETLWebApp.ViewModel
{
public class MappingWellViewModel
{
public string StudyId { get; set; }
public SelectList StudyList { get; set; }
public string ScenarioId { get; set; }
public SelectList ScenarioList { get; set; }
public string ProjectId { get; set; }
public SelectList ProjectList { get; set; }
}
}
Index.view:
Filters:
<select id="ddlStudy" class="form-control col-md-8" onchange="GetScenario(this.value);"
asp-items="@Model.StudyList">
<option value="">--Please Select--</option>
</select>
<select id="ddlScenario" class="form-control col-md-8" name="ddlScenario">
<option value="">--Please Select--</option>
</select>
<select id="ddlProject" class="form-control col-md-8"
asp-items="@Model.ProjectList">
<option value="">--Please Select--</option>
</select>
<button id="btnFilter" style="border: none;background-color: transparent;" onclick="filterDataTable();">
<img src="@Url.Content("~/images/Apply.png")" style="height:30px;width:30px;" />
</button>
Controller:
using Microsoft.AspNetCore.Http;
public IActionResult Index()
{
MappingWellViewModel mappingWellViewModel = new MappingWellViewModel();
mappingWellViewModel.StudyList = BindStudyDDL();
mappingWellViewModel.ProjectList = BindProjectDDL();
return View(mappingWellViewModel);
}
public SelectList BindStudyDDL()
{
var data = (from x in _repository.GetAllMappingDetails().ToList()
join z in _repository.GetScenarioDDL().ToList() on x.FdpStudy equals z.StudyName
orderby z.StudyName
select new
{
StudyId = z.StudyId,
StudyName = x.FdpStudy
}).Distinct().OrderBy(x => x.StudyName).ToList();
var ddlStudyList = new SelectList(data, "StudyId", "StudyName");
return ddlStudyList;
}
public JsonResult BindScenarioDDLByStudyId(string selectedStudyId)
{
var data = (from x in _repository.GetAllMappingDetails().ToList()
join w in _repository.GetScenarioDDL().ToList() on x.FdpScenario equals w.CombinedName
where w.StudyId == selectedStudyId
orderby w.ScenarioName
select new
{
ScenarioId = w.ScenarioId,
ScenarioName = w.ScenarioDisplayId + " | " + w.ScenarioName
}).Distinct().OrderBy(x => x.ScenarioName).ToList();
return Json(data);
}
public SelectList BindProjectDDL()
{
var data = (from x in _repository.GetAllMappingDetails().ToList()
join y in _repository.GetProjectDDL().ToList() on x.PearlProject equals y.ProjectName
orderby y.ProjectName
select new
{
ProjectId = y.ProjectId,
ProjectName = x.PearlProject
}).Distinct().OrderBy(x => x.ProjectName).ToList();
var ddlProjectList = new SelectList(data, "ProjectId", "ProjectName");
return ddlProjectList;
}
Requirement: Upon selecting dropdown item, the selected item shall maintain throughout the application until user re-selects or closes the application.
I am not sure how to set session inside controller code.
Example code:
// Set Session.
HttpContext.Session.SetString("StudyDDL", "StudyItem1"); //here "StudyItem1" is study dropdown selected value
HttpContext.Session.SetString("ScenarioDDL", "ScenarioItem1"); //here "ScenarioItem1" is scenario dropdown selected value
HttpContext.Session.SetString("ProjectDDL", "ProjectItem1"); //here "ProjectItem1" is project dropdown selected value
Please guide me/help me to achieve the requirement, please. I am a bit confused.
Thank you in advance