[英]ASP.NET MVC2 C# - Easy Question
var categoryModel = AfvClassifiedsDB.Categories.Include("Listings")
.Single(c => c.Title == categoryName);
var viewModel = new ClassifiedsBrowseViewModel
{
Category = categoryModel,
Listings = categoryModel.Listings.OrderBy(c => c.Price).ToList(),
DateListed =
};
I am retrieving some results from a database and then declaring them in a ViewModel. 我正在从数据库中检索一些结果,然后在ViewModel中声明它们。
I want to give DateListed (part of the view model) a value. 我想给DateListed(视图模型的一部分)一个值。 This value is part of listings.
此值是清单的一部分。 I however, want this value to be in a specific date format: ".ToString("MMMM dd, yyyy");"
但是,我希望此值采用特定的日期格式:“ .ToString(” MMMM dd,yyyy“);”
Just a bit stuck as to why I am unable to get a value from listings. 关于为什么我无法从列表中获得价值的问题,我只停留了一点。
Many Thanks, J 非常感谢,J
Edited to add full ActionMethod: 编辑后添加完整的ActionMethod:
public ActionResult ViewCategory(string categoryName, string searchCriteria = "Price")
{
// Retrieve Category and its associated Listings from the database
var categoryModel = AfvClassifiedsDB.Categories.Include("Listings")
.Single(c => c.Title == categoryName);
var viewModel = new ClassifiedsBrowseViewModel();
viewModel.Category = categoryModel;
viewModel.Listings = categoryModel.Listings.OrderBy(c => c.Price).ToList();
viewModel.DateListed = viewModel.Listings.First().DateListed.ToString("MMMM dd, yyyy");
switch(searchCriteria)
{
case "Title":
viewModel.Listings = categoryModel.Listings.OrderBy(c => c.Title).ToList();
break;
case "Price":
viewModel.Listings = categoryModel.Listings.OrderBy(c => c.Price).ToList();
break;
case "FuelType":
viewModel.Listings = categoryModel.Listings.OrderBy(c => c.FuelType).ToList();
break;
case "Transmission":
viewModel.Listings = categoryModel.Listings.OrderBy(c => c.Transmission).ToList();
break;
default:
viewModel.Listings = categoryModel.Listings.OrderBy(c => c.DateListed).ToList();
break;
}
return View(viewModel);
}
there is a great listing of all the different types on date-time string formatting in this post http://www.csharp-examples.net/string-format-datetime/ http://www.csharp-examples.net/string-format-datetime/中有很多关于日期时间字符串格式的不同类型的清单
hopefully this will help 希望这会有所帮助
paul 保罗
不知道您的数据是什么样子,但是不应该这样做吗?
DateListed = categoryModel.DateListed.ToString("MMMM dd, yyyy");
You can't use property initializer here. 您不能在此处使用属性初始化器。 Assign values step by step:
逐步分配值:
var viewModel = new ClassifiedsBrowseViewModel();
viewModel.Category = categoryModel;
viewModel.Listings = categoryModel.Listings.OrderBy(c => c.Price).ToList();
viewModel.DateListed = viewModel.Listings.Select(item=>item.ToString("MMMM dd, yyyy")).ToList();
A simple explanation: 一个简单的解释:
class Person
{
public int ID { get;set; }
public string Name { get;set; }
}
var p = new Person { ID=1, Name="test1" }; //ok
var another = new Person
{
ID = 2,
Name = "test"+ID //compile error
};
var again = new Person();
again.ID = 3;
again.Name = "test"+again.ID; //of course it's correct
假设您在清单中的对象上具有日期列出的属性
DateListed = categoryModel.Listings.First().DateListed.ToString("MMMM dd, yyyy")
I think your problem is the call of Single
instead of First
我认为您的问题是致电
Single
而不是First
I would use code like this: 我将使用如下代码:
var categoryModel = AfvClassifiedsDB.Categories.Include("Listings")
.First(c => c.Title == categoryName);
var viewModel = new ClassifiedsBrowseViewModel
{
Category = categoryModel,
Listings = categoryModel.Listings.OrderBy(c => c.Price).ToList(),
DateListed = categoryModel.Listings.DateListed.ToString("("MMMM dd, yyyy")
};
Single return a list of length 1, and First returns the first object from the list. Single返回一个长度为1的列表,First返回该列表中的第一个对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.