0

I do not understand why this result keeps coming back null. I know ID 100 exist in the database. I am creating online forms that can be stored into a database. I want to be able to pull them back by ID to update information.

public ActionResult reviewPreevent(int? id)
{
    id = 100;
    if (id.HasValue)
    {
        using(formEntities db = new formEntities()) {
            var form = (from a in db.form_preevent
                select new preeventForm
                {
                    id = a.id,
                    meeting = a.meeting,
                    date = (DateTime)a.eventDate,
                    location = a.location,
                    p1Foyer = (bool)a.p1Foyer,
                    p2Foyer = (bool)a.p2Foyer,
                    meetingRoom = (bool)a.meetingroom,
                    skRoom = (bool)a.skroom,
                    kk1 = (bool)a.kk1,
                    kk2 = (bool)a.kk2,
                    nursery = (bool)a.nursery,
                    Sanctuary = (bool)a.sanctuary,
                    kitchen = (bool)a.kitchen,
                    parkingLot = (bool)a.parkinglot,
                    mainLeaders = a.mainleaders,
                    helpers = a.helpers,
                    backup = a.backuphelps,
                    soundboard = (bool)a.soundboard,
                    soundboardtech = a.soundboardtech,
                    projector = (bool)a.projector,
                    projectorOp = a.projectorop,
                    camera = (bool)a.camera,
                    cameraops = a.cameraops,
                    livestream = (bool)a.livestream,
                    ushers = (bool)a.ushers,
                    totalUshers = (int)a.totalushers,
                    greeters = (bool)a.greeters,
                    totalGreeters = (int)a.totalgreeters,
                    security = (bool)a.security,
                    setupTime = (DateTime)a.setuptime,
                    setup = a.setup,
                    breakdown = a.breakdown,
                    foodItems = a.fooditems,
                    groceryShoppers = a.groceryshoppers,
                    foodPrepPersonal = a.foodprep,
                    estExpense = (float)a.estexpense,
                    estIncome = (float)a.estincome,
                    expense = (float)a.expense,
                    income = (float) a.income
                }).Where(t => t.id == id).FirstOrDefault();
            return View();
        }
    }else
    {
        TempData["notice"] = "No form with ID: " + id + " was found.";
        return View();
    }
}

Also is there an easier way to match a sql class to a viewmodels class?

2
  • Build a minimal query first: var form = db.form_preevent.FirstOrDefault(x => x.id == 100); and see if that works. As for your second question, use AutoMapper to copy all those fields. Commented Dec 27, 2017 at 19:47
  • Please refine your question or mark one of the below answers as accepted. Commented Jan 3, 2018 at 14:24

1 Answer 1

1

You were so close. You must return the Form variable when you return the View to the client.

public ActionResult reviewPreevent(int? id)
{
    id = 100;
    if (id.HasValue)
    {
        using(formEntities db = new formEntities()) {
            var form = (from a in db.form_preevent
                select new preeventForm
                {
                    id = a.id,
                    meeting = a.meeting,
                    date = (DateTime)a.eventDate,
                    location = a.location,
                    p1Foyer = (bool)a.p1Foyer,
                    .
                    .
                    .
                    income = (float) a.income
                }).Where(t => t.id == id).FirstOrDefault();
            return View(form);  //THIS LINE MODIFIED
        }
    }else
    {
        TempData["notice"] = "No form with ID: " + id + " was found.";
        return View();
    }
}
Sign up to request clarification or add additional context in comments.

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.