Xamarin Forms CarouselView does not show images - mvvm

I have two Models and a ViewModels for articles and posts from DB . One of them like this :
public class ArticleData
{
public string Id { get; set; }
public string Title { get; set; }
public string Subtitle { get; set; }
public string Body { get; set; }
public string Section { get; set; }
public string Author { get; set; }
public string Avatar { get; set; }
public string BackgroundImage { get; set; }
public string Quote { get; set; }
public string QuoteAuthor { get; set; }
public string When { get; set; }
public string Followers { get; set; }
public string Likes { get; set; }
}
and i use BackgroundImage in CarouselView like this :
<CarouselView
HeightRequest="300"
Margin="0"
VerticalOptions="Center"
ItemsSource="{ Binding Images.Articles }">
<CarouselView.ItemTemplate>
<DataTemplate>
<Grid>
<ffimageloading:CachedImage
Source="{ Binding BackgroundImage }"
VerticalOptions="Fill"
Aspect="AspectFill" />
</Grid>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
And there is no problem with CarouselView and it shows images . But with another Model that likes this
public class FollowingsPostResponse
{
public List<FPostModel> data { get; set; }
public bool isSuccess { get; set; }
public int statusCode { get; set; }
public string message { get; set; }
}
public class FPostModel
{
public int id { get; set; }
public List<string> images { get; set; }
public string userName { get; set; }
public string userProfileImage { get; set; }
public string cityName { get; set; }
public int likesCounts { get; set; }
public string postCaption { get; set; }
}
and use like this
<CarouselView
HeightRequest="300"
Margin="0"
VerticalOptions="Center"
ItemsSource="{ Binding Home.Post }">
<CarouselView.ItemTemplate>
<DataTemplate>
<Grid>
<ffimageloading:CachedImage
Source="{ Binding images }"
VerticalOptions="Fill"
Aspect="AspectFill" />
</Grid>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
CarouselView does not show anythings . What's the problem ?

Thanks to #Jason and LeoZhu-MSFT
I solved my problem like this :
<CarouselView x:Name="MainCarouselView" ItemsSource="{Binding images}" Grid.Row="1" HeightRequest="150">
<CarouselView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding .}"></Image>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
ItemsSource="{Binding images}" because CarouselView is in CollectionView with ItemsSource="{Binding Home.Post}"

Related

Unable to access the navigation property of a newly created object

I am working on an asp.net core MVC framework + entity framework. where i got those model classes:-
public partial class Submission
{
public Submission()
{
SubmissionQuestionSubmission = new HashSet<SubmissionQuestionSubmission>();
}
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? Created { get; set; }
public virtual ICollection<SubmissionQuestionSubmission> SubmissionQuestionSubmission { get; set; }
}
public partial class SubmissionQuestion
{
public SubmissionQuestion()
{
SubmissionQuestionSubmission = new HashSet<SubmissionQuestionSubmission>();
}
public int Id { get; set; }
public string Question { get; set; }
public virtual ICollection<SubmissionQuestionSubmission> SubmissionQuestionSubmission { get; set; }
}
public partial class SubmissionQuestionSubmission
{
public int SubmissionQuestionId { get; set; }
public long SubmissionId { get; set; }
public bool? Answer { get; set; }
public virtual Submission Submission { get; set; }
public virtual SubmissionQuestion SubmissionQuestion { get; set; }
}
public class SubmissionCreate
{
public Submission Submission {set; get;}
public IList<SubmissionQuestion> SubmissionQuestion { set; get; }
public IList<SubmissionQuestionSubmission> SubmissionQuestionSubmission { set; get; }
}
and i have the following create view:-
#for (var i = 0; i < Model.SubmissionQuestion.Count(); i++)
{
<div class="form-group">
<input asp-for="#Model.SubmissionQuestion[i].Question" hidden />
<input asp-for="#Model.SubmissionQuestionSubmission[i].SubmissionQuestionId" hidden />
<label class="control-label" style="font-weight:bold">#Model.SubmissionQuestion[i].Question</label><br />
<input type="radio" asp-for="#Model.SubmissionQuestionSubmission[i].Answer" value="true" /><span style="color: #4d9b84;font-size:14px;font-weight:bold"> Yes</span><br />
<input type="radio" asp-for="#Model.SubmissionQuestionSubmission[i].Answer" value="false" /><span style="color: #4d9b84;font-size:14px;font-weight:bold"> No</span>
</div>
}
and the following create post method:-
public async Task<IActionResult> Create([Bind("Submission,SubmissionQuestionSubmission")] SubmissionCreate sc )
{
if (ModelState.IsValid)
{
var newsubmission = _context.Submission.Add(sc.Submission);
sc.Submission.Created = DateTime.Now;
await _context.SaveChangesAsync();
foreach (var v in sc.SubmissionQuestionSubmission)
{
v.SubmissionId = sc.Submission.Id;
_context.SubmissionQuestionSubmission.Add(v);
}
await _context.SaveChangesAsync();
but inside my action method if i try the following sc.Submission.SubmissionQuestionSubmission.FirstOrDefault(a => a.SubmissionQuestion.Question.StartsWith("Are you")).Answer i will get null reference exception where the sc.Submission.SubmissionQuestionSubmission.SubmissionQuestion will be null, although the relation of these objects are defined inside the database.. any advice?
FirstOrDefault could be null in any case. It doesn't related to this particular case. So, before using any property, it should be checked whether it is null or not.
var object = list.FirstOrDefault(x=>x.p1='aa');
if(object != null)
{
//use object.Answer
}

ViewModel To Model Use ExpressMapper List<object> to List<Model> as Field

My Model Is :
public class Product
{
public int id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public int id { get; set; }
public string Name { get; set; }
public int ProductId { get; set; }
[ForeignKey("ProductId")]
public virtual Product Product { get; set; }
}
And View Model Is :
public class ProductViewModel
{
public int id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public List<string> Tags { get; set; }
}
im using ExpressMapper To Mapping.
could it be map productviewModel List Tags To public ICollection Tags?
You can register your mappings like that:
Mapper.RegisterCustom<Tag, string>((tag) => tag.Name);
Mapper.Register<Product, ProductViewModel>();
Mapper.Compile();
Here is working example: https://dotnetfiddle.net/2r7l4z

Entity framework navigation property is null

I have two models using Entity Framework.
public class Player
{
public int PlayerId { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public string Plays { get; set; }
public string FavouriteSurface { get; set; }
}
public class SinglesMatch
{
public int SinglesMatchId { get; set; }
public int Player1Id { get; set; }
public int Player2Id { get; set; }
public int PlayerIdWinner { get; set; }
public DateTime Date { get; set; }
public string Venue { get; set; }
public string Score { get; set; }
public List<Player> Players { get; set; }
}
I am using the below code to attempt to display the Name of the player, based on the PlayerId in the SinglesMatch model matching the PlayerID from the Player model.
#foreach (var item in #Model)
{
<ul id="Players" class="bg-success"></ul>
<br/>
<h3>Date - #Html.DisplayFor(#modelItem => item.Date)</h3>
<li>Venue - #Html.DisplayFor(#modelItem => item.Venue)</li>
<li>Player 1 - #Html.DisplayFor(#modelItem => item.Players.First(p => p.PlayerId == item.Player1Id).Name)</li>
<li>Player 2 - #Html.DisplayFor(#modelItem => item.Players.First(p => p.PlayerId == item.Player2Id).Name)</li>
<li>Score- #Html.DisplayFor(#modelItem => item.Score)</li>
}
Upon debugging, the navigation property is always showing as null when the model is retrieved from my repository.
Am I using the navigation property in the correct fashion ? is there a problem with my query ?
Edit to include DbContext:
public TennisTrackerContext() : base("name=TennisTrackerContext")
{
}
public DbSet<Player> Players { get; set; }
public DbSet<PlayerRecord> PlayerRecords { get; set; }
public DbSet<SinglesMatch> SinglesMatches { get; set; }
public DbSet<DoublesMatch> DoublesMatches { get; set; }
public DbSet<Venue> Venues { get; set; }
}
}
You need to add a bridge table. Sql will create this automatically but you won't have access to the variables unless you create it in c#.
public class Player
{
public int PlayerId { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public string Plays { get; set; }
public string FavouriteSurface { get; set; }
List<PlayerInMatch> Matches { get; set; }
public Player()
{
Matches = new List<PlayerInMatch>();
}
}
public class PlayerInMatch
{
public int Id { get; set; }
public int PlayerId { get; set; }
[ForeignKey("PlayerId")]
public Player Player { get; set; }
public int SinglesMatchId { get; set; }
[ForeignKey("SinglesMatchId")]
public SinglesMatch SinglesMatch { get; set; }
}
public class SinglesMatch
{
public int SinglesMatchId { get; set; }
public int PlayerIdWinner { get; set; }
public DateTime Date { get; set; }
public string Venue { get; set; }
public string Score { get; set; }
public List<PlayerInMatch> Players { get; set; }
public SinglesMatch()
{
Players = new List<PlayerInMatch>();
}
}
static void Main(string[] args)
{
var match = new SinglesMatch();
match.Players.Select(c => c.Player.Name);
}
You need to make your navigation property virtual to enable lazy/eager loading:
public class SinglesMatch
{
public int SinglesMatchId { get; set; }
public int Player1Id { get; set; }
public int Player2Id { get; set; }
public int PlayerIdWinner { get; set; }
public DateTime Date { get; set; }
public string Venue { get; set; }
public string Score { get; set; }
public virtual List<Player> Players { get; set; }
}
Also, did you define the relationship between SinglesMatch and Singles in fluent api?
EDIT: I see you don't have any relations mapped through annotations or fluent api whatsoever, I suggest you take a look at this:
https://msdn.microsoft.com/en-us/data/jj591617.aspx

Bind one-many relation model in the view in MVC

I have a user registration page where i uses a tabbed layout.In the first tab i will enter the personal details and in the second tab I want to enter a couple of references provied by the user.
I have created a viewmodel and binded the viewmodel to the View.The problem is i cannot access properties in the Relation class in the View
I am using two simple models
public partial class WalkInn
{
public WalkInn()
{
this.tblWalkIn_Relation = new HashSet<WalkIn_Relation>();
}
public long WALKINID { get; set; }
public long LOGINID { get; set; }
public string CANDIDATE_NAME { get; set; }
public string FATHER_SPOUSE_NAME { get; set; }
public System.DateTime DOB { get; set; }
public virtual Login tblLogin { get; set; }
public virtual ICollection<WalkIn_Relation> tblWalkIn_Relation { get; set; }
}
public partial class WalkIn_Relation
{
public long RELATIONID { get; set; }
public long WALKINID { get; set; }
public string NAME { get; set; }
public string RELATION { get; set; }
public virtual WalkInn tblWalkInn { get; set; }
}
Viewmodel is as follows
public class WalkInnVM
{
public WalkInn WalkInn { get; set; }
public ICollection<WalkIn_Relation> WalkInRelation { get; set; }
}
View is as follows
#model CRM.Models.ViewModels.WalkInnVM
#{
ViewBag.Title = "Add2";
}
<h2>Add WalkInn</h2>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Name</label>
#Html.TextBoxFor(model=>model.WalkInn.CANDIDATE_NAME)
**Cannot access the below code**
#Html.TextBoxFor(model=>model.WalkInn_Relation.Name)

Entity Framework [Key] tag not being recognised

I am getting warning errors of no key having been defined for each of my class library classes despite the fact that I have the [Key] tag and including the System.ComponentModel.DataAnnotations namespace, here is my context:
Context:
namespace Project.Data
{
public class ProjectContext : DbContext, IProjectContext
{
public ProjectContext(string connString)
: base(connString)
{
this.Configuration.LazyLoadingEnabled = true;
Database.SetInitializer<ProjectContext>(new ProjectInitializer());
this.Database.CreateIfNotExists();
this.Database.Initialize(true);
}
public IDbSet<Article> Article { get; set; }
public IDbSet<Brand> Brand { get; set; }
public IDbSet<Colour> Colour { get; set; }
public IDbSet<Customer> Customer { get; set; }
public IDbSet<CustomerCredit> CustomerCredit { get; set; }
public IDbSet<Delivery> Delivery { get; set; }
public IDbSet<DesignerTicket> DesignerTicket { get; set; }
public IDbSet<EuroRate> EuroRate { get; set; }
public IDbSet<Gift> Gift { get; set; }
public IDbSet<GZero> GZero { get; set; }
public IDbSet<InvoiceStock> InvoiceStock { get; set; }
public IDbSet<PrintOptions> PrintOptions { get; set; }
public IDbSet<Product> Product { get; set; }
public IDbSet<ProductLocation> ProductLocation { get; set; }
public IDbSet<Sale> Sale { get; set; }
public IDbSet<SaleAccount> SaleAccount { get; set; }
public IDbSet<SalesToWeb> SalesToWeb { get; set; }
public IDbSet<Shop> Shop { get; set; }
public IDbSet<Staff> Staff { get; set; }
public IDbSet<Ticket> Ticket { get; set; }
public IDbSet<Transfer> Transfer { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
Context Interface:
namespace Project.Data
{
public interface IProjectContext
{
IDbSet<Article> Article { get; set; }
IDbSet<Brand> Brand { get; set; }
IDbSet<Colour> Colour { get; set; }
IDbSet<Customer> Customer { get; set; }
IDbSet<CustomerCredit> CustomerCredit { get; set; }
IDbSet<Delivery> Delivery { get; set; }
IDbSet<DesignerTicket> DesignerTicket { get; set; }
IDbSet<EuroRate> EuroRate { get; set; }
IDbSet<Gift> Gift { get; set; }
IDbSet<GZero> GZero { get; set; }
IDbSet<InvoiceStock> InvoiceStock { get; set; }
IDbSet<PrintOptions> PrintOptions { get; set; }
IDbSet<Product> Product { get; set; }
IDbSet<ProductLocation> ProductLocation { get; set; }
IDbSet<Sale> Sale { get; set; }
IDbSet<SaleAccount> SaleAccount { get; set; }
IDbSet<SalesToWeb> SalesToWeb { get; set; }
IDbSet<Shop> Shop { get; set; }
IDbSet<Staff> Staff { get; set; }
IDbSet<Ticket> Ticket { get; set; }
IDbSet<Transfer> Transfer { get; set; }
}
}
[Key] decorated class example:
namespace Project.Data
{
public class Article
{
[Key]
public int ArticleID;
public bool IsCore;
public string Make;
public string Product;
public decimal Sale;
public string Department;
public string Scale;
public string Detail;
public DateTime InDate;
public decimal Reduce;
public bool IsOnSale;
public string VAT;
public bool IsOnWeb;
public string ProductCode;
public string Pick;
public string MemoDetail;
public string LOC;
public string ColourCode;
public bool StatusFlag;
public string Terminal;
}
}
Despite have the [Key] placed on Article I am getting the following message for the article class as shown below and this is repeated for each of the classes:
Project.Data.Article: : EntityType 'Article' has no key defined. Define the key for this EntityType.
Anyone see what I am doing wrong here? would be greatly appreciated
Define the members of your class as public properties as opposed to public variables like you have here, by including {get; set;} at the end of the declaration