Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,12 @@ public IActionResult Index()
{
var model = new ProductViewModel
{
Products = new List<SelectListItem>
{
new SelectListItem
{
Text = "Motherboards",
Value = "MB"
},
new SelectListItem
{
Text = "Graphic Cards",
Value = "GC"
},
new SelectListItem
{
Text = "Liquid Coolants",
Value = "LC"
}
},
Products =
[
new() { Text = "Motherboards", Value = "MB" },
new() { Text = "Graphic Cards", Value = "GC" },
new() { Text = "Liquid Coolants", Value = "LC" }
],
Product = "GC"
};

Expand Down Expand Up @@ -74,6 +62,17 @@ public IActionResult SelectTagHelperWithEnum()
return View(model);
}

//Usage of a disabled first item to render a placeholder row
public IActionResult Placeholder()
{
var model = new SelectViewModel
{
Genders = StaticRepository.GetGendersWithPlaceholder(),
SelectedGender = string.Empty
};
return View(model);
}

//Consolidated action method - renders select element using all the approaches
public IActionResult Details()
{
Expand Down Expand Up @@ -142,4 +141,4 @@ public IActionResult Error()
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SelectTagHelper.Enums
{
public enum Department
{
IT,
HR,
Finance,
Admin
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public class EmployeeViewModel
{
public int Id { get; set; }
public string EmployeeName { get; set; }
public string EmployeeName { get; set; } = string.Empty;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ namespace SelectTagHelper.Models
{
public class ErrorViewModel
{
public string RequestId { get; set; }
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace SelectTagHelper.Models
{
public class GroupViewModel
{
public List<SelectListItem> Courses { get; set; }
public List<SelectListItem> Courses { get; set; } = [];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class MultiSelectViewModel
{
public List<EmployeeViewModel> Employees { get; set; }
public int[] SelectedEmployeeIds { get; set; }
public List<EmployeeViewModel> Employees { get; set; } = [];
public int[] SelectedEmployeeIds { get; set; } = [];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace SelectTagHelper.Models
{
public class ProductViewModel
{
public string Product { get; set; }
public List<SelectListItem> Products { get; set; }
public string? Product { get; set; }
public List<SelectListItem> Products { get; set; } = [];
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using static SelectTagHelper.Enums.Enumerations;
using SelectTagHelper.Enums;

namespace SelectTagHelper.Models
{
public class SampleViewModel
{
public Department Department { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using static SelectTagHelper.Enums.Enumerations;
using SelectTagHelper.Enums;

namespace SelectTagHelper.Models
{
public class SelectViewModel
{
public List<SelectListItem> Genders { get; set; }
public string SelectedGender { get; set; }
public List<EmployeeViewModel> Employees { get; set; }
public List<SelectListItem> Genders { get; set; } = [];
public string? SelectedGender { get; set; }
public List<EmployeeViewModel> Employees { get; set; } = [];
public int? SelectedEmployeeId { get; set; }
public SelectList Countries { get; set; }
public string SelectedCountry { get; set; }
public SelectList? Countries { get; set; }
public string? SelectedCountry { get; set; }
public Department Department { get; set; }
public int? SelectedDepartment { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
namespace SelectTagHelper
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();
app.UseRouting();

app.UseAuthorization();
app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
}
}
}
app.Run();
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,99 +5,39 @@ namespace SelectTagHelper.StaticData
{
public class StaticRepository
{
public static List<SelectListItem> GetGenders()
{
return new List<SelectListItem>
{
new SelectListItem
{
Text = "Male",
Value = "Male"
},
new SelectListItem
{
Text = "Female",
Value = "Female"
},
new SelectListItem
{
Text = "Others",
Value = "Others"
}
};
}
public static List<SelectListItem> GetGenders() =>
[
new() { Text = "Male", Value = "Male" },
new() { Text = "Female", Value = "Female" },
new() { Text = "Others", Value = "Others" }
];

public static List<EmployeeViewModel> GetEmployees()
{
return new List<EmployeeViewModel>
{
new EmployeeViewModel
{
Id = 101,
EmployeeName = "Mark"
},
new EmployeeViewModel
{
Id = 102,
EmployeeName = "Dave"
},
new EmployeeViewModel
{
Id = 103,
EmployeeName = "Rosy"
}
};
}
public static List<SelectListItem> GetGendersWithPlaceholder() =>
[
new() { Text = "Please select a gender", Value = "", Disabled = true, Selected = true },
.. GetGenders()
];

public static List<string> GetCountries()
{
return new List<string>
{
"India","USA","UK","France","Germany"
};
}
public static List<EmployeeViewModel> GetEmployees() =>
[
new() { Id = 101, EmployeeName = "Mark" },
new() { Id = 102, EmployeeName = "Dave" },
new() { Id = 103, EmployeeName = "Rosy" }
];

public static List<SelectListItem> GetCourses(SelectListGroup science, SelectListGroup humanities)
{
return new List<SelectListItem>
{
new SelectListItem
{
Text = "Physics",
Value = "PH101",
Group = science
},
new SelectListItem
{
Text = "Chemistry",
Value = "CH101",
Group = science
},
new SelectListItem
{
Text = "Mathematics",
Value = "MT101",
Group = science
},
new SelectListItem
{
Text = "English",
Value = "EN101",
Group = humanities
},
new SelectListItem
{
Text = "Environmental Studies",
Value = "EN101",
Group = humanities
},
new SelectListItem
{
Text = "Economics",
Value = "EC101",
Group = humanities
}
};
}
public static List<string> GetCountries() =>
[
"India", "USA", "UK", "France", "Germany"
];

public static List<SelectListItem> GetCourses(SelectListGroup science, SelectListGroup humanities) =>
[
new() { Text = "Physics", Value = "PH101", Group = science },
new() { Text = "Chemistry", Value = "CH101", Group = science },
new() { Text = "Mathematics", Value = "MT101", Group = science },
new() { Text = "English", Value = "EN101", Group = humanities },
new() { Text = "Environmental Studies", Value = "EN101", Group = humanities },
new() { Text = "Economics", Value = "EC101", Group = humanities }
];
}
}
}
Loading
Loading