I'm currently working on a page where I can list all the bookings from an account but encountering a problem. I would like to access all the properties from the room that's assigned to a booking but i'm not quite sure how to achieve this.
Bookings Table:
Room Model:
public class Room
{
public int RoomID { get; set; }
public int RoomNumber { get; set; }
public string RoomType { get; set; }
public string Image { get; set; }
public int Price { get; set; }
public int Adults { get; set; }
public int Childs { get; set; }
public bool SmokingRoom { get; set; }
public bool IsOccupied { get; set; }
public Floor Floor { get; set; }
public virtual ICollection<Booking> Bookings { get; set; }
}
Booking Model:
public class Booking
{
public int BookingID { get; set; }
public Account Account { get; set; }
public DateTime DateOfReservation { get; set; }
public DateTime Arrival { get; set; }
public DateTime Departure { get; set; }
public int Adults { get; set; }
public int Children { get; set; }
public Room Room { get; set; }
public bool CheckIn { get; set; }
public bool CheckOut { get; set; }
public string Status { get; set; }
}
Databasehandler method:
public static List<Booking> GetAllBookingsByAccount(int id)
{
HotelDbContext context = new HotelDbContext();
var bookings = context.Bookings.Where(b => b.Account.AccountID == id).ToList();
return bookings;
}
View:
#if (Model.Bookings.Count != 0)
{
<table>
<thead>
<tr>
<th>Arrival</th>
<th>Departure</th>
<th>Hotel</th>
<th>Room</th>
<th>Persons</th>
<th>Price</th>
<th>Status</th>
</tr>
</thead>
<tbody>
#foreach (var booking in Model.Bookings)
{
<tr>
<td>#booking.Arrival.ToShortDateString()</td>
<td>#booking.Departure.ToShortDateString()</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>#booking.TotalPersons</td>
<td></td>
<td>#booking.Status</td>
</tr>
}
</tbody>
</table>
}
else
{
<p>You currently do not have any bookings.</p>
}
Thank you for your time!
For lazy loading to work you need to set the properties as virtual so that EF is able to override the property. This means you do not have to call the Include method.
Update your data model to:
public class Booking
{
public int BookingID { get; set; }
public virtual Account Account { get; set; }
public DateTime DateOfReservation { get; set; }
public DateTime Arrival { get; set; }
public DateTime Departure { get; set; }
public int Adults { get; set; }
public int Children { get; set; }
public virtual Room Room { get; set; }
public bool CheckIn { get; set; }
public bool CheckOut { get; set; }
public string Status { get; set; }
}
That will enable EF to lazyload and you'll be able to access all the properties from that entity without any extra code.
You need to include the room on bookings:
var bookings = context.Bookings.Include("Room").Where(b => b.Account.AccountID == id).ToList();
Related
I have four models ( Item, Fabric, Production, stock)
and i want to get value of stock by item ID like that :
<table class="table-responsive">
<tr>
<td>Color</td>
<td>Size</td>
<td>Quantity</td>
</tr>
<tbody class="StockTbody"></tbody>
</table>
so that the values for the color and size are not repeated.
fabric Model:
public class Fabric
{
public int ID { get; set; }
public string FabricName { get; set; }
//[DataType(DataType.Date)]
[Display(Name = "Color")]
public int ColorID { get; set; }
public string Code { get; set; }
public virtual Colors Colors { get; set; }
public virtual List<FabricStore> fabricStore { get; set; }
public virtual List<Production> production { get; set; }
}
Color Model:
public class Colors
{
public int ID { get; set; }
[Display(Name ="Color")]
public string ColorName { get; set; }
public virtual List<Fabric> fabric { get; set; }
public virtual List<OrderDetail> OrderDetails { get; set; }
}
Item Model :
public class Item
{
public int ID { get; set; }
[Display(Name = "Item")]
public string ItemName { get; set; }
public int code { get; set; }
[Display(Name ="Collection")]
public Nullable<int> CollectionID { get; set; }
[Display(Name = "Category")]
public Nullable<int> CategoryID { get; set; }
public string Image { get; set; }
public string BarcodeImage { get; set; }
public string Barcode { get; set; }
public int NumberOfDraft { get; set; }
public decimal SectoralSellingPrice { get; set; }
public decimal wholesalePrice { get; set; }
public decimal discountPrice { get; set; }
public decimal wholesalevariegated { get; set; }
public decimal DesignCost { get; set; }
public int three { get; set; }
public virtual List<Production> production { get; set; }
public virtual Collection collection { get; set; }
public virtual Categories categories { get; set; }
public virtual List<OrderDetail> orderDetail { get; set; }
}
and this is stock model:
public class Stock
{
public int ID { get; set; }
public int ProductionID { get; set; }
public int SizeID { get; set; }
public int Quantity { get; set; }
public string ShelfNumber { get; set; }
public virtual Production Production { get; set; }
public virtual Size size { get; set; }
public virtual List<StockRemain> stockRemain { get; set; }
}
this is my Controller:
public JsonResult StuckDetails (int ID)
{
var table = (from tb in db.Stocks
where ID == tb.Production.ItemID
select new
{
color = tb.Production.fabric.Colors.ColorName,
ColorId=tb.Production.fabric.ColorID,
size = tb.size.SizeName,
sizeID = tb.SizeID,
//Quantity =
}).Distinct().ToList();
return Json(table, JsonRequestBehavior.AllowGet);
}
the problem is that : i want to sum Quantity
please help
I think you are looking for a group by here:
public JsonResult StuckDetails (int ID)
{
var table = (from tb in db.Stocks
where ID == tb.Production.ItemID
group tb by new
{
tb.Production.fabric.Colors.ColorName,
tb.size.SizeName
} into grp
select new
{
color = grp.Key.ColorName,
size = grp.Key.SizeName,
Quantity = grp.Sum(g => g.Quantity)
});
return Json(table, JsonRequestBehavior.AllowGet);
}
They Key.ColorName property name might vary - I'm not too sure here. Use Intellisense to guide you through it.
I have customer model
public class Customer
{
public Customer()
{
this.SystemValues = new HashSet<SystemValue>();
}
public string Name { get; set; }
public Nullable<System.Guid> GUID { get; set; }
public int Id { get; set; }
public virtual ICollection<SystemValue> SystemValues { get; set; }
}
and systemValue model
public class SystemValue
{
public int CustomerId { get; set; }
public int SystemValueId { get; set; }
public Nullable<int> SystemValueCategoryId { get; set; }
public string SystemValueType { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string TextValue { get; set; }
public Nullable<int> IntValue { get; set; }
public Nullable<double> FloatValue { get; set; }
public byte[] BlobValue { get; set; }
public Nullable<System.DateTime> DateTimeValue { get; set; }
public Nullable<bool> BooleanValue { get; set; }
public Nullable<int> LookupValueId { get; set; }
public Nullable<int> LookupValueGroupId { get; set; }
public Nullable<bool> IsReadonly { get; set; }
public bool IsHidden { get; set; }
public int Id { get; set; }
public virtual Customer Customers { get; set; }
}
in which way I could show a link in CustomerView(CustomersController) foreach customer that redirect to the SystemValuesView(SystemValuesController) with related to this customer SystemValues?
I found out one way - redirect to this controller's action with parameter.
public ActionResult ViewSystemValues(int? id)
{
return RedirectToAction("Index", "SystemValues", new {id});
}
But I'm sure there must be smarter way.
#for(int i = 0; i < Model.yourcustomerlist.Count(); i++)
{
<tr>
<td>
<a class="btn btn-primary" href="#Url.Action("Index", "SystemValues", new { id = Model.yourcustomerlist[i].CustomerId })">
<b>Go to system values</b>
</a>
</td>
</tr>
}
I hope I understood you correctly.
This code should go in the view. The view should be strongly typed to a model.
Code is if you want a button that redirects to the index view of the SystemValues controller, with the CustomerId as input. You should change "yourcustomerlist" to the list containing the customer information. If it's not part of a table, remove the table-related tags (<td> and <tr>).
I am creating a Poll in my ASP.NET MVC project.
1) I select poll and it's replies as an object of Poll model from database [poll model contain a list of reply model that named PollReplies]:
Poll model:
public class Poll
{
public Poll() {
PollReplies = new List<PollReply>();
}
public int ID { get; set; }
public System.DateTime CDate { get; set; }
public System.DateTime SDate { get; set; }
public System.DateTime EDate { get; set; }
public string User_ID { get; set; }
public string Title { get; set; }
public int Language_ID { get; set; }
public bool IsActive { get; set; }
public bool IsPublic { get; set; }
public bool ResultIsPublic { get; set; }
public List<PollReply> PollReplies { get; set; }
}
Reply model:
public class PollReply
{
public int ID { get; set; }
public int Poll_ID { get; set; }
public string Title { get; set; }
public int Count { get; set; }
public bool IsChecked { get; set; }
}
2) I render poll title of the selected poll object as question of poll.
3) With a for render replies:
for (int i = 0; i < Model.PollReplies.Count; i++)
{
#Html.HiddenFor(model => model.PollReplies[i].ID, new { #Value = Model.PollReplies[i].ID })
counter++;
<tr>
<td>#counter</td>
<td>#Html.DisplayFor(model=>model.PollReplies[i].Title)</td>
<td>
#Html.RadioButtonFor(model => model.PollReplies[i].IsChecked,counter)
</td>
</tr>
}
My problem:
1) When I submit form, The Poll model is contain all replies. All replies has ID but the IsChecked propery of replies not set with the client selected radiobutton!(all IsChecked of replies are false) How I can post the selected reply as true?
2) I am using #Html.RadioButtonFor and radios give name attributes automaticly, so the name attribute of generated inputs are diffrente, but i need to just select one of radios! What is the best solution?
My web page / view will have 1 to many import records.
Within each import record will be 0 to many of the following:
Purchase Orders
Shipping Containers
Products
Invoices
I have coded my view model as follows:
namespace LemansCorpIntranet.Models
{
public class ImportWebViewModel
{
public string button { get; set; }
public string status_filter { get; set; }
public string import_id_filter { get; set; }
public DateTime date_filter { get; set; }
public string vendor_filter { get; set; }
public string port_filter { get; set; }
public string whse_filter { get; set; }
public Boolean not_released_filter { get; set; }
public int release_gap { get; set; }
public List<import_row> import_rows;
}
public class import_row
{
public string update_switch { get; set; }
public string id { get; set; }
public IEnumerable<SelectListItem> ship_via { get; set; }
public string broker_number { get; set; }
public string voyage { get; set; }
public string vessel { get; set; }
public decimal shipment_value { get; set; }
public int cartons { get; set; }
public decimal weight { get; set; }
public decimal volume { get; set; }
public string clearance_port { get; set; }
public string warehouses_in_shipment { get; set; }
public string payment_type { get; set; }
public string insurance { get; set; }
public DateTime ship_date { get; set; }
public DateTime close_date { get; set; }
public DateTime customs_date { get; set; }
public string customs_entry { get; set; }
public DateTime pl_2_whse_date { get; set; }
public DateTime estimated_arrival_date { get; set; }
public DateTime wire_transfer_request_done_date { get; set; }
public DateTime approved_broker_bill_date { get; set; }
public DateTime product_released_date { get; set; }
public List<Invoice> Invoices;
public List<PurchaseOrder> PurchasOrders;
public List<Product> Products;
public List<Container> Containers;
}
public class Invoice
{
public string invoice_number { get; set; }
}
public class PurchaseOrder
{
public string id { get; set; }
public string whse { get; set; }
public string vendor_code { get; set; }
public string vendor_name { get; set; }
}
public class Product
{
public int line_number { get; set; }
public string description { get; set; }
}
public class Container
{
public int line_number { get; set; }
public int size { get; set; }
public string id { get; set; }
public string seal { get; set; }
public DateTime received_date { get; set; }
public int cartons { get; set; }
}
}
Is the above OK? Are the classes nested correctly?
I can get the view to display my data from the server correctly, but when I "post" the form the data in the nested classes is null.
I believe this is because the input controls don't get built as they need to be.
My view is similar to the following:
<table>
<% if (Model.import_rows != null)
{ %>
<% for (int row = 0; row < Model.import_rows.Count; row++)
{ %>
<tr><td>
<%=Html.HiddenFor(x=>x.import_rows[row].id) %>
<%=Html.TextBoxFor(x => x.import_rows[row].id)%>
</td>
</tr>
<% } %>
<% } %>
</table>
Any suggestions would be greatly appreciated....
Update: I have changed my view to include "hiddenFor" items.
"import_rows" is no longer null in the controller, but the count is "0".
I still must be missing something.....
Thanks in advance.
I was attempting to pass the viewmodel in a RedirectToAction..
Have discovered this doesn't work that way.
Thanks everyone for your assistance.
I have a Model that named word.
this is my word model
public class Word : BaseFieldsTables
{
int ID { get; set; }
string Name { get; set; }
Guid UniqID { get; set; }
DateTime CreatedDate { get; set; }
byte[] RowVersion { set; get; }
public string Text { get; set; }
public virtual Category Category { get; set; }
public int CategoryID { get; set; }
public virtual Language Language { get; set; }
public int LanguageID { get; set; }
public virtual ICollection<Picture> Pictures { get; set; }
[InverseProperty("MainWord")]
public virtual ICollection<RelationshipBetweenWords> MainWords { get; set; }
[InverseProperty("RelatedWord")]
public virtual ICollection<RelationshipBetweenWords> RelatedWords { get; set; }
}
word has a category and language and...
for example this is my language model
public class Language : BaseFieldsTables
{
int ID { get; set; }
string Name { get; set; }
Guid UniqID { get; set; }
DateTime CreatedDate { get; set; }
byte[] RowVersion { set; get; }
public virtual ICollection<Word> Words { get; set; }
}
i have word word viewmodel like this
public class WordViewModel
public string Name { get; set; }
public IList<SelectListItem> Categories { set; get; }
[HiddenInput(DisplayValue = false)]
public int SelectedCategoryID { set; get; }
public IList<SelectListItem> Languages { set; get; }
[HiddenInput(DisplayValue = false)]
public int SelectedLanguageID { set; get; }
}
and this my controller
public ActionResult Index(
{
IList<Word> list = _wordService.GetAll();
}
i want to get all word and map(auotomapper) to my WordViewModel with languagename and category name and return in my view please help me if i have to change my wordviewmodel or... and how to map.tnx
oh i found it
public ActionResult Index()
{
var orders = _respository.GetAll();
AutoMapper.Mapper.CreateMap<Order, OrderDto>()
.ForMember(dest => dest.OrderNumber, opt => opt.MapFrom(src => src.OrderNo))
.ForMember(dest => dest.OrderItemsDto, opt => opt.Ignore());
var model = AutoMapper.Mapper.Map<IEnumerable<Order>, IEnumerable<OrderDto>>(orders);
return View(model);
}
//Razor View Code
<h2>Orders</h2>
<table>
<tr>
<th>Order Number</th>
<th>Name</th>
<th>Total</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>#item.OrderNumber</td>
<td>#item.CustomerName</td>
<td>$#item.Total</td>
</tr>
}
</table>