So basically I have View where I pass whole enum of my models and it displays them and generates form for each one of them. The problem is that after submitting individual form it doesn't modify my model (it should add string to list of strings). After refresh everything goes back to as it was. I'am beginner so this might be pretty silly problem but I can't find answer becouse my situation is some what different what I find on the internet.

Here's my code

Model:

 public class Post
    {
        [Key]
        public int PostID { get; set; }

        [DataType(DataType.ImageUrl)]
        public string PhotoPath {get; set; }

        [DisplayName("Description")]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        [DisplayName("Title")]
        [Required(ErrorMessage="Title is required")]    
        public string Title { get; set; }

        [Display(Name = "Comments")]
        public List<string> Comments = new List<string>();
    }

Controller:

  [HttpPost]
    public ActionResult Index(int? id) 
    {
        string comment = Request.Form["comment-text"];
        Post post = db.Images.Find(id);

        if (!String.IsNullOrEmpty(comment) && post != null)
        {

                    post.Comments.Add(comment);
                    db.Entry(post).State = EntityState.Modified;
                    db.SaveChanges();

        }
        return View(db.Images.ToList());
    }

And that's my view and I think that's poorly made becouse I can't post single model out of enum instead it gives me back whole enum to controller

<div class="posts-section">
    @foreach (var item in Model)
    {
        <div class="single-post col-lg-4">
            <h3>@Html.DisplayFor(modelItem => item.Title)</h3>
            <img src="@item.PhotoPath" style="width:128px;height:128px" />
            <p>@Html.DisplayFor(modelItem => item.Description)</p>
            <div class="comments">
                @using (Html.BeginForm("Index", "Home", FormMethod.Post))
                {
                    <input id="id" name="id" type="hidden" value="@item.PostID">
                    @Html.LabelFor(modelItem => item.Comments, new { @class = "col-sm-4 control-label" })
                    <div class="col-sm-4">
                        <input type="text" class="form-control" id="@item.PostID" name="comment-text" placeholder="Write you comment here">
                        <button type="submit" class="btn btn-default">Submit</button>
                    </div>
                }

                @foreach (string comment in item.Comments)
                {
                    <div class="single-comment">
                        <p>@comment</p>
                    </div>
                }
            </div>
        </div>
    }
</div>

All I would want to do is get value from input(comment-text) and add that value to particular model's list. My problem is that db.find gets model but it doesn't change it's value becouse after refresh everything disapears

Related posts

Recent Viewed