1

私がするかもしれない誤解を許してください。asp.net c#、OOP、および MVC 4 を学んでいます。

サイトで Annotator プラグインを使用しており、データベースから結果を保存および取得しようとしています。新しいアノテーションが作成されると、このような情報がコントローラーに送信されます。

 {
      "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; } 
}

サーバーからデータを取得してモデルにマップするには、どうすればよいですか。また、プラグインがコントローラーの Details アクションから要求したときにそれを理解できるように、上部の形式でそれを送り返すことができる必要があります。

4

1 に答える 1

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 オブジェクトと一致します。上記のコントローラーアクションと完全に一致すると動作します。

于 2012-12-21T08:09:42.743 に答える