0

매장에서 설문지를 작성할 수있는 응용 프로그램이 있습니다. 이 응용 프로그램 내에서 모든 상점 정보를 포함하는 db.StoreAud (pk : AuditId) 테이블과 모든 질문 정보가 들어있는 db.storequests (pk : ReviewId) 테이블이 있습니다.외래 키가 같은 값일 때 데이터베이스 테이블 행을 업데이트하는 방법 ASP.NET mvc

AuditId는 db.storequests 테이블의 외래 키입니다. 이제 저장소가 데이터가 데이터베이스에 완벽하게 저장되는 설문지를 완료하는 경우 문제가 발생하지만 같은 상점에서 설문지를 다시 작성하면 db.storequests는 이전 주 메뉴를 업데이트하는 대신 새 기본 키 값을 사용하여 데이터베이스에 새 행을 만듭니다 열. 같은 상점에서 같은 설문지를 다시 작성하면 이전 행을 어떻게 업데이트 할 수 있습니까? 그 이후로 만들어지기를 바랍니다.

db.StoreAUD

[DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [Key] 
    [Column(Order = 1)] 
    public int AuditId { get; set; } 

    public string Date { get; set; } 

    public int StoreNumber { get; set; } 
    public string StoreName { get; set; } 

db.storequests

[DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [Key] 
    public int ReviewId { get; set; } 

    public int AuditId { get; set; } 

    public int QuestionOne { get; set; } 

    public string QuestionTwo { get; set; } 
    public string QuestionThree { get; set; } 
    public string QuestionFour { get; set; } 

제어기

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(StoreQuestions storequestions) 
    { 
     if (ModelState.IsValid) 
     { 
      StoreAudit findingAudit = db.StoreAudit.Find(storequestions.AuditId); // grabbing the id from th store audit table 
      findingAudit.ReadOnlyTickBox = true; 
      db.StoreQuestions.Add(storequestions); 
      db.SaveChanges(); 
      return RedirectToAction("Details", "Audit", new { id = storequestions.AuditId }); 
     } 

     return View(storequestions); 
    } 
+0

당신이 StoreAudit을 위해 그것을 같은 방식으로. 먼저 조회하고, 존재하면 리턴 된 행을 갱신합니다. 존재하지 않는 경우에만 추가하십시오. –

+0

업데이트를 위해'Update()'액션을 사용하십시오. – mxmissile

+0

i @ ErikFunkenbusch 생각이 맞지만 성공하지 못했다고 생각하면 실수를했을 때를 대비하여 코드를 게시 할 수 있습니다. – cedPound

답변

0

나는 다음과 같은 새로운 Update 행동으로 업데이트 로직을 분리해서 것이다 Single Responsibility Principle :

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Update(StoreQuestions storequestions) 
    { 
     if (ModelState.IsValid) 
     { 
       StoreAudit findingAudit = db.StoreAudit.Find(storequestions.AuditId); 
       findingAudit.ReadOnlyTickBox = true; 

       // update objects here 

       db.SaveChanges(); 
       return RedirectToAction("Details", "Audit", new { id = storequestions.AuditId }); 
       } 

      return View(storequestions); 
     } 
    } 
+0

아직 메신저가 아닌 업데이트 개체가 필요합니다. 그것을하는 방법에 확실하다. – cedPound

관련 문제