1

I have ceremony Model and it has id, date.

I want to get ceremonies which are date<= today.

This is my function in service class.

 public virtual IList<Ceremony> GetAllCeremonyByDate(DateTime currentDate)
        {

            var query = from c in _ceremonyRepository.Table
                        where c.ceremony_date >= currentDate
                        select c;
            var countries = query.ToList();
            return countries;

        }

This is my controller.

   public ActionResult DetailForm()
            {

                Ceremony model = new Ceremony();

                var ceremonyDate = _ceremonyService.GetAllCeremonyByDate(DateTime.Now);
                if (ceremonyDate.Count == 0)
                    return Content("No ceremony can be loaded");

                if (ceremonyDate.Count > 0)
                {

                    foreach (var c in ceremonyDate)
                       // My problem


                }

                return View(model);

            }

I don't know how to assign value to model.

2
  • 1
    Which kind of value you want to assign? Commented Oct 8, 2012 at 5:38
  • ceremony_id and ceremony_date.there are int and dateTime. Commented Oct 8, 2012 at 5:40

3 Answers 3

1

What type is the view expecting?

If it's IEnumerable<Ceremony> (or something similar) then your controller just needs to be:

public ActionResult DetailForm()
{
    var model = _ceremonyService.GetAllCeremonyByDate(DateTime.Now);

    if (model.Count == 0)
    {
        return Content("No ceremony can be loaded");
    }
    else
    {
        return View(model);
    }
}

If the view is expecting a Ceremony then you need to decide which one to send back. If you just want the first valid one then you could do something like:

public ActionResult DetailForm()
{
    var ceremonies = _ceremonyService.GetAllCeremonyByDate(DateTime.Now);

    if (ceremonies.Count == 0)
    {
        return Content("No ceremony can be loaded");
    }
    else
    {
        return View(ceremonies.First());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Since you're returning a IList<Ceremony> your view should accept a model compatible with this type. For example:

Controller

var ceremonies = _ceremonyService.GetAllCeremonyByDate(DateTime.Now);

if (ceremonies.Count == 0)
{
   return Content("No ceremony can be loaded");
}
else
{
    return View(ceremonies);
}

View

@model IEnumerable<Ceremony>

Then you can enumerate your ceremonies in the view like this:

@foreach (var ceremony in Model)
{
  ...
}

I also think you need to correct your logic. Instead of >= use <=.

var query = from c in _ceremonyRepository.Table
            where c.ceremony_date <= currentDate
            select c;

Comments

0

You would just assign the values to the model instant inside your foreach loop. For example,

model.id = c.id;
model.date=c.date;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.