I am having a problem to bind the data sent with the model.
What I would like to bind is the OrderStatus that even if I pass it 10 it is always turned into 0.
I don't know why. I made this part bold to make it easier to detect in code:
Data sent as JSON:
{"CustomerOrderHeader":{"OrderNumber":"5",
"PartnerName":"BLI",
"Reference":"",
"ShippingDate":"10/152011",
"OrderStatusName":"Approved",
"CustomerOrderHeader.OrderStatus":"10",
"TotalAmount":"16.00",
"CustomerOrderHeader.CurrencyId":"0",
"CustomerOrderHeader.LanguageId":"0",
"DaLine1":"",
"DaLine2":"",
"DaCity":"",
"DaPostalCode":"",
"DaState":"",
"DaCountry":"",
"BaLine1":"",
"BaLine2":"",
"BaCity":"",
"BaPostalCode":"",
"BaState":"",
"BaCountry":"",
"Notes":""},
"CustomerOrderLines":[{"Id":214,"Num":10,"PartNumber":"dasdasd","Description":"dadadasd","OrderedQty":4,"UoM":"inteee","SalesPrice":null,"Amount":16,"LineStatus":10,"ConfirmedShippingDate":null,"ReservedQty":0,"DeliveredQty":0,"RemainingQty":4}]}
The method signature:
[HttpPost]
public JsonResult SaveOrUpdateOrderLines( CustomerOrderModel customerOrderModel )
and the model:
public class CustomerOrderModel
{
public CustomerOrderModel()
{
this.CustomerOrderLines = new List<CustomerOrderLineModel>();
this.CustomerOrderHeader = new CustomerOrderHeaderModel();
}
public CustomerOrderHeaderModel CustomerOrderHeader
{ get; set; }
public List<CustomerOrderLineModel> CustomerOrderLines
{ get; set; }
}
Customer Order List Model:
public class CustomerOrderLineModel
{
public CustomerOrderLineModel()
{
}
//TODO: Uncomment it
public long Id { get; set; }
[Required]
public String Num
{
get;
set;
}
//[ExistsOrEmptyPartNum(ErrorMessage = "The Part Number doesn't exist")]
//[Required]
[ExistInventoryPartNumOrNullAttribute]
public String PartNumber { get; set; }
public String Description { get; set; }
[Numeric(ErrorMessage = "Should be a number")]
[Min(0, ErrorMessage = "The number should be greater than zero")]
[IsTheSameTypeOfUOM("UnitOfMeasureId")]
public String OrderedQty { get; set; }
public String UoM { get; set; }
public long? UnitOfMeasureId { get; set; }
[Numeric(ErrorMessage = "Should be a number")]
public String SalesPrice { get; set; }
//[Numeric(ErrorMessage = "Should be a number")]
//[Min(0, ErrorMessage = "The number should be greater than zero")]
public String Amount { get; set; }
public String LineStatus { get; set; }
private int? _lineStatusNum;
public int? LineStatusNum
{
get
{
if (this.LineStatus == "Draft")
{
_lineStatusNum = 0;
return _lineStatusNum;
}
_lineStatusNum = null;
return null;
}
set
{
if (value == 0)
{
_lineStatusNum = value;
this.LineStatus = "Draft";
}
if (value == 10)
{
_lineStatusNum = value;
this.LineStatus = "Approved";
}
}
}
public DateTime? ConfirmedShippingDate { get; set; }
[Numeric(ErrorMessage = "Should be a number")]
public String ReservedQty { get; set; }
[Numeric(ErrorMessage = "Should be a number")]
public String DeliveredQty { get; set; }
[Numeric(ErrorMessage = "Should be a number")]
public String RemainingQty { get; set; }
public CustomerOrderHeader CustomerOrderHeader { get; set; }
}
Customer Order Header Model:
public class CustomerOrderHeaderModel
{
public class CustomDropDown
{
public CustomDropDown(object dataValue, string displayValue)
{
this.DataValue = dataValue;
this.DisplayValue = displayValue;
}
public object DataValue { get; set; }
public string DisplayValue { get; set; }
}
public CustomerOrderHeaderModel()
{
this.CurrencyId = 0;
this.TotalAmount = 0;
this.LanguageId = 0;
this.LanguageTypeList = new List<CustomDropDown>
{
new CustomDropDown(0, "English (Local Language)")
};
this.CurrencyTypeList = new List<CustomDropDown>
{
new CustomDropDown(0, "EUR (Local Currency)")
};
}
//public CustomerOrderHeaderModel(int orderStatus, long totalAmount)
//{
// this.OrderStatus = orderStatus;
// this.TotalAmount = totalAmount;
//}
#region Implementation of ICustomerOrderHeader
//public long CompanyId
//{ get; set; }
public string BillingAddress
{ get; set; }
public string ShippingAddress
{ get; set; }
[DisplayName("Customer name*:")]
[Required]
public string PartnerName
//public string CustomerName
{ get; set; }
[DisplayName("Order Number :")]
public long? OrderNumber
{ get; set; }
[DisplayName("Order Status :")]
public int OrderStatus
{ get; set; }
public string OrderStatusName
{ get
{
switch (OrderStatus)
{
case 0:
return "Draft";
case 10:
return "Approved";
case 20:
return "Ready for shipping";
case 30:
return "In Progress";
case 40:
return "Delivered";
case 50:
return "Closed";
case 60:
return "Cancelled";
default:
return "Undefinied";
}
}
}
[DisplayName("Shipping Date :")]
//[Range(typeof(DateTime), "01/01/1753", "12/31/9999", ErrorMessage = "Value for {0} must be between {1} and {2}")]
[DateRange]
//[DataType(DataType.Date, ErrorMessage = "Please enter a valid date in the format dd/mm/yyyy")]
//[Range(typeof(DateTime), "01/01/1753", "12/31/9999", ErrorMessage = "Please enter a valid date in the format dd/mm/yyyy")]
public DateTime? ShippingDate
{ get; set; }
[DisplayName("Reference :")]
public string Reference
{ get; set; }
public Partner Partner
{ get; set; }
[DisplayName("Description :")]
public string Description
{ get; set; }
[DisplayName("Address Line 1 :")]
public string AddressLine1
{ get; set; }
[DisplayName("Address Line 2 :")]
public string AddressLine2
{ get; set; }
[DisplayName("City :")]
public string City
{ get; set; }
[DisplayName("Postal Code :")]
public string PostalCode
{ get; set; }
[DisplayName("State :")]
public string State
{ get; set; }
[DisplayName("Country :")]
public string Country
{ get; set; }
public bool IsDefaultda
{ get; set; }
public bool IsDefaultba
{ get; set; }
[DisplayName("Currency :")]
public long CurrencyId
{ get; set; }
[DisplayName("Language :")]
public long LanguageId
{ get; set; }
[DisplayName("Total Amount :")]
public decimal TotalAmount
{ get; set; }
[DisplayName("Address Line 1 :")]
public string DaLine1
{ get; set; }
[DisplayName("Address Line 2 :")]
public string DaLine2
{ get; set; }
[DisplayName("City :")]
public string DaCity
{ get; set; }
[DisplayName("Postal Code :")]
public string DaPostalCode
{ get; set; }
[DisplayName("State :")]
public string DaState
{ get; set; }
[DisplayName("Country :")]
public string DaCountry
{ get; set; }
[DisplayName("Address Line 1 :")]
public string BaLine1
{ get; set; }
[DisplayName("Address Line 2 :")]
public string BaLine2
{ get; set; }
[DisplayName("City :")]
public string BaCity
{ get; set; }
[DisplayName("Postal Code :")]
public string BaPostalCode
{ get; set; }
[DisplayName("State :")]
public string BaState
{ get; set; }
[DisplayName("Country :")]
public string BaCountry
{ get; set; }
[DisplayName("Notes :")]
public string Notes
{ get; set; }
public List<CustomDropDown> CurrencyTypeList { get; set; }
public List<CustomDropDown> LanguageTypeList { get; set; }
public List<CustomDropDown> OrderStatusTypeList { get; set; }
#endregion
}
At first glance the problem I see is that you are sending "OrderStatusName", but that property only has a get function and not a set.
I haven't examined the rest of the code or tested this; but if you don't have a SET for OrderStatusName, it won't work.
You could either send in the OrderStates (which has both get; set;), or implement the set to the OrderStatusName, which I wouldn't recommend as is not good practice.
Hope this helps,
-covo
Related
I want to select from client model all client without duplicate client and order them by count of the purchases they already did
This is model of client :
public class Client
{
public int ID { get; set; }
public string FacebookName { get; set; }
public string RealName { get; set; }
public string Mobile { get; set; }
public int RegionID { get; set; }
public string Addressdetails { get; set; }
public string Email { get; set; }
[DataType(DataType.Date)]
public DateTime DateOfStart { get; set; }
public virtual Region Region { get; set; }
public virtual List<Order> order { get; set; }
}
this is Order model :
public class Order
{
public int ID { get; set; }
public int ClientID { get; set; }
public Nullable<int> ShippingID { get; set; }
public Nullable<int> SharepointID { get; set; }
public Nullable<int> CallStatuID { get; set; }
public Nullable<int> ShippingStatuID { get; set; }
public Nullable<int> ShippingStatuReasonsID { get; set; }
[DataType(DataType.Date)]
public DateTime Date { get; set; }
// public int OrderStatusID { get; set; }
public Nullable<decimal> ShippingCost { get; set; }
public string purchasetype { get; set; }
public string Notes { get; set; }
public Nullable<decimal> Discount { get; set; }
public decimal Total { get; set; }
[DefaultValue(false)]
public bool Prepared { get; set; }
[DefaultValue(false)]
public bool Done { get; set; }
[DefaultValue(false)]
public bool Shipe { get; set; }
[DefaultValue(false)]
public bool Collected { get; set; }
[DataType(DataType.Date)]
public Nullable<DateTime> DateOfCall { get; set; }
[DataType(DataType.Date)]
public Nullable<DateTime> StockCheckDate { get; set; }
[DataType(DataType.Date)]
public Nullable<DateTime> ShippingDate { get; set; }
public virtual List<OrderDetail> orderdetails { get; set; }
public virtual Client Client { get; set; }
public virtual Shipping shipping { get; set; }
public virtual Sharepoint sharepoint { get; set; }
public virtual CallStatu callStatu { get; set; }
public virtual ShippingStatu ShippingStatus { get; set; }
public virtual ShippingStatuReason ShippingStatuReasons { get; set; }
}
and finally this is OrderDetails model:
public class OrderDetail
{
public int ID { get; set; }
public int OrderID { get; set; }
public Nullable<int> ItemID { get; set; }
public Nullable<int> SizeID { get; set; }
public int ColorID { get; set; }
public decimal Price { get; set; }
public Nullable<int> StockStatuID { get; set; }
public Nullable<int> StockStatuReasonsID { get; set; }
public int Quantity { get; set; }
public decimal Total { get; set; }
public virtual Order order { get; set; }
public virtual Item item { get; set; }
public virtual Size size { get; set; }
public virtual Colors Colors { get; set; }
public virtual StockStatu stockStatus { get; set; }
public virtual StockStatuReason StockStatuReasons { get; set; }
}
I tried this way but it the select is repeat Clients If they make more than one purchase:
public JsonResult TopClientCount()
{
var index = (from dex in db.Clients
join O in db.Orders on dex.ID equals O.ClientID
select new
{
RealName = dex.RealName,
FacebookName = dex.FacebookName,
GovernorateName = dex.Region.governorate.GovernorateName,
RegionName = dex.Region.RegionName,
OrderCount = O.orderdetails.Count()
}).AsEnumerable().OrderByDescending(o => o.OrderCount)
.Distinct()
.ToList();
return Json(index, JsonRequestBehavior.AllowGet);
}
You can use let statement and you can use navigation properties i.e (order ,orderdetails) instead of joins
var index = (from dex in db.Clients
//This will give you all the order details for a client
let orderDetails = dex.order.SelectMany(p=> p.orderdetails)
select new
{
RealName = dex.RealName,
FacebookName = dex.FacebookName,
GovernorateName = dex.Region.governorate.GovernorateName,
RegionName = dex.Region.RegionName,
OrderCount = orderDetails.Count()
}).AsEnumerable().OrderByDescending(o => o.OrderCount)
.Distinct()
.ToList();
My portal has worked fine for several years but nowadays I have problems with my some codes. I want to update my code but at the first step I have problems with login page. when I use FirstOrDefault I got error on object reference while the list is filled and contains 1 user.
int g = db.sp_GetUserData_ByUserName(model.UserName.Trim()).Count();
//it shows 1
OSUserData userData1 = db.sp_GetUserData_ByUserName(model.UserName.Trim()).FirstOrDefault();
I didn't change this code and It works perfectly on the portal. However, I can't run it on my development server.
I checked again and I found that OSUserData constructor throws exception : Enumeration yielded no result. this is my class code :
public OSUserData()
{
this.OSUploadedFilesMultipleUsers = new HashSet<OSUploadedFilesMultipleUser>();
this.OSSalaryLists = new HashSet<OSSalaryList>();
this.OSUploadedFiles = new HashSet<OSUploadedFile>();
}
public int Id { get; set; }
public string Address { get; set; }
public string BranchCode { get; set; }
public string BranchParentNationalCode { get; set; }
public string CitName { get; set; }
public string City { get; set; }
public string CompanyName { get; set; }
public string CouName { get; set; }
public string Country { get; set; }
public string FaxNo { get; set; }
public string FollowCode { get; set; }
public string Hozeh { get; set; }
public Nullable<bool> IsBranch { get; set; }
public string LoginStatus { get; set; }
public string NationalCode { get; set; }
public string NewEconomicNo { get; set; }
public string Oldeconomicno { get; set; }
public string PostalCode { get; set; }
public string ProIdTTMS { get; set; }
public string ProName { get; set; }
public string Provision { get; set; }
public string RegistrationNo { get; set; }
public string State { get; set; }
public string TaxOfficeCode { get; set; }
public string TaxPayerType { get; set; }
public string TelCode { get; set; }
public string TelNo { get; set; }
public string TprId { get; set; }
public string UserName { get; set; }
public bool Validate { get; set; }
public bool Disabled { get; set; }
public string OwnerShipTypeDesc { get; set; }
public Nullable<byte> OwnerShipTypeCode { get; set; }
public bool Processed { get; set; }
public bool CheckedUser { get; set; }
public bool Blocked { get; set; }
public virtual ICollection<OSUploadedFilesMultipleUser> OSUploadedFilesMultipleUsers { get; set; }
public virtual ICollection<OSSalaryList> OSSalaryLists { get; set; }
public virtual ICollection<OSUploadedFile> OSUploadedFiles { get; set; }
You are trying to add to a null/unreferenced/not initialized object/list. Initialize first the list
List<OSUserData> userData1 = new List<OSUserData>();
Then add it
var user = db.sp_GetUserData_ByUserName(model.UserName.Trim()).FirstOrDefault();
userData1.Add(new OSUserData{
Prop1 = user.Prop1,
Prop2 = user.Prop2,
......
});
Or if you only want one user, just make it var
var user = db.sp_GetUserData_ByUserName(model.UserName.Trim()).FirstOrDefault();
I have two classes which are mentioned below. I am trying to map those through Auto Mapper but it is not working.
public class VMTemplates
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Path { get; set; }
public bool IsActive { get; set; }
public System.DateTime TimeStamp { get; set; }
public virtual ICollection<VMTemplateGroup> VMTemplateGroup { get; set; }
}
public VMTemplateViewModel()
{
VMTemplateGroup = new List<VMTemplateGroupViewModel>();
}
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
public string Path { get; set; }
public bool IsActive { get; set; }
[Required(ErrorMessage = "Please Upload File")]
[Display(Name = "Upload File")]
[ValidateFile]
public HttpPostedFileBase TemplateUpload { get; set; }
public System.DateTime TimeStamp { get; set; }
public List<VMTemplateGroupViewModel> VMTemplateGroup { get; set; }
I am using this code to map them
confi.CreateMap<VMTemplateViewModel, VMTemplates>().ReverseMap();
confi.CreateMap<VMTemplateGroupViewModel, VMTemplateGroup>().ReverseMap();
calling code
public VMTemplates GetVMTemplateById(int id)
{
return DataContext.VMTemplates.Include("VMTemplateGroup").Where(a => a.Id == id).FirstOrDefault();
}
This is where exception happens. I get stack overflow exception
public VMTemplateViewModel GetVMTemplateById(int templateId)
{
var result = _vmTemplateRepository.GetVMTemplateById(templateId);
return _autoMapperService.MapEntity<VMTemplateViewModel>(result);
}
I am getting null value for the model on postback.I am not able to find out where I am going wrong.I have seen similar questions but couldn't find any solution yet.
Here is my code:
Controller:
public ActionResult ContactUpdate(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
string[] testsplit = id.Split(',');
List<int> intTest = new List<int>();
foreach (string s in testsplit)
intTest.Add(int.Parse(s));
ObjectParameter ObjParam = new ObjectParameter("ErrorCode", 0);
var cont = db.spErrorContactGet(365, ObjParam);
var ToBeUpdated = (from contacts in cont
where intTest.Contains(contacts.ResponseID)
select contacts);
IEnumerable<spErrorContactGet_Result> Update = ToBeUpdated.ToList();
return View(Update);
}
[HttpPost]
public ActionResult ContactUpdate(List<spErrorContactGet_Result> Res)
{
if (Res == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
//do something
// redirect to another view
}
Here is the model class:
public class spErrorContactGet_Result
{
public int ResponseID { get; set; }
public string ContactAlchemyMessage { get; set; }
public string ContactTeamAlchemyMessage { get; set; }
public string ContactElectronicAddressAlchemyMessage { get; set; }
public string ContactAccountAlchemyMessage { get; set; }
public string CRMContactID { get; set; }
public string InfluenceLevel { get; set; }
public string JobRole { get; set; }
public string Department { get; set; }
public string DepartmentName { get; set; }
public string MobilePhone { get; set; }
public string Email { get; set; }
public string Suffix { get; set; }
public string FaxNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string JobTitle { get; set; }
public string HonorablePrefix { get; set; }
public string Prefix { get; set; }
public string ContactAuthPhoneId { get; set; }
public string ContactAuthDmailId { get; set; }
public string ContactAuthEmailId { get; set; }
public string AllowFax { get; set; }
public string PartnerContactAuthPhoneID { get; set; }
public string PartnerContactAuthDmailID { get; set; }
public string PartnerContactAuthEmailID { get; set; }
public string PrivacyStatementReviewed { get; set; }
public string PreferredLanguage { get; set; }
public string IndWorkPhone { get; set; }
public string FullNamePronunciation { get; set; }
public string CRMOwner { get; set; }
public string KeyContact { get; set; }
public string MarketingAudience { get; set; }
public bool IsSelected { get; set; }
}
}
I am unable to post the view in the right format.
It is likely that structure of your view differs from one that is of your model, they must syntactically match. If you would post your view it could help. BTW there is no 'Postback' in MVC
I need to select all rows from a table with a status as closed, and then I need to display results in a list in a asp.net mvc view.
What should I place in the cshtml first line?
#model HelpDesk.Domain.Entities.Ticket
#{
ViewBag.Title = "Help Desk | Admin Console";
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.selectedSub = 0;
}
How about here.
#using (Html.BeginForm())
{
}
Here is the method.
public Tickets GetTicketByStatus(string status)
{
return context.dbentity.Where()........
}
Here is the model
public class Ticket
{
public int CaseID { get; set; }
public string Title { get; set; }
[Required]
public string UFirstName { get; set; }
[Required]
public string ULastName { get; set; }
//public string UDisplayName { get; set; }
[Required]
public string UDep_Location { get; set; }
[Required]
public string UEmailAddress { get; set; }
//public string UComputerName { get; set; }
//public string UIPAddress { get; set; }
[Required]
public string UPhoneNumber { get; set; }
[Required]
public string Priority { get; set; }
[Required]
public string ProbCat { get; set; }
//public string IniDateTime { get; set; }
//public string UpdateProbDetails { get; set; }
//public string UpdatedBy { get; set; }
public string InitiatedBy_tech { get; set; }
public string AssignedBy { get; set; }
[Required]
public string TechAssigned { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string ProbDetails { get; set; }
[Required]
public string TicketStatus { get; set; }
public string TicketSource { get; set; }
}