CoreVisional
CoreVisional
CC#
Created by CoreVisional on 2/21/2024 in #help
Difference in using the second overloaded method and first method from CosmosClient
Hi, I am trying to understand what's the difference in using the second overloaded method in CosmosClient compared to just supply the connection string using the first overloaded method of CosmosClient? I understand that the second requires me to provide the endpoint uri and primary key, but why is there a need for the second overloaded method if CosmosClient(string connectionString, CosmosClientOptions clientOptions = null) already simplifies my work. Is there a reason(s) that I should be using the second one? Note that I am using the latest Azure CosmosDB SDK v3 for my ASP.NET Core 7 application.
36 replies
CC#
Created by CoreVisional on 12/10/2022 in #help
Remove string on specified character
Hi, how do I slice off a string on a specified character. For example, I have To.Remove.This. How do I omit both the dot character and the word after the dot symbol?
2 replies
CC#
Created by CoreVisional on 12/9/2022 in #help
Refactoring long if statement that checks for user roles
Hi, I'm trying to find a cleaner way of refactoring this if code block that checks for each user role. The if statement works but looks really messy, and I was wondering if it's possible to do something like how EF Core would do, just check if it contains something using .Contains().
if (roleName != "Admin" && (User.IsInRole("Admin.Global") || User.IsInRole("Department.A") || User.IsInRole("Department.B") || User.IsInRole("Department.C") || User.IsInRole("Department.D")))
if (roleName != "Admin" && (User.IsInRole("Admin.Global") || User.IsInRole("Department.A") || User.IsInRole("Department.B") || User.IsInRole("Department.C") || User.IsInRole("Department.D")))
22 replies
CC#
Created by CoreVisional on 11/27/2022 in #help
✅ Cannot convert from string[] to 'char' [SOLVED]
Hi, I'm getting this error that I am converting from string[] to char. The data type for the field I am trying to retrieve isn't char but string. Somehow, I receive this error message.
var allLocations = new[] { "PlaceOne", "PlaceTwo" };

var locations = _context.CityLocations.Where(x => x.Location.Contains(allLocations)).ToList(); //Cannot convert from string[] to 'char'
var allLocations = new[] { "PlaceOne", "PlaceTwo" };

var locations = _context.CityLocations.Where(x => x.Location.Contains(allLocations)).ToList(); //Cannot convert from string[] to 'char'
4 replies
CC#
Created by CoreVisional on 11/26/2022 in #help
❔ How to pass anchor tag values to controller to render view based on selected value
HI, how do I pass the values assigned to an anchor tag to the controller so that the controller can render the view or filter out what to show to the user? For example, I have in my views
<a asp-controller="Example" asp-action="Details" value="1">
//
</a>

<a asp-controller="Example" asp-action="Details" value="2">
//
</a>
<a asp-controller="Example" asp-action="Details" value="1">
//
</a>

<a asp-controller="Example" asp-action="Details" value="2">
//
</a>
Controller
[HttpGet]
public IActionResult Summary(int viewsIndicator)
{
return View();
}
[HttpGet]
public IActionResult Summary(int viewsIndicator)
{
return View();
}
I'm not sure how to do something like this in ASP
public IActionResult Summary(int viewsIndicator)
{
if (value === 1) {
// filter out what to show
} else {
// further filtering
}
}
public IActionResult Summary(int viewsIndicator)
{
if (value === 1) {
// filter out what to show
} else {
// further filtering
}
}
8 replies
CC#
Created by CoreVisional on 11/26/2022 in #help
Possible to have one controller checking which button is clicked and render view based on it?
1 replies
CC#
Created by CoreVisional on 11/26/2022 in #help
❔ Save each specified value in an input field as a whole when there are multiple tags in input field
7 replies
CC#
Created by CoreVisional on 11/24/2022 in #help
Get data from a model and display it as dropdown on client side in Razor
Hi, how do I retrieve data in a database from a model and show it in as my dropdown values in my razor view? For example, I have model called "Block", and I have a model called "ClassroomLocation", then on client side, there's a dropdown with "Block" as selection. But the values I'm not sure how to generate it based on the data stored inside "Block". Do I have to use something called model binding? These two models are also non-relational to each other.
5 replies
CC#
Created by CoreVisional on 11/24/2022 in #help
✅ Shorten data querying line
Hi, how do I shorten this line that's querying for data? Like, instead of putting them into one long line, I'm looking to separate them, is it something like
var cityOneBlock = _context.CityBlocks.Where(x => x.Location == Location.One)
var cityTwoBlock = _context.CityBlocks.Where(x => x.Location == Location.Two)

var cityBlocks = _context.CityBlocks.Where(cityOneBlock || cityTwoBlock).ToList();
var cityOneBlock = _context.CityBlocks.Where(x => x.Location == Location.One)
var cityTwoBlock = _context.CityBlocks.Where(x => x.Location == Location.Two)

var cityBlocks = _context.CityBlocks.Where(cityOneBlock || cityTwoBlock).ToList();
7 replies
CC#
Created by CoreVisional on 11/21/2022 in #help
❔ The type or namespace name Model could not be found
Hi, I'm facing an error on a missing directive when trying to specify the model name in my view model. I'm not sure if this works on Models directory but it works across controllers and views, so I assume it does. So, I have all my imports called in "_ViewImports", and this includes the model directory. However, when I wanted to use the model name, I get the error. I mean, calling the import inside the view model solves the issue, but I was looking to not include it in since I already have "_ViewImports". For example,
namespace Test.Web.Area.Admin.SomeViewModel {
public class SomeViewModel {
public List<ModelName> { get; set; }
}
}
namespace Test.Web.Area.Admin.SomeViewModel {
public class SomeViewModel {
public List<ModelName> { get; set; }
}
}
Then the error appears on "ModelName"
8 replies
CC#
Created by CoreVisional on 11/19/2022 in #help
❔ Naming enum value for level or floor for a building
Hi, so, I've read some posts saying it is not descriptive at all to name enum values like "One", "Two", "Three", etc.. However, if it is used for a building where there's level/floor, wouldn't it make sense to name it as those aforementioned values? Or should it be like "LevelOne", "LevelTwo", etc...
25 replies
CC#
Created by CoreVisional on 11/19/2022 in #help
❔ Display data saved in database in a specified format
Hi, how can i display the value in a specific format than the one saved in database? For example, I have in my database
ID Level
1 LevelOne
2 LevelTwo
ID Level
1 LevelOne
2 LevelTwo
When retrieving data from the database and showing it in a table, how do I make it display "Level 1", "Level 2", instead of "LevelOne", "LevelTwo"?
13 replies
CC#
Created by CoreVisional on 11/16/2022 in #help
Display value of EnumMember on Razor Page View
Hi, how do I display the value defined in my enum using EnumMember and have the value displayed on my razor page? For example, I want to display "Block A", "Block B", etc.. instead of BlockA, BlockB
using System.Runtime.Serialization;

namespace RoundingPortal.EntityModels.Enums
{
public enum Block
{
[EnumMember(Value = "Block A")]
BlockA,

[EnumMember(Value = "Block B")]
BlockB,

[EnumMember(Value = "Block D")]
BlockD,

[EnumMember(Value = "Block E")]
BlockE
}
}
using System.Runtime.Serialization;

namespace RoundingPortal.EntityModels.Enums
{
public enum Block
{
[EnumMember(Value = "Block A")]
BlockA,

[EnumMember(Value = "Block B")]
BlockB,

[EnumMember(Value = "Block D")]
BlockD,

[EnumMember(Value = "Block E")]
BlockE
}
}
<select asp-for="Block" asp-items="Html.GetEnumSelectList<Block>()" class="form-control"></select>
<select asp-for="Block" asp-items="Html.GetEnumSelectList<Block>()" class="form-control"></select>
1 replies
CC#
Created by CoreVisional on 11/15/2022 in #help
❔ Compare Enum Values with submitted value
Hi, how can I compare the submitted value with values in an enum without using switch-case? For example,
public enum Weekends {
Saturday,
Sunday
}

[HttpPost]
public async Task<IActionResult> Create([FromForm] Day day)
{
var weekendvalues = Enum.GetNames(typeof(Weekends));
var values = string.Join(", ", weekendvalues);

if (day.Day != values) {
// Do something
}
else if () {
// Some other things
}
}
public enum Weekends {
Saturday,
Sunday
}

[HttpPost]
public async Task<IActionResult> Create([FromForm] Day day)
{
var weekendvalues = Enum.GetNames(typeof(Weekends));
var values = string.Join(", ", weekendvalues);

if (day.Day != values) {
// Do something
}
else if () {
// Some other things
}
}
75 replies
CC#
Created by CoreVisional on 11/15/2022 in #help
Get values from an Enum class and perform checking
Hi, I am trying to get the values from an enum and perform a conditioning checking with the values retrieved. I was told that the way I am trying to check is fine but the way I am checking it is incorrect. Here's an example of what I am trying to get. It was hinted that the if statement is incorrect.
using System;

namespace MyApplication
{
class Program
{
enum BikeType
{
Road,
Mountain
}
static void Main(string[] args)
{
var bikeTypeValues = Enum.GetValues(typeof(BikeType));

if (ModelState.IsValid && bike.BikeType.Equals(bikeTypeValues) && bike.File != null) {
// code here
}

Console.WriteLine(bikeTypeValues);
}
}
}
using System;

namespace MyApplication
{
class Program
{
enum BikeType
{
Road,
Mountain
}
static void Main(string[] args)
{
var bikeTypeValues = Enum.GetValues(typeof(BikeType));

if (ModelState.IsValid && bike.BikeType.Equals(bikeTypeValues) && bike.File != null) {
// code here
}

Console.WriteLine(bikeTypeValues);
}
}
}
24 replies
CC#
Created by CoreVisional on 11/12/2022 in #help
Entity Framework Migration Creation
Hi, so I know migrations created are based on DBContext. But how can I create a migration for a model specified in DBContext without having EF create everything that is inside DBContext?
3 replies
CC#
Created by CoreVisional on 10/25/2022 in #help
How to put enum values in a list [SOLVED]
Hi, I'm quite new to C# and I'm still learning, but I'm tasked to debug a piece of code using C#, I managed to fix it but I have like a super long if statement that contains the same thing, except for the values of enum. I came from Python and usually, I'd just declare a list like my_list = [1, 2], but from what I found, I just need to use Enum.GetValues().
11 replies