2017-12-27 24 views
0

왜이 결과가 계속 null로 돌아갈 지 이해할 수 없습니다. ID 100이 데이터베이스에 있다는 것을 압니다. 데이터베이스에 저장할 수있는 온라인 양식을 작성 중입니다. 정보를 업데이트하기 위해 ID로 다시 가져오고 싶습니다.MVC/Entity Framework 동작 편집

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(); 
    } 
} 

또한 viewmodels 클래스에 SQL 클래스와 일치하는 쉬운 방법은 무엇입니까?

+0

최소 쿼리를 먼저 빌드하십시오. 'var form = db.form_preevent.FirstOrDefault (x => x.id == 100);'가 작동하는지 확인하십시오. 두 번째 질문에 대해서는 [AutoMapper] (http://docs.automapper.org/en/stable/Queryable-Extensions.html)를 사용하여 해당 필드를 모두 복사하십시오. –

+0

질문을 수정하거나 아래 답변 중 하나를 수락 한 것으로 표시하십시오. –

답변

1

너무 가까웠습니다. 뷰를 클라이언트에 반환 할 때 Form 변수를 반환해야합니다.

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(); 
    } 
}