I'm a newbie here. I'm trying to create a dropdown list (SelectList) which would display the location of shops in my ASP.NET MVC application.
The Shop class has a Location property which in turn has a Name property:
public class Shop
{
public int Id { get; set; }
public Location Location { get; set; }
}
public class Location
{
public int Id { get; set; }
public string Name { get; set; }
}
The Name property is a string.
I tried to use this code:
var shops = _applicationDbContext.Shops
.Include(x => x.Location).ToList();
var mwc = new Create
{
ShopSL = new SelectList(shops, nameof(Shop.Id), nameof(Shop.Location.Name))
};
But I get an error:
Object reference not set to an instance of an object
If I remove the Shop.Location.Name and change it to Shop.Location only, the dropdown list displays the object instead, something like Namespace.Data.Location.
I would like the dropdown list to be able to display the name of the location.
Could someone point me to the right direction please? Thank you.
try this
var shops = _applicationDbContext.Shops
.Select (i=> new
{
Id=i.Id
Name=i.Location.Name
}).ToList();
var mwc = new Create
{
ShopSL = new SelectList(shops,"Id","Name") ;
};
Related
I try to use MongoDB in combination with .net core (c#) to save some survey results.
The challenge is that I plan to make it as generic as possible to be able to add other rating controls later.
I am able to save different result types in one table.
public class VoteBase
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public User User { get; set; }
public Control Control { get; set; }
public string ControlType { get; set; }
}
public class VoteStarRatingControl : VoteBase
{
public int? Rating { get; set; }
}
public class VoteStarRatingWithComment : VoteStarRatingControl
{
public string Comment { get; set; }
}
I created a table of base type as MongoCollection:
private readonly IMongoCollection<VoteBase> _voteBases;
To save it to that collection I used this code (where the DTO is same content datatransfer object to decouple the REST service from DB structure):
List<VoteBase> dbVotes = new List<VoteBase>();
foreach (VoteBaseDTO v in votes)
{
switch (v) {
case VoteStarRatingWithCommentDTO src:
dbVotes.Add(new VoteStarRatingWithComment() { User = new User() { Id = UserId }, Control = new Control() { Id = src.ControlId }, Rating = src.Rating, Comment = src.Comment });
break;
case VoteStarRatingControlDTO sr:
dbVotes.Add(new VoteStarRatingControl() { User = new User() { Id = UserId }, Control = new Control() { Id = sr.ControlId }, Rating = sr.Rating });
break;
}
}
_voteBases.InsertMany(dbVotes);
return dbVotes;
Until here all works fine.
Now I try to get the votes back for a specific list of controls (for one survey).
The following command fails with
'Element 'Rating' does not match any field or property of class SurveyToolRestAPI.Models.VoteBase.'
object obj = _voteBases.Find(vb => vb.Control.Id == "5e9c24c50a099728b027e176").SingleOrDefault();
This is because it is of Type StarRatingControl instead of type VoteBase.
Is there a way to get the list as dynamic instead of strong typed from a MongoDB collection?
Thanks Eldho, you pointed me to the right direction.
Doings some more research with the Bson... attributes I found the solution finally in this post Mongodb collection as dynamic
The key is to add a BsonKnownTypes attribute. The it can be used as strong typed collection and the list will contain the appropriate subtypes.
[BsonKnownTypes(typeof(VoteBase), typeof(VoteStarRatingControl), typeof(VoteStarRatingWithComment))]
I have this class in C#:
public class Init
{
public string First_name { get; set; }.
public List<LeadLocations> Locations { get; set; }
}
public Init()
{
this.Locations = new List<LeadLocations>();
}
public class LeadLocations
{
public string City { get; set; }
public string State { get; set; }
public string County { get; set; }
}
I need to fill Locations with values that I have in another list like:
foreach (var item2 in AreaOfInterest.AreaOfInterests)
{
foreach (var item in transactionInit.Locations)
{
item.City = item2.City;
item.County = item2.County;
}
}
but it never goes to second foreach as it is empty. How I can initialize it to new object, anything I try is not working.
If you are trying to add location to a list of locations, you need to create a new location object (based on the AreaOfInterests objects) and then add that to the list of locations.
// Initialize your Locations if its not already initialized.
// If its not initialized, you will get Object Reference Not Set to Instance of an Object.
transactionInit.Locations = new List<LeadLocations>();
foreach (var item2 in AreaOfInterest.AreaOfInterests)
{
// For Each item2, create a new location then insert it in transactionInit.Locations.
var leadLocation = new LeadLocation() {City = item2.City, County = item2.County};
transactionInit.Locations.Add(leadLocation);
}
I believe I understood what you are intending to do. You could do the following.
transactionInit.Locations.AddRange(AreaOfInterest.AreaOfInterests
.Select(x=> new Location
{
City = x.City,
County=x.County
});
In the above code, you are iterating over the AreaOfInterest.AreaOfInterests to create instances of Location and using the List.AddRange(IEnumerable<T>) to add them to the Locations property of transactionInit.
Sorry if this is a simple question but if I want to have a list inside a model, and later access and set the values of the list?
Say my main model looks like this:
public class StartPageModel : IPageViewModel<StartPage>
{
public IList<ListContent> ListContent { get; set; }
public StartPage CurrentPage { get; set; }
}
public class ListContent
{
public IList<ListElement> ArticleListContent { get; set; }
public IList<ListElement> InsightListContent { get; set; }
}
How can I set the ArticleListContent list to a value by referencing the parent model?
public ActionResult Index(StartPage currentPage)
{
var model = new StartPageModel(currentPage);
model.ListContent.ArticleListContent = GetListContent(currentPage.ArticleCollection);
}
However this returns the error:
IList does not contain a definition for 'ArticleListContent'
I'm not sure you require a collection of ListContent in your StartPageModel, correct me if I'm wrong.
Change
public IList<ListContent> ListContent { get; set; }
to
public ListContent ListContent { get; set; }
And provided ListContent is initialized, your assignment will work.
It's because it's referencing the List of ListContent, not an individual item in that list. Here's some examples:
var model = new StartPageModel(currentPage);
model.ListContent[0].ArticleListContent = GetListContent(currentPage.ArticleCollection); // Access first in list
model.ListContent[1].ArticleListContent = GetListContent(currentPage.ArticleCollection); // Access secondin list
model.ListContent.First().ArticleListContent = GetListContent(currentPage.ArticleCollection); // Access first in list using Linq
I am doing a project and can't make a Seletect Value from a list get the right value.A list is generated from a class UserDepartment,in this class I have this basically:
public class UserDepartment
{
public long ID { get; set; }
public string Description { get; set; }
public User UserResponsible { get; set; }
}
The Problem is that I need a value from the class userResponsible,inside there is a value called EDV, I need this value but I don't know how to get.See this image below:
If I use " ListBeneficiaryArea.DataValueField = "ID"; ",I get the ID value normally,but i cant get the EDV value, I already tried "EDV","UserResponsible.EDV" and "UserResponsible"but it didn't work.
There is other way for me to get the UserResponsible.EDV in the DataValueField?
After the change in DataSource i received this error:
You can change your DataSource to the following:
ListBeneficiaryArea.DataSource = from a in lstBUAreas
select new { ID, EDV = a.UserResponsible.EDV };
Then you can do:
ListBeneficiaryArea.DataTextField = "ID";
ListBeneficiaryArea.DataValueField = "EDV";
I have obtained the list of data from database in the following way
List<MakerCheckerModel> mkckdata = new List<MakerCheckerModel>();
var dataContext = new PetaPoco.Database("MessageEntity");
mkckdata = dataContext.Query<MakerCheckerModel>(PetaPoco.Sql.Builder.Append("Select * from MakerChecker1")).ToList();
The data comes in mkckdata. My model is of the following way.
public class MakerCheckerModel
{
public int MakerCheckerId { get; set; }
public string OldJson { get; set; }
public string NewJson { get; set; }
public string ModelName { get; set; }
}
Now I want to put the value obtained in OldJson and NewJson of mkckdata in new List type of model variables so that I can manipulate it further.I want something like this.
List<MakerCheckerModel> oldDataList = new List<MakerCheckerModel>();
oldDataList.Add(mkckdata.OldJson));
But this is not allowed here. PLease help me how to do this.