I have this code:
Mapper.AddMap<Product, DetailsVM>(src =>
{
var res = new DetailsVM();
res.InjectFrom(src); // maps properties with same name and type
res.test = "asd";
return res;
});
productVM.InjectFrom(test);
I have everything working and this is my VM:
public int ProductId { get; set; }
public decimal Cost { get; set; }
public decimal UnitPrice { get; set; }
public int OnHandQty { get; set; }
public ProductPicture thumb { get; set; }
public ProductPicture main { get; set; }
public string test { get; set; }
the actual model doesn't have the property test, I simply want to set test to any string. how do I do it? I keep on getting null whenever i try to map.
you need to call Mapper.Map instead of InjectFrom,
InjectFrom is not affected by Mapper.AddMap
Related
I've got this class in my Model:
public class GetDocParams {
public string LogonTicket { get; set; }
public int CliRid { get; set; }
public string[] ValPairs { get; set; }
public string SortBy { get; set; }
public int StartRec { get; set; }
public int EndRec { get; set; }
}
This is going to be used as input to a WebApi2 function to retrieve query results from Entity Framework.
The function takes the valPairs from input and uses it to build a query that sorts by the passed pairs, i.e.
CLI_RID=111111
DOC_NAME=Letter
would create the SQL:
WHERE CLI_RID = 111111
AND DOC_NAME = 'Letter'
I'm kind of curious, how would I pass the ValPairs, using ajax and/or WebClient?
GET or POST doesn't matter.
You may have to add a new class for ValPair, like the following.
public class GetDocParams {
public string LogonTicket { get; set; }
public int CliRid { get; set; }
public ValPair[] ValPairs { get; set; }
public string SortBy { get; set; }
public int StartRec { get; set; }
public int EndRec { get; set; }
}
public class ValPair {
public int CLI_RID { get; set; }
public string DOC_NAME { get; set; }
}
And you can pass values to the parameters via the following GET API call:
http://www.example.com/api/docs/getDocParams?LogonTicket=111&ValPairs[0][CLI_RID]=111111&ValPairs[0][DOC_NAME]=Letter&ValPairs[1][CLI_RID]=22222&ValPairs[1][DOC_NAME]=document&....
This should work if you know the names of the keys.
I have looking on SO but I didn't find a solution and I'm totally lost the correct way to fix this.
What's happening?
I'm getting error CS0411 in a view: the type arguments for method cannot be inferred from the usage mvc
I'm using a Tuple in that view to use 2 models.
var model = new Tuple<IList<TimeTableInsertModel>, List<Ticket>>(ttc.getTimeTableDetails(id), ticket);
#model Tuple<IList<TimeTableInsertModel>, List<Ticket>>
And I want to use the field TicketQuantity.
#Html.HiddenFor(item.TicketQuantity)
But that's give the error. I can use the property in the view without any #Html functions.
The views:
public class TimeTableInsertModel
{
[Key]
public int TimeTableId { get; set; }
public int MovieId { get; set; }
public int RoomId { get; set; }
public int SeatsAvaible { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public virtual Movie Movie { get; set; }
public virtual ICollection<Reservation> Reservations { get; set; }
public virtual Room Room { get; set; }
public int TicketQuantity { get; set; }
}
public partial class Ticket
{
public Ticket()
{
ReservationTickets = new HashSet<ReservationTicket>();
}
public int TicketId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public virtual ICollection<ReservationTicket> ReservationTickets { get; set; }
}
I'm assuming for the moment you're looping through an IEnumerable like this:
#foreach (var item in Model.Item1)
{
...
Html.HiddenFor(item.TicketQuantity);
...
}
The HiddenFor method takes a delegate as an argument, so instead of this:
#Html.HiddenFor(item.TicketQuantity)
you need to do something like this:
#Html.HiddenFor( m = > item.TicketQuantity)
I've tried inputting some data into a ViewData called "Cost". This is taking a value from the "PrinterCreditsCost" column in a database, and should display the value of that column. However, the Column name is also displayed with curly brackets, so as opposed to just display "2" it displays "{ PrinterCreditCost = 2 }". Does anyone know how can I have it so that it only displays "2". This is going to be used in a mathematical equation, so I only need the 2.
The code is as follows:
var result = (from a in db.tblOptions.Include(t => t.PrinterCreditCost)
where a.ID == 1
select new
{
a.PrinterCreditCost
}
).First();
ViewData["Cost"] = result;
Update for Jason:
public int ID { get; set; }
public int Visible { get; set; }
public int DeleteLicense { get; set; }
public Nullable<int> ICTSurvey { get; set; }
public Nullable<int> PaidWorkSurvey { get; set; }
public Nullable<int> ICTGeneralSurvey { get; set; }
public Nullable<int> PESSCLSurvey { get; set; }
public Nullable<int> PastSurvey { get; set; }
public Nullable<int> PresentSurvey { get; set; }
public Nullable<int> Yr11Survey { get; set; }
public Nullable<int> NewScientistCount { get; set; }
public Nullable<int> UCASYear { get; set; }
public Nullable<int> OnlineSurveyActive { get; set; }
public Nullable<int> OnlineExaminationsActive { get; set; }
public string UCASFirstHalf { get; set; }
public string UCASSecondHalf { get; set; }
public string SIMSManual { get; set; }
public string SIMSApplication { get; set; }
public string SIMSAmpark { get; set; }
public Nullable<double> PrinterCreditFund { get; set; }
public Nullable<int> PrinterCreditCost { get; set; }
public string UCASIndividualFirstHalf { get; set; }
public string UCASIndividualSecondHalf { get; set; }
public string BookingSheetYear { get; set; }
public Nullable<System.DateTime> InductionDate { get; set; }
public string InductionYear { get; set; }
public virtual ICollection<tblPrinterCredit> tblPrinterCredits { get; set; }
In this bit of the query
select new
{
a.PrinterCreditCost
}
is there a property of PrinterCreditCost you need to use e.g.
select new
{
a.PrinterCreditCost.Value
}
I don't know if Value is the property you need, it might be called something else.
As it stands, it does indeed look like your query is returning the column definition, rather than the value of that column.
EDIT:
I see the following:
public Nullable<int> PrinterCreditCost { get; set; }
So I'm surprised that
select new
{
a.PrinterCreditCost.Value
}
Does not work. As it's a nullable value, the .Value property should return the value, not { Value = 2 }. I'm not sure what's going on there.
One possible way is to store result in a string and select substring of that stored string and assign that to View data.
string abc = result;
string viewdatastring = abc.substring(20);
ViewData[Cost]= viewdatastring
Just look here for tutorial on substrings
http://www.dotnetperls.com/substring
var result = (from a in db.tblOptions.Include(t => t.PrinterCreditCost)
where a.ID == 1
select new
{
a.PrinterCreditCost
}
).First();
ViewData["Cost"] = result;
Is incorrect, however, changing it to:
var result = (from a in db.tblOptions.Include(t => t.PrinterCreditCost)
where a.ID == 1
select new
{
a.PrinterCreditCost
}
).First();
ViewData["Cost"] = result.Value;
(adding .Value) to the end of the ViewData line will work.
I'm looking to do bulk importing of complex models containing objects and collections into Neo4j.
I have the following model:
public class PSNGame
{
public int EarnedPlatinum { get; set; }
public int EarnedGold { get; set; }
public int EarnedSilver { get; set; }
public int EarnedBronze { get; set; }
public int EarnedTotal { get; set; }
public int AvailablePlatinum { get; set; }
public int AvailableGold { get; set; }
public int AvailableSilver { get; set; }
public int AvailableBronze { get; set; }
public int AvailableTotal { get; set; }
public double PercentCompleteBronze { get; set; }
public double PercentCompleteSilver { get; set; }
public double PercentCompleteGold { get; set; }
public double PercentCompletePlatinum { get; set; }
public double PercentCompleteTotal { get; set; }
public DateTimeOffset LastUpdated { get; set; }
public string Platform { get; set; }
public string NPCOMMID { get; set; }
public string TitleName { get; set; }
public string TitleDetail { get; set; }
public string Image { get; set; }
public string LargeImage { get; set; }
// complex model parts
public GameInfo GameInfo { get; set; }
public GameCommon.Rating Rating { get; set; }
public IEnumerable<GameCommon.RatingDescriptor> RatingDescriptors { get; set; }
public IEnumerable<GameCommon.Genre> Genres { get; set; }
public IEnumerable<GameCommon.Publisher> Publishers { get; set; }
public IEnumerable<GameCommon.Developer> Developers { get; set; }
public PSNGame()
{
}
}
I use this code to insert the games to Neo4j, however, it only works without the complex objects/collections:
var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();
client.Cypher
.Match("(p:PSNProfile {PSNId : {profile}.PSNId})")
.ForEach(#"(game in {PSNGames} |
MERGE p-[:PLAYS {LastPlayed : game.LastUpdated}]->(g:PSNGame {NPCOMMID : game.NPCOMMID})-[:LOCALE]->(l:PSNGameLocalized {NPCOMMID : game.NPCOMMID})
SET g = game,
l = { NPCOMMID : game.NPCOMMID,
TitleName : game.TitleName,
TitleDetail : game.TitleDetail,
Locale : {locale}
})")
.WithParams(new
{
PSNGames = games.ToList(),
locale = locale,
profile = profile
})
.ExecuteWithoutResults();
I've tried doing nested FOREACH clauses, but this can get messy very fast. Also, the syntax of MERGE g-[:GAME_RATING]->g.Rating doesn't seem quite right and Neo4j complains that there is an invalid . token. My thought was to loop over the collections and access specific properties with the . accessor, but it doesn't look like Cypher likes the syntax.
For complex types, I would like to automatically create/update relationships/nodes for any child objects/collections contained in the complex type. Is there a way to do this in Neo4jClient?
Is there a way to do this in Neo4jClient?
No. Neo4jClient is a lower level driver, kind of like SqlClient. If you want more ORM-style behaviours on top of it, that would be a higher level library, equivalent to something like Entity Framework. There was a project called Neo4jRepository for a while, which built on top of Neo4jClient, but it has not been updated for the Neo4j 2.0 wave as far as I'm aware.
I can't understand what i'm doing wrong. Every time I'm getting this error:
The entity or complex type 'BusinessLogic.CompanyWithDivisionCount' cannot be constructed in a LINQ to Entities query.
I need to get info from 'Company' table and divisions count of each company from 'Division' table, and then make PagedList. Here is my 'Company' table:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using BusinessLogic.Services;
using BusinessLogic.Models.ValidationAttributes;
namespace BusinessLogic.Models
{
public class Company
{
public Company()
{
Country = "US";
Status = true;
}
public int Id { get; set; }
[Required]
[UniqueCompanyName]
public string Name { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public int Zip { get; set; }
public string Country { get; set; }
public string ContactInfo { get; set; }
[Required]
public DateTime EffectiveDate { get; set; }
public DateTime TerminationDate { get; set; }
public bool Status { get; set; }
[Required]
public string URL { get; set; }
public string EAP { get; set; }
public string EAPCredentials { get; set; }
public string BrandingColors { get; set; }
public string Comments { get; set; }
}
}
Here is my domain model:
public class Company
{
public Company()
{
Country = "US";
Status = true;
}
public int Id { get; set; }
[Required]
[UniqueCompanyName]
public string Name { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public int Zip { get; set; }
public string Country { get; set; }
public string ContactInfo { get; set; }
[Required]
public DateTime EffectiveDate { get; set; }
public DateTime TerminationDate { get; set; }
public bool Status { get; set; }
[Required]
public string URL { get; set; }
public string EAP { get; set; }
public string EAPCredentials { get; set; }
public string BrandingColors { get; set; }
public string Comments { get; set; }
}
public class CompanyWithDivisionCount: Company // I'm using this
{
public int DivisionCount { get; set; }
}
Here is my controller:
public ActionResult CompaniesList(int? page)
{
var pageNumber = page ?? 1;
var companies = companyService.GetCompaniesWithDivisionsCount2();
var model = companies.ToPagedList(pageNumber, PageSize);
return View(model);
}
And here is my service part:
public IQueryable<CompanyWithDivisionCount> GetCompaniesWithDivisionsCount2()
{
return (from c in dataContext.Companies.AsQueryable()
select new CompanyWithDivisionCount
{
Id = c.Id,
Name = c.Name,
Status = c.Status,
EffectiveDate = c.EffectiveDate,
URL = c.URL,
EAP = c.EAP,
EAPCredentials = c.EAPCredentials,
Comments = c.Comments,
DivisionCount = (int)dataContext.Divisions.Where(b => b.CompanyName == c.Name).Count()
});
}
}
Thanks for help!!!
Creator of PagedList here. This has nothing to do with PagedList, but rather is an Entity Framework issue (I'm no expert on Entity Framework, so can't help you there). To confirm that this is true, write a unit test along the following lines:
[Test]
public void ShouldNotThrowAnException()
{
//arrange
var companies = companyService.GetCompaniesWithDivisionsCount2();
//act
var result = companies.ToList();
//assert
//if this line is reached, we win! no exception on call to .ToList()
}
I would consider changing you data model if possible so that instead of relating Companies to Divisions by name strings, instead use a properly maintained foreign key relationship between the two objects (Divisions should contain a CompanyID foreign key). This has a number of benefits (including performance and data integrity) and will almost certainly make your life easier moving forward if you need to make further changes to you app (or if any company ever decides that it may re-brand it's name).
If you create a proper foreign key relationship then your domain model could look like
public class Company
{
...
public virtual ICollection<Division> Divisions{ get; set; }
public int DivisionCount
{
get
{
return this.Divisions.Count()
}
}
...
}