How to load a property value of an object from database [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to pass a list of string to dropdownlist in my view. when I am passing this list as hard coded I am getting values in dropdownlist but I want to pass this list of strings from database. Following is my model class.
public class League
{
public int Id { get; set; }
public string LeagueName { get; set; }
public string Icon { get; set; }
}
and action is as
public ActionResult Index()
{
var leagues= new List<string> { "STAR", "GOLD", "SILVER" };
var leagueOptions = new SelectList(leagues);
ViewBag.leagues = leagueOptions;
return View();
}
till now everything is fine but I want to build a List<LeagueName> leagues = new LeagueName(); from my model class but do not know hot to query this.

Related

IEnumerable Type List Conversion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 29 days ago.
Improve this question
How to convert user-defined class (view model) to IEnumerable list type?
I want to display all the data from the view model class.
namespace DBCOntextClass.Models.Classes
{
public class UserRoleList
{
public int UserId { get; set; }
public string? UserName { get; set; }
public string? UserPassword { get; set; }
public string? RoleName { get; set; }
}
}
public IActionResult Index(UserRoleList _userRoleList)
{
var res = _context.TbUsers.FromSqlInterpolated($"exec [dbo].[sp_DisplayRoleNameInUserList] ");
IEnumerable<UserRoleList> _userRoleList = res;
return View(res);
}
#model IEnumerable<DBCOntextClass.Models.Classes.UserRoleList>
#{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
Try the following, may have different names due to you data structure of res but you should be able to see with IntelliSense:
var res = _context.TbUsers.FromSqlInterpolated($"exec [dbo].[sp_DisplayRoleNameInUserList] ").ToList();
var results = res.Select(a => new UserRoleList
{
UserId = a.UserId,
UserName = a.UserName,
UserPassword = a.UserPassword,
RoleName = a.RoleName
}).ToList();
return View(results);

Parse nested values in C# dictionary [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
My C# application has the below json which is deserialized to a dictionary which is assigned to values:
{
"armSpan": 1.8081974983215332,
"handPosition": {
"x": 1.23,
"y": 1.74,
"z": 2.05,
}
}
This is the code which deserializes:
var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
I want to assign data from it to various fields in my Size model. For armSpan I'm happy that the following works:
size.ArmSpan = decimal.Parse(values["armSpan"]);
I'm not sure how to get the values of x, y and z though. should it be something like
size.HandPosX = decimal.Parse(values["handPosition"]["x"]);
or
size.HandPosX = decimal.Parse(values["handPosition"].["x"]);
There are online converters to generate c# code based on your json (search for "JSON to C#"). With one of those, I made these classes based on the json you supplied (removed the extra comma in '"z": 2.05,'):
public partial class ClassYouDeserializeTo
{
[JsonProperty("armSpan")]
public double ArmSpan { get; set; }
[JsonProperty("handPosition")]
public HandPosition HandPosition { get; set; }
}
public partial class HandPosition
{
[JsonProperty("x")]
public double X { get; set; }
[JsonProperty("y")]
public double Y { get; set; }
[JsonProperty("z")]
public double Z { get; set; }
}
You can use them like this:
var values = JsonConvert.DeserializeObject<ClassYouDeserializeTo>(response);

How to define a C# class model for these dataobjects [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can I define classes with properties where the following code would compile and be valid?
AtomEntry newPost = new AtomEntry();
newPost.Title.Text = "Marriage!";
newPost.Content = new AtomContent();
newPost.Content.Content = "<div xmlns='http://www.w3.org/1999/xhtml'>" +
"<p>Mr. Darcy has <em>proposed marriage</em> to me!</p>" +
"<p>He is the last man on earth I would ever desire to marry.</p>" +
"<p>Whatever shall I do?</p>" +
"</div>";
newPost.Content.Type = "xhtml";
This should do it:
public class AtomEntry
{
public AtomContent Content { get; set; }
public Title Title { get; set; }
}
public class AtomContent
{
public string Content { get; set; }
public string Type { get; set; }
}
public class Title
{
public string Text { get; set; }
}

asp.net MVC c#, model List<t> binding and saving data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a view that handles a ViewModel as below,
public class MyVM {
public Exercise exer{ get; set; }
public List<Category> categories { get; set; }
public List<ExerciseInput> inputs { get; set; }
}
The view has multiple exercises based on the number of categories in the database. With a for loop i create the initial exercises for each category, the user can also add a question to each category too (as linked in has in it profile page, you can add experience to the profile), my issue is saving all this data. or updating existing data. I used Editor templates, but still can figure out how to handle multiple data for each category of the same type (ExceriseInput).
Thanks in advance.
Controller code,
public ActionResult viewExcerciseDetails(int Id)
{
var vm = new MyVM();
vm.exer= db.Exercise .SingleOrDefault(m => m.exerId == Id);
vm.categories = db.Categories.OrderBy(m => m.orderNum).ToList();
vm.inputs = db.ExerciseInput.Where(m => m.exerId == Id).ToList();
return View(vm);
}
A piece of code where the for loop in the view
#for (var i = 0; i < data.Count(); i++)
{
#Html.EditorFor(m => data[i])
}
Try like this
1. Change your ViewModel to the below code
public class MyVM {
public Exercise exer{ get; set; }
public ICollection<Category> categories { get; set; }
public ICollection<ExerciseInput> inputs { get; set; }
}
Then in the controller add the data, which is like your code.
Then in the view, populate the categories and inputs by Editor Template.
The Editor Template files should be in the name of respected model name like Category and ExerciseInput.
Hope this helps

Convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.IList<>' [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
In my model Account I have a property like this
public List<String> Roles { get; set; }
Later on I want to get that property but convert it IList<IApplicationUserRole<Role>>, so I have this function
public IList<IApplicationUserRole<Role>> Roles
{
get
{
return _account.Roles; // how do I convert this in the specific type intended.
}
}
Here is my IApplicationUserRole
public interface IApplicationUserRole<TRoleModel> : IRole<string>
where TRoleModel : EntityModel
{
TRoleModel Role { get; set; }
String Name { get; set; }
}
I am a newbie to this thing. Looking forward for any help.
Say you have your implementing class be something like:
public class ApplicationUserRole : IApplicationUserRole<T> where T : Role
{
public ApplicationUserRole()
{
}
public User User { get; set; }
public T Role { get; set; }
}
Then, you'd do something like this:
public IList<IApplicationUserRole<Role>> Roles
{
get
{
return _account.Roles
.Select(r => new ApplicationUserRole { Role = roleService.FindRoleByName(r) })
.Cast<IApplicationUserRole<Role>>()
.ToList();
}
}
Where roleService is some way of building a Role instance from the role name (which above is r)
NOTE: This being said, there is a catch in the above implementation. Since Roles is a property it should not do data access operations. So, in this case, you should create a method instead of a property.
I would start with something like this:
public IList<IApplicationUserRole<Role>> Roles
{
get
{
return _account.Roles.Select(r=>
new ApplicationUserRole<Role>() {Name = r})
.Cast<IApplicationUserRole<Role>>()
.ToList();
}
}
This assuming that you have a class that implements the IApplicationUserRole<Role> interface.
As #MartinLiversage says you can't directly convert List<T> to List<U>, you have to manually do the conversion.

Categories