-1
[HttpGet]
    public ActionResult Create()
    {
        Articles article = new Articles();
        return View(article);
    }

    [HttpPost]
    public ActionResult Create(Articles article)
    {
        try
        {
            article.Family = article.ArticleIDX;  //그룹번호
            article.Parent = 0;                 //순서
            article.Depth = 1;                  //그룹내 최상위 글로부터 매겨지는 순서
            article.Indent = 0;                 //들여쓰기
            article.ModifyDate = DateTime.Now;
            article.ModifyMemberID = User.Identity.Name;

            db.Articles.Add(article);
            db.SaveChanges();

            if (Request.Files.Count > 0)
            {
                var attachFile = Request.Files[0];

                if (attachFile != null && attachFile.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(attachFile.FileName);
                    var path = Path.Combine(Server.MapPath("~/Upload/"), fileName);
                    attachFile.SaveAs(path);

                    ArticleFiles file = new ArticleFiles();
                    file.ArticleIDX = article.ArticleIDX;
                    file.FilePath = "/Upload/";
                    file.FileName = fileName;
                    file.FileFormat = Path.GetExtension(attachFile.FileName);
                    file.FileSize = attachFile.ContentLength;
                    file.UploadDate = DateTime.Now;

                    db.ArticleFiles.Add(file);
                    db.SaveChanges();
                }
            }
            ViewBag.Result = "OK";
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Board");
            Debug.WriteLine(ex.ToString());
            ViewBag.Result = "FAIL";
        }
        return View(article);
        //return RedirectToAction("ArticleList");
    }
[HttpGet]
    public ActionResult ReplyCreate(int aidx)
    {
        Articles articleReply = new Articles();
        return View(articleReply);
    }

    [HttpPost]
    public ActionResult ReplyCreate(Articles replyArticles)
    {
        Articles articles = new Articles();
        try
        {
            //부모글 불러오기(글번호로 불러오기)
            Articles note = db.Articles.Find(articles.ArticleIDX);

            //Family는 원글의 번호
            replyArticles.Family = replyArticles.ArticleIDX;
            //Parent 순서

            //Depth 는 답글의 글 번호
            //Indent 들여쓰기

            replyArticles.ModifyDate = DateTime.Now;
            replyArticles.ModifyMemberID = User.Identity.Name;

            db.Articles.Add(replyArticles);
            db.SaveChanges();

            ViewBag.Result = "OK";
        }
        catch (Exception ex)
        {
            ViewBag.Result = "FAIL";
        }
        return View(replyArticles);
    }

public partial class Articles
{
    [Key]
    public int ArticleIDX { get; set; }

    public int? Family { get; set; }

    public int? Depth { get; set; }

    public int? Indent { get; set; }

    public int? Parent { get; set; }

    [StringLength(200)]
    public string Title { get; set; }

    [Column(TypeName = "text")]
    public string Contents { get; set; }

    [StringLength(50)]
    public string Category { get; set; }

    [StringLength(20)]
    public string ModifyMemberID { get; set; }

    public DateTime? ModifyDate { get; set; }

    public virtual Members Members { get; set; }

}

The above code is the code I implemented. Articles created using the create method are stored in the database. What do you do when you try to recall a post stored in the database with ReplyCreate? The null value is often entered into the model. I try to find it using the primary key, but the primary key also has a null value.

Cœur
  • 37,241
  • 25
  • 195
  • 267
김세림
  • 291
  • 1
  • 5
  • 17
  • _Primary key has a null value_ ? really not a primary key https://stackoverflow.com/questions/3906811/null-permitted-in-primary-key-why-and-in-which-dbms – Steve Jan 12 '18 at 08:21
  • 1
    A primary key must have unique value, it cannot be null or empty. You can use query to retrieve `articleReply` values from DB by mentioning that PK and present it to view. – Tetsuya Yamamoto Jan 12 '18 at 08:22
  • The primary key stored in the database is not null. When I load it from the controller, it is not called properly. – 김세림 Jan 12 '18 at 08:26
  • Articles note = db.Articles.Find(articles.ArticleIDX); This means that the null value is displayed. The primary key in the database is not null. – 김세림 Jan 12 '18 at 08:28
  • 1
    Look again at your code. What article are you searching for with the Find method ? The one passed in the input parameter or ....? – Steve Jan 12 '18 at 08:31
  • I want to search for a primary key with Find. I can not search. How do I retrieve it when I call it? – 김세림 Jan 12 '18 at 08:37

1 Answers1

0
Articles note = db.Articles.Find(articles.ArticleIDX);

does not work because articles is an empty object, due to the line

Articles articles = new Articles();

just above. You never set any of the fields in this object, including the ArticleIDX, before calling the Find method.

I think you probably intended to search using the value passed in to the controller? In that case it would need to be

Articles note = db.Articles.Find(replyArticles.ArticleIDX);

because replyArticles is the variable which was received from the browser in the request. I assume this contains a value in the ArticleIDX field.

Having said that, I don't know what the purpose of this line of code is, because you never use the note object in any of the following code, so I don't know why you needed to find it.

ADyson
  • 57,178
  • 14
  • 51
  • 63