Model
public class Cart
{
public int id { get; set; }
public int userId { get; set; }
public int messageId { get; set; }
public virtual List<Product> cart { get; set; }
}
ViewModel
public class cartVM
{
public int id { get; set; }
public int productId { get; set; }
public int userId { get; set; }
public int messageId { get; set; }
public DateTime AddDate { get; set; }
public virtual List<Product> Product{ get; set; }
public List<int> SelectedProductIds { get; set; }
public IEnumerable<SelectListItem> CartChoices { get; set; }
}
Controller function to populate Selectedlist
/// here i can only display productname column, how can i display multiple columns for selectlistitem
private void CartChoices(cartVM model)
{
model.CartChoices = db.Products.Select(m => new SelectListItem
{
Value = ((int)m.ProductId).ToString(),
Text = m.productName
});
}
Razor View to display listbox
<div>
#Html.ListBoxFor(m => m.SelectedProductIds , Model.CartChoices)
</div>
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'm quite new to ASP.NET MVC. I am creating a holiday tracking app, and I'm attempting to create a details page for each Employee. I want this page to display a table showing a table of all the holiday request that employee has taken. I've gotten it to work but it only shows only one row.
Here's my models
public partial class Employee
{
public int EmployeeID { get; set; }
public string FullName { get; set; }
public string EmailID { get; set; }
public string Password { get; set; }
public System.DateTime StartDate { get; set; }
public int RoleID { get; set; }
public int ShiftID { get; set; }
public int AreaID { get; set; }
public int DisciplineID { get; set; }
public int SiteID { get; set; }
public int ALCategory { get; set; }
public Nullable<int> HoursTaken { get; set; }
public Nullable<int> AwardedLeave { get; set; }
public Nullable<int> TotalHoursThisYear { get; set; }
public int HoursCarriedForward { get; set; }
public Nullable<int> EntitlementRemainingThisYear { get; set; }
public string Comments { get; set;}
}
Employee Model:
public class EmployeeViewModel
{
public Employee Employee { get; set; }
public HolidayRequestForm HolidayRequestForm { get; set; }
}
Holiday Request Form Model:
public partial class HolidayRequestForm
{
public int RequestID { get; set; }
public int EmployeeID { get; set; }
public System.DateTime StartDate { get; set; }
public System.DateTime FinishDate { get; set; }
public int HoursTaken { get; set; }
public string Comments { get; set; }
public int YearCreated { get; set; }
public int MonthCreated { get; set; }
public Nullable<int> DayCreated { get; set; }
public Nullable<int> YearOfHoliday { get; set; }
public virtual Employee Employee { get; set; }
}
Controller Action:
public ActionResult Details(int? id)
{
Employee employee = db.Employees.FirstOrDefault(emp => emp.EmployeeID == id);
HolidayRequestForm holidayRequestForm db.HolidayRequestForms.FirstOrDefault(emp => emp.EmployeeID == id);
EmployeeViewModel employeeViewModel = new EmployeeViewModel()
{
Employee = employee,
HolidayRequestForm = holidayRequestForm,
};
return View(employeeViewModel);
}
My View is then just a HTML table to display the data from the database.
So the question is how do I display more than one row or entry?? Is it that I'm using FirstorDefault?
You must change Employee Model and declare a list for the Holiday :
Employee Model:
public class EmployeeViewModel
{
public Employee Employee { get; set; }
public List<HolidayRequestForm> HolidayRequestForm { get; set; }
}
Controller Action:
public ActionResult Details(int? id)
{
Employee employee = db.Employees.FirstOrDefault(emp => emp.EmployeeID == id);
List<HolidayRequestForm> holidayRequestForm = db.HolidayRequestForms.Where(emp => emp.EmployeeID == id).ToList();
EmployeeViewModel employeeViewModel = new EmployeeViewModel()
{
Employee = employee,
HolidayRequestForm = holidayRequestForm,
};
return View(employeeViewModel);
}
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 need to map a model to a viewmodel using AutoMapper.
Model:
[Table("News")]
public class News
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
public DateTime DatePostedOn { get; set; }
public int Position { get; set; }
public Category Category { get; set; }
public virtual ICollection<Picture> Pictures { get; set; }
}
[Table("Pictures")]
public class Picture
{
[Key]
public int Id { get; set; }
public DateTime DateCreated { get; set; }
public string Filename { get; set; }
public int Type { get; set; }
public virtual ICollection<News> News { get; set; }
}
Viewmodel:
public class HomeViewModels
{
public IList<HomeMainNews> MainNews { get; private set; }
}
public class HomeMainNews
{
public int Id { get; set; }
public string Title { get; set; }
public string Date { get; set; }
public string PictureURL { get; set; }
}
Mapping:
Mapper.CreateMap<News, HomeMainNews>();
How can I map a News that have a set of Pictures, to a viewmodel with only one picture according to a certain condition "Type = 2"
Current solution:
vm.MainNews = db.News
.Select(n => new HomeMainNews {
Id = n.Id,
Date = n.DatePostedOn.ToString(),
Title = n.Title,
PictureURL = n.Pictures.Where(p => p.Type == 1).Select(p => p.Filename).FirstOrDefault().ToString()
}).ToList();
Automapper solution:
vm.MainNews = db.News.Project().To<HomeMainNews>().ToList();
Try this
Mapper.CreateMap<News, HomeMainNews>()
.ForMember(mainNew => mainNew.Date, opt => opt.MapFrom(news => news.DatePostedOn))
.ForMember(mainNew => mainNew.PictureURL, opt => opt.MapFrom(news => news.Pictures.First(pic => pic.Type == 2).Filename));
At first I should say i am compeletely newbie in MVC.
I have 3 Objects
public partial class Magazine
{
public Magazine()
{
this.NumberTitles = new HashSet<NumberTitle>();
}
public int Id { get; set; }
public int MagYear { get; set; }
public int MagNo { get; set; }
public int MagSeason { get; set; }
public string MagYear2 { get; set; }
public virtual ICollection<NumberTitle> NumberTitles { get; set; }
}
public partial class NumberTitle
{
public NumberTitle()
{
this.Articles = new HashSet<Article>();
}
public int Id { get; set; }
public int MagazineId { get; set; }
public int TitleId { get; set; }
public int position { get; set; }
public virtual ICollection<Article> Articles { get; set; }
public virtual Magazine Magazine { get; set; }
public virtual Title Title { get; set; }
}
public partial class Title
{
public Title()
{
this.ChildrenTitle = new HashSet<Title>();
this.NumberTitles = new HashSet<NumberTitle>();
}
public int Id { get; set; }
public string TitleText { get; set; }
public Nullable<int> ParentId { get; set; }
public virtual ICollection<Title> ChildrenTitle { get; set; }
public virtual Title ParentTitle { get; set; }
public virtual ICollection<NumberTitle> NumberTitles { get; set; }
}
In a View I want to have TextBox to show Magazine Number and 2 List boxes. one shows all the Available Titles and the Other just selected Titles for that Magazine Number.So I have made View Model
public class NumberTitleViewModel
{
public Magazine Magazine { get; set; }
public List<NumberTitle> NumberTitles { get; set; }
}
this is in controller. how can i get the list of titles for specified MagazineId
public ActionResult EditTitle(int id)
{
Func<IQueryable<Magazine>, IOrderedQueryable<Magazine>> orderByFunc = null;
Expression<Func<Magazine, bool>> filterExpr = null;
if (id>0)
{
filterExpr = p => p.Id.Equals(id);
}
Magazine magazine = unitOfWork.MagazineRepository.Get(filter: filterExpr, orderBy: orderByFunc, includeProperties: "").SingleOrDefault();
NumberTitleViewModel numberTitleViewMode = new NumberTitleViewModel();
numberTitleViewMode.Magazine = magazine;
Expression<Func<NumberTitle, bool>> filterExpr2 = null;
if (id > 0)
{
filterExpr2 = p => p.MagazineId.Equals(id);
}
var numberTitles = unitOfWork.NumberTitleRepository.Get(filterExpr2, null, includeProperties: "Title").ToList();
var titles = unitOfWork.TitleRepository.Get(null, null, "");
numberTitleViewMode.NumberTitles = numberTitles; ///this part doesn't show the Titles. how should access the TitleName not Id
ViewBag.titles = new SelectList(titles, "Id", "TitleText");
return View("../Panel/Magazine/EditTitle", "_BasicLayout", numberTitleViewMode);
}
Not sure what you have in your view but you should have something like:
#using NameSpace.Models
#model NameSpace.Models.NumberTitleViewModel
Then you can do something like this in your view:
foreach (NumberTitles item in #Model)
{
<label>#item.Title.TitleText</label>
}
Not exact but should get you close to what you need