I'm trying to set a property Klimatogram.Locatie in a controller class like this:
public ActionResult SelectKlimatorgramVanLocatie(LocatieKlimatogramViewModel model)
{
Locatie selectedLocatie = (Locatie)Session["GevondenLocatie"];
Klimatogram klimatogram = new Klimatogram(selectedLocatie);
TempData["kilmatogram"] = klimatogram;
return RedirectToAction("Index", "Vragen");
}
So if I debug I can see that klimatogram.Locatie is being set by selectedLocatie.
EDIT :
Now klimatogram.Locatie gets filled up. But when i look at the values from Klimatogram they are all 0.
Here is an example in the class Klimatogram:
public class Klimatogram
{
public Klimatogram(Locatie selectedLocatie)
{
Locatie =selectedLocatie;
}
public Locatie Locatie { get; set; }
public int Id { get; set; }
private double warmsteMaand;
private double aantalDrogeMaanden;
private double gemJaarTemp;
private double gemJaarNeerslag;
private double hoeveelNeerslagWinter;
private double hoeveelNeerslagZomer;
private double koudsteMaand;
public int klimaId { get; set; }
public double TempWarmsteMaand
{
get { return warmsteMaand; }
set
{
value = Locatie.TemperatuurPerMaand[0];
for (int i = 1; i < 12; i++)
{
if (Locatie.TemperatuurPerMaand[i] > value)
{
value = Locatie.TemperatuurPerMaand[i];
}
}
warmsteMaand = value;
}
}
So this method doesn't get any value from my Locatie. When i debug it says it is 0 which is wrong.
this is my vragenController:
public ActionResult Index()
{
var klimatogram = (Klimatogram)TempData["kilmatogram"];
VragenViewModel vragenViewModel = new VragenViewModel(klimatogram);
return View("VragenControl",vragenViewModel);
}
This is the viewmodel:
public class VragenViewModel
{
// public SelectList Maanden { get; set; }
public VragenViewModel()
{
}
public VragenViewModel(Klimatogram klima)
{
warm = klima.TempWarmsteMaand;
koud = klima.TempKoudsteMaand;
droge = klima.AantalDrogeMaanden;
winterneerslag = klima.HoeveelheidNeerslagWinter;
zomerneerslag = klima.HoeveelheidNeerslagZomer;
}
public double zomerneerslag
{ get; set; }
public double winterneerslag { get; set; }
public double droge { get; set; }
public double koud { get; set; }
public double warm { get; set; }
}
maybe just
public ActionResult SelectKlimatorgramVanLocatie(LocatieKlimatogramViewModel model)
{
Locatie selectedLocatie = (Locatie)Session["GevondenLocatie"];
Klimatogram klimatogram = new Klimatogram();
klimatogram.Locatie = selectedLocatie;
return Index(klimatogram);
}
The third argument of RedirectToAction is routeValues, not passing a model like you do with View.
You can use the TempData variable to pass data through as per this answer:
public ActionResult SelectKlimatorgramVanLocatie(LocatieKlimatogramViewModel model)
{
Locatie selectedLocatie = (Locatie)Session["GevondenLocatie"];
Klimatogram klimatogram = new Klimatogram();
klimatogram.Locatie = selectedLocatie;
TempData["kilmatogram"] = kilmatogram;
return RedirectToAction("Index", "Vragen");
}
// ...
public ActionResult Index()
{
var kilmatogram = (Kilmatogram)TempData["kilmatogram"];
// ...
}
Related
I have class for capturing all parameters :
public class UserParams
{
private const int MaxPageSize = 50;
public int PageNumber { get; set; } = 1;
private int pageSize = 10;
public int PageSize
{
get { return pageSize;}
set { pageSize = (value > MaxPageSize) ? MaxPageSize : value;}
}
public int UserId { get; set; }
public string OrderBy { get; set; }
public bool isAll {get; set;} = true;
public bool isManagers { get; set; } = false;
public bool isPartners { get; set; } = false;
}
and my controllers:
[HttpGet]
public async Task<IActionResult> GetUsers([FromQuery] UserParams userParams)
{
var users = await _appRepo.GetUsers(userParams);
var usersDto = _mapper.Map<IEnumerable<UserSimpleDto>>(users);
Response.AddPagination(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);
return Ok(usersDto);
}
While my appRepo.GetUsers:
public async Task<PagedList<User>> GetUsers(UserParams userParams)
{
var users = _context.Users.OrderByDescending(p => p.FirstName).AsQueryable();
if(userParams.isManagers)
{
users = users.Where( u => u.IsManager == true);
}
if(userParams.isPartners)
{
users = users.Where( u => u.IsPartner == true);
}
return await PagedList<User>.CreateAsync(users, userParams.PageNumber, userParams.PageSize);
}
I tried to debug the code with Postman :
http://localhost:5000/api/user?isManagers=1
or
http://localhost:5000/api/user?isManagers=true
but it seems the params isManagers still false (the result is still get all users)
How to fix this bug?
I am using the Caliburn.Micro MVVM pattern.
I am writing an application that has 2 DataGrids, one holds a BindableCollection of RepairOrder, the other a BindableCollection WriteOff.
BindableCollection_WriteOff is property of BindableCollection_RepairOrder. (See the below code).
I need to find a way to write all the RepairOrder including the WriteOff associated with each RO. Besides RepairOrder class holding the WriteOff class, the WriteOff class does not have a way to tie the WriteOff to a RepairOrder.
Repair Order Class:
public class RepairOrder
{
public string Schedule { get; set; }
public string ControlNumber { get; set; }
public int Age { get; set; }
public double Value { get; set; }
public string Note { get; set; }
public double OrgValue { get; set; }
private List<WriteOff> _myWriteOffs;
public List<WriteOff> GetMyWriteOffs()
{
return _myWriteOffs;
}
public void AddMyWriteOff(WriteOff value)
{ _myWriteOffs.Add(value); }
public void DeleteMyWriteOff(WriteOff value)
{ _myWriteOffs.Remove(value); }
public RepairOrder(string CN, string SC, double VL)
{
ControlNumber = CN;
Schedule = SC;
Value = Math.Round(VL, 2);
Note = null;
_myWriteOffs = new List<WriteOff>();
}
public RepairOrder()
{
_myWriteOffs = new List<WriteOff>();
}
public static RepairOrder FromCSV(string CSVLine, string Sched)
{
string[] values = CSVLine.Split(',');
RepairOrder rep = new RepairOrder();
rep.ControlNumber = values[2];
rep.Value = Math.Round(double.Parse(values[5]),2);
rep.Age = int.Parse(values[4]);
rep.Schedule = Sched;
return rep;
}
}
Write Off Class:
public class WriteOff
{
private string _store;
public string Account { get; set; }
public string Description { get; set; }
public double WriteOffAmount { get; set; }
public string Schedule { get; set; }
public string Store
{
get {
if (String.IsNullOrEmpty(_store)) return "";
string temp = _store.Substring(0, 3);
return temp;
}
set { _store = value; }
}
public string Note { get; set; }
public WriteOff(string Acct, string Desc, double Amount, string _store)
{
Account = Acct;
Description = Desc;
WriteOffAmount = Amount;
Store = _store;
}
public string GetWOAccount() {
string SchedAccountNumber = "";
//{ "Navistar", "Cummins", "Misc", "Kenworth", "Mack/Volvo" }
switch (Account)
{
case "Navistar":
SchedAccountNumber = "222000";
break;
case "Cummins":
SchedAccountNumber = "223000";
break;
case "Misc":
SchedAccountNumber = "224500";
break;
default:
SchedAccountNumber = "";
break;
}
return SchedAccountNumber;
}
}
Small doubt, In Update method I had int count, let say count == 4, then i am using for loop to get id, version and set.
In this case Id,version and set values getting only the last value, but how to get the all the values
I tried but i feel its wrong and not working,
Created a seperate list for id, version and set,
eg: _details.imageList.Add(logoHeader.LogoID);
public void Updates(AUnit _aUnit, int Id)
{
ImageDetails _details = new ImageDetails(_aUnit, Id);
int count = (int) _aUnit.ReadBits(8);
for (int i = 0; i < (int) count; i++)
{
_details.ID = (int) _aUnit.ReadBits(8);
_details.Version = (int) _aUnit.ReadBits(8);
_details.set = (int) _aUnit.ReadBits(24);
}
_details.Rset = _aUnit.Buffer.Skip(10).Take(_details.set).ToArray();
//MemoryStream ms = new MemoryStream(_details.PortrateImages.First());
//Image image = Image.FromStream(ms);
//Bitmap bmp = new Bitmap(image);
_details.UpdateTime = DateTime.Now.ToString("h:mm:ss tt");
newData.Add(_details);
}
public class ImageDetails
{
public ImageDetails(AUnit _au, int carouselId)
{
carId = carouselId;
_AUnit = _au;
_updateTime = "";
}
private string _updateTime;
public int ID { get; set; }
public int Version { get; set; }
public int set { get; set; }
public int carId { get; set; }
public byte[] Rset { get; set; }
public AUnit _AUnit { get; set; }
public byte[] bytes { get; set; }
public List<byte[]> dataArray = new List<byte[]>();
public string UpdateTime
{
get { return _updateTime; }
set { _updateTime = value; }
}
public List<byte[]> PImages
{
get
{
List<byte[]> Plogos = new List<byte[]>();
if (carId == 2)
{
Plogos.Add(Rset);
}
return Plogos;
}
}
public List<byte[]> LImages
{
get
{
List<byte[]> Llogos = new List<byte[]>();
if (carId == 1)
{
Llogos.Add(Rset);
}
return Llogos;
}
}
}
You could make detail an object(class) and make a list of details.
like,
public class Detail
{
public int Id{get;set;}
public int Version{get;set;}
public int set{get;set;}
}
and make a list of detail like,
var Details = new List<Detail>();
and you could access it using the foreach loop. like,
foreach(var detail in Details){
Console.log(detail.Id)
.....
}
hope this helps.
I have this:
public class Blah
{
public int id { get; set; }
public string blahh { get; set; }
}
public class Doh
{
public int id { get; set; }
public string dohh { get; set; }
public string mahh { get; set; }
}
public List<???prpClass???> Whatever(string prpClass)
where string prpClass can be "Blah" or "Doh".
I would like the List type to be class Blah or Doh based on what the string prpClass holds.
How can I achieve this?
EDIT:
public List<prpClass??> Whatever(string prpClass)
{
using (var ctx = new ApplicationDbContext())
{
if (prpClass == "Blah")
{
string queryBlah = #"SELECT ... ";
var result = ctx.Database.SqlQuery<Blah>(queryBlah).ToList();
return result;
}
if (prpClass == "Doh")
{
string queryDoh = #"SELECT ... ";
var result = ctx.Database.SqlQuery<Doh>(queryDoh).ToList();
return result;
}
return null
}
}
you have to have a common supertype:
public interface IHaveAnId
{
int id { get;set; }
}
public class Blah : IHaveAnId
{
public int id { get; set; }
public string blahh { get; set; }
}
public class Doh : IHaveAnId
{
public int id {get;set;}
public string dohh { get; set; }
public string mahh { get; set; }
}
then you can do:
public List<IHaveAnId> TheList = new List<IHaveAnId>();
and in some method:
TheList.Add(new Blah{id=1,blahh = "someValue"});
TheList.Add(new Doh{id =2, dohh = "someValue", mahh = "someotherValue"});
to iterate through the list:
foreach(IHaveAnId item in TheList)
{
Console.WriteLine("TheList contains an item with id {0}", item.id);
//item.id is allowed since you access the property of the class over the interface
}
or to iterate through all Blahs:
foreach(Blah item in TheList.OfType<Blah>())
{
Console.WriteLine("TheList contains a Blah with id {0} and blahh ='{1}'", item.id, item.blahh);
}
Edit:
the 2 methods and a int field holding the autovalue:
private int autoValue = 0;
public void AddBlah(string blahh)
{
TheList.Add(new Blah{id = autovalue++, blahh = blahh});
}
public void AddDoh(string dohh, string mahh)
{
TheList.Add(new Doh{id = autovalue++, dohh = dohh, mahh = mahh});
}
Another Edit
public List<object> Whatever(string prpClass)
{
using (var ctx = new ApplicationDbContext())
{
if (prpClass == "Blah")
{
string queryBlah = #"SELECT ... ";
var result = ctx.Database.SqlQuery<Blah>(queryBlah).ToList();
return result.Cast<object>().ToList();
}
if (prpClass == "Doh")
{
string queryDoh = #"SELECT ... ";
var result = ctx.Database.SqlQuery<Doh>(queryDoh).ToList();
return result.Cast<object>.ToList();
}
return null;
}
}
in the view you then have to decide what type it is. In asp.net MVC you can use a display template and use reflection to get a good design. But then i still don't know what technology you are using.
Yet another Edit
TestClass:
public class SomeClass
{
public string Property { get; set; }
}
Repository:
public static class Repository
{
public static List<object> Whatever(string prpClass)
{
switch (prpClass)
{
case "SomeClass":
return new List<SomeClass>()
{
new SomeClass{Property = "somestring"},
new SomeClass{Property = "someOtherString"}
}.Cast<object>().ToList();
default:
return null;
}
}
}
And a controller action in mvc:
public JsonResult Test(string className)
{
return Json(Repository.Whatever("SomeClass"),JsonRequestBehavior.AllowGet);
}
then i called it with: http://localhost:56619/Home/Test?className=SomeClass
And got the result:
[{"Property":"somestring"},{"Property":"someOtherString"}]
Is this what you are trying to do?
public class Blah
{
public int id { get; set; }
public string blahh { get; set; }
}
public class Doh
{
public int id { get; set; }
public string dohh { get; set; }
public string mahh { get; set; }
}
class Program
{
public static List<T> Whatever<T>(int count) where T: new()
{
return Enumerable.Range(0, count).Select((i) => new T()).ToList();
}
static void Main(string[] args)
{
var list=Whatever<Doh>(100);
// list containts 100 of "Doh"
}
}
Hi I have two controller methods. I am passing two parameters from the 1st method to the 2nd. The values inserted to database are correct and not NULL. However when there are displayed back on the webpage in the return Json line, they come out as null and im not sure as to why? Here are the controller methods:
[HttpPost]
public void CalculateAndSaveToDB(BMICalculation CalculateModel)
{
if (ModelState.IsValid)
{
CalculateModel.Id = User.Identity.GetUserId();
CalculateModel.Date = System.DateTime.Now;
CalculateModel.BMICalc = CalculateModel.CalculateMyBMI(CalculateModel.Weight, CalculateModel.Height);
CalculateModel.BMIMeaning = CalculateModel.BMIInfo(CalculateModel.BMICalc);
db.BMICalculations.Add(CalculateModel);
db.SaveChanges();
}
CalculateAndSaveToDB(CalculateModel.BMICalc.ToString(), CalculateModel.BMIMeaning.ToString());
}
public JsonResult CalculateAndSaveToDB(string o, string t)
{
var data = new
{
CalculatedBMI = o,
CalculatedBMIMeaning = t
};
return Json(data, JsonRequestBehavior.AllowGet);
}
Update
BMICalculationsModel:
public partial class BMICalculation
{
public string Id { get; set; }
public System.DateTime Date { get; set; }
public Nullable<double> BMICalc { get; set; }
[Required]
public double Height { get; set; }
[Required]
public double Weight { get; set; }
public int BMICalculationID { get; set; }
public virtual AspNetUser AspNetUser { get; set; }
public string BMIMeaning { get; set; }
public double CalculateMyBMI(double KG, double Height)
{
return KG / (Height * Height);
}
public string BMIInfo(double? BMI)
{
string BMIInfo = "";
if (BMI <= 18.5)
{
BMIInfo = "Underweight";
}
else if (BMI > 18.5 && BMI < 25)
{
BMIInfo = "Average";
}
else if (BMI > 25)
{
BMIInfo = "Overweight";
}
return BMIInfo;
}
}
You need to make your first method return JsonResult and not void. The second CalculateAndSaveToDB returns a JsonResult which never gets used.
I would definitely not call that second method CalculateAndSaveToDB as it doesn't save anything to the DB. Maybe GenerateJsonCalc would be more suitable or maybe no method at all:
[HttpPost]
public JsonResult CalculateAndSaveToDB(BMICalculation CalculateModel)
{
if (ModelState.IsValid)
{
CalculateModel.Id = User.Identity.GetUserId();
CalculateModel.Date = System.DateTime.Now;
CalculateModel.BMICalc = CalculateModel.CalculateMyBMI(CalculateModel.Weight, CalculateModel.Height);
CalculateModel.BMIMeaning = CalculateModel.BMIInfo(CalculateModel.BMICalc);
db.BMICalculations.Add(CalculateModel);
db.SaveChanges();
}
return CalculateAndSaveToDB(CalculateModel.BMICalc.ToString(), CalculateModel.BMIMeaning.ToString());
I would go for something like:
return Json(new
{
CalculatedBMI = CalculateModel.BMICalc.ToString(),
CalculatedBMIMeaning = CalculateModel.BMIMeaning.ToString()
}, JsonRequestBehavior.AllowGet);
Change your return type of your POST method and its last line to this:
public ActionResult CalculateAndSaveToDB(BMICalculation CalculateModel)
{
//stuff
return RedirectToAction("CalculateAndSaveToDB", new { o = CalculateModel.BMICalc.ToString(), t = CalculateModel.BMIMeaning.ToString());
}
try return a dynamic object
public dynamic CalculateAndSaveToDB(BMICalculation CalculateModel)
{
...
return CalculateAndSaveToDB(CalculateModel.BMICalc.ToString(), CalculateModel.BMIMeaning.ToString());
}
public dynamic CalculateAndSaveToDB(string o, string t)
{
dynamic data = new new ExpandoObject();
data.CalculatedBMI = o;
data.CalculatedBMIMeaning = t;
return data ;
}