开发者

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.

I want to give DateListed (part of the view model) a value. This value is part of listings. I however, want this value to be in a specific date format: ".ToString("MMMM dd, yyyy");"

Just a bit stuck as to why I am unable to get a value from listings.

Many Thanks, J

Edited to add full 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);
    }


Assuming you have a date listed property on objects within Listings

DateListed = categoryModel.Listings.First().DateListed.ToString("MMMM dd, yyyy")


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/

hopefully this will help

paul


Not sure what your data looks like but shouldn't this do the trick?

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


I think your problem is the call of Single instead of 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜