我需要帮助显示comodities的视图,排除重复,但是将数量添加到计数中 E.g。
Name ComodityModel count SellerName
Iphone 3gs 1 Neal
Iphone 4g 1 Jane
Iphone 3gs 1 Neal
Iphone 3gs 1 Jane
输出应为
Name ComodityModel count SellerName
Iphone 3gs 2 Neal
Iphone 4g 1 Jane
Iphone 3gs 1 Jane
我需要通过Comodity模型和SellerName过滤它。我有数据库和模型Comodity
public class Comodity
{
public int ID { get; set; }
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
[Required(ErrorMessage = "Date is required")]
//[DisplayFormat(DataFormatString = "{0:d}")]
[DataType(DataType.Date)]
public DateTime ? RegisterDate { get; set; }
[Required(ErrorMessage = "Comodity Model must be specified")]
public string ComodityModel { get; set; }
[Required(ErrorMessage = "Color must be specified")]
[StringLength(15)]
public string Color { get; set; }
[Required(ErrorMessage = "Price Required")]
[Range(1, 1000, ErrorMessage = "Price must be between $1 and $100")]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal Price { get; set; }
[Required(ErrorMessage = "Seller name must be specified")]
[StringLength(15)]
public string SellerName { get; set; }
public int CountCom { get; set; }
}
public class ComodityDBContext : DbContext
{
public DbSet<Comodity> Comodities { get; set; }
}
和Controller我定义了Actionresult MyView
public ActionResult MyView(DateTime? startDate, DateTime? endDate, string comModel, string searchString)
{
// if (string.IsNullOrEmpty(comModel))
// {
// return View(comodities);
// }
// else
// {
// return View(comodities.Where(x => x.ComodityModel == comModel));
// }
DateTime dtNow;
dtNow = DateTime.Today;
if (!startDate.HasValue)
{
startDate = new DateTime(dtNow.Year, dtNow.Month, 1);
endDate = startDate.Value.AddMonths(1).AddDays(-1);
}
if (startDate.HasValue && !endDate.HasValue)
{
endDate = (new DateTime(startDate.Value.Year, startDate.Value.Month, 1)).AddMonths(1).AddDays(-1);
}
ViewBag.startDate = startDate.Value.ToShortDateString();
ViewBag.endDate = endDate.Value.ToShortDateString();
var viewDate = from r in db.Comodities
where r.RegisterDate >= startDate.Value && r.RegisterDate <= endDate.Value == true
// orderby r.RegisterDate
select r.RegisterDate;
var SelectListName = new List<string>();
var SelectNameQry = from m in db.Comodities
select m.SellerName;
SelectListName.AddRange(SelectNameQry.Distinct());
ViewBag.searchString = new SelectList(SelectListName);
var comModelLst = new List<string>();
var comModelQry = from d in db.Comodities
orderby d.ComodityModel
select d.ComodityModel;
comModelLst.AddRange(comModelQry.Distinct());
ViewBag.comModel = new SelectList(comModelLst);
var comodities = from m in db.Comodities
select m;
IDictionary<string, IList<string>> dict = new Dictionary<string, IList<string>>();
var queryC = from c in db.Comodities
group c by c.ComodityModel into g
where g.Count() > 1
select new { ComodityModel = g.Key, CCount = g.Count() };
foreach (var item in queryC)
{ // comodities = comodities.Where(item => item.Name && item => item.CCount);
//View("", item.ComodityModel, item.CCount);
// ViewBag(item.ComodityModel, item.CCount);
String key = item.ComodityModel;
if (dict.ContainsKey(key))
{
// add the class name into an existing "string" collection
dict[key].Add(item.ComodityModel);
}
else
{
// instantiate a new "string" collection and add the class name.
dict[key] = new List<string> { item.ComodityModel };
}
int maxCourseCount = 0;
foreach (var k in dict.Keys)
{
int valueCount = dict[k].Count;
if (valueCount > maxCourseCount)
maxCourseCount = valueCount;
}
}
if (!String.IsNullOrEmpty(searchString))
{
comodities = comodities.Where(s => s.SellerName.Contains(searchString));
}
if (startDate.HasValue && endDate.HasValue)
{
comodities = comodities.Where(r => r.RegisterDate >= startDate.Value && r.RegisterDate <= endDate.Value);
}
if (string.IsNullOrEmpty(comModel))
{
return View(comodities);
}
else
{
return View(comodities.Where(x => x.ComodityModel == comModel));
}
}
我是MVC的新手,不知道如何计算和查看。请帮忙。还有一些关于日期选择的建议。它显示不正确。我认为它只比较了一天,而不是整个一天 - 一年。提前致谢
最后MyView
@model IEnumerable<SaleCenter.Models.Comodity>
@{
ViewBag.Title = "total";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<h2>total</h2>
<p>
@using (Html.BeginForm()){
<p> startdate:@Html.TextBox("startDate", null, new { @class = "datePicker" })
enddate : @Html.TextBox("endDate", null, new { @class = "datePicker" }) </p>
<p> model: @Html.DropDownList("comModel", "All")
seller: @Html.DropDownList("SearchString", "All")
<input type="submit" value="Total" /></p>
}
</p>
<table>
<tr>
<th>
product name
</th>
<th>
Model
</th>
<th>
seller
</th>
<th>
quantity
</th>
<th>
graphic
</th>
</tr>
@foreach (var item in Model)
{
int w = (int)(2000 * item.CountCom / 100);
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.ComodityModel)
</td>
<td>
@Html.DisplayFor(modelItem => item.SellerName)
</td>
<td>
@Html.DisplayFor(modelItem => item.CountCom)
</td>
<td>
<img src="@Url.Content("~/Content/Images/graph.gif")" width="@w.ToString()px" height="6px" alt="" />
</td>
</tr>
}
</table>
<script type="text/javascript">
$(document).ready(function () {
$('.datePicker').datepicker({ firstDay: 1, dateFormat: 'dd.mm.yy', showOn: 'button',
buttonImage: '/Content/images/calendar.gif',
duration: 0
});
});
</script>
答案 0 :(得分:0)
用于显示商品及其在视图中的计数。快速通过分组名称来创建一个匿名对象并获得它的数量。将此匿名对象作为模型返回到视图。
更好的方法是创建特定于此目的的ViewModel,以便您可以创建紧密绑定的视图。
答案 1 :(得分:0)
这是一个插入ComodityController.cs文件的方法:
private int getComodityCount(string param_Name, string param_ComodityModel, string param_SellerName)
{
return db.Comodity.Where(x => x.Name == param_Name && x.ComodityModel == param_ComodityModel && x.SellerName == param_SellerName).Count();
}
现在,要获取您所称的Comodity Collection中的重复数量:
int count = getComodityCount("iPhone","3gs","Neal");
更重要的是,如果您将隐私设置从私人更改为公开,您甚至可以从您的视图中调用该方法,如下所示:
以下是将方法更改为public:
public int getComodityCount(string param_Name, string param_ComodityModel, string param_SellerName)
{
return db.Comodity.Where(x => x.Name == param_Name && x.ComodityModel == param_ComodityModel && x.SellerName == param_SellerName).Count();
}
以下是您的观看代码:
@{
int count = new ComodityController().getComodityCount("iPhone","3gs","Neal");
}
我希望这会有所帮助。 Where(linq)方法非常适合数据库查询。