2012-12-21 9 views
1

내가 할 수있는 실수를 용서해주십시오. 저는 asp.net C#, OOP 및 MVC 4를 배우고 있습니다.JSON 데이터를 모델링하여 MVC 4에 다시 매핑

내 사이트에서 주석 자 플러그인을 사용하고 있으며 데이터베이스에서 결과를 저장하고 검색하려고합니다. 새로운 주석이 만들어지면 컨트롤러에 이와 같은 정보를 보냅니다.

{ 
     "text":"asdas sd a dasd as dasd", 
     "ranges": 
     [{ 
      "start":"/div/p[3]", 
      "startOffset":195, 
      "end":"/div/p[3]", 
      "endOffset":532 
     }], 
     "quote":"Some quote that was selected.", 
     "UserID":"1", 
     "ProjectDocID":"1" 
} 

이제 모든 데이터를 멋진 쉬운 개체로로드하기를 바랍니다.

// GET: /Annotation/Create 

    public ActionResult Create(Comment comment) 
    { 
     db.Comments.Add(comment); 
     db.SaveChanges(); 
     return Json(comment); 
    } 

모델은 내가 본 것처럼 보였으 나 (변경해야하지만 변경해야 함).

public class Comment 
{ 
    [Key] 
    public int CommentID { get; set; } 
    public int ProjectDocID { get; set; } 
    public int UserID { get; set; } 

    public string text { get; set; } 
    public string Annotation { get; set; } 
    public string quote { get; set; } 
    public string start { get; set; } 
    public string end { get; set; } 
    public string startOffset { get; set; } 
    public string endOffset { get; set; } 
    public DateTime DateCreated { get; set; } 

    public virtual ICollection<CommentVote> CommentVote { get; set; } 
    public virtual ICollection<CommentReply> CommentReply { get; set; } 

    public ProjectDoc ProjectDoc { get; set; } 
    public virtual User User { get; set; } 
} 

서버에서 데이터를 가져 와서 모델에 매핑하려면 어떻게해야합니까? 또한 상단의 형식으로 다시 보낼 수 있어야하므로 플러그인이 컨트롤러의 세부 동작에서 요청할 때이를 이해합니다.

답변

1

이것이 올바른 방법인지 확실하지는 않지만 데이터베이스에 저장할 수있게 해주는 것이 중요합니다. 나는 다음과 같이 모델을 업데이트했다.

public class Comment 
{ 
    [Key] 
    public int CommentID { get; set; } 
    public int ProjectDocID { get; set; } 
    public int UserID { get; set; } 
    public string text { get; set; } 
    public string quote { get; set; } 
    public DateTime DateCreated { get; set; } 

    public virtual ICollection<CommentVote> CommentVote { get; set; } 
    public virtual ICollection<CommentReply> CommentReply { get; set; } 
    public ProjectDoc ProjectDoc { get; set; } 
    public virtual User User { get; set; } 
    public List<ranges> ranges { get; set; } 
} 
public class ranges { 
    public int ID { get; set; } 
    public string start { get; set; } 
    public string end { get; set; } 
    public string startOffset { get; set; } 
    public string endOffset { get; set; } 


} 

이는 Ajax를 사용하여 컨트롤러에 보내는 JSON 객체와 일치합니다. 위의 컨트롤러 동작이 정확히 일치하면 작동합니다.

관련 문제