0

I have page where employee can Add/Edit/Delete their own Comments under each RMA/product. Add method its working fine employee can add their own Comments under each RMA/product and Delete method is also working fine for each Comments , BUT i do have problem with Edit method for each Comments , let me explain first relation btw tbl RMA_History and tbl Comments which i inner join tbl Comments (RMAID) with tbl RMA_History (ID) (to get which Comments related to which RMA).

  • Design Tbl RMA_History:

Id int
Description text
 
132 somevalue
133 somevalue

  • Design Tbl Comments:

ID int
RMAID int
MSG text

1 132 Hello world
2 132 Smith John
3 132 Good bye
4 133 test

enter image description here

And when i try to Edit Comment to something else and than hit submit button to update comments ,but no luck update not going to work:( and i also did the debugg in UpdateComments Action everything it was NULL.

Can anyone please help me or point me in the right direction !
Thanks in advance :)

View:

    @using NameSpace.Models
    @model VMRMA 

    //If Comments modal is null dont Display  
    @if (!Model.Comment_Lists.Any())
       {
        <div style="display:none;" class="form-group">
        <label style="display:none;" class="form-control-label">PM</label>
        @foreach (var item in Model.Comment_Lists)
        {
        <a onclick="return confirm('Er du sikker på, at du vil fjerne denne email?');" 
       href="/User/DeleteComments/@item.ID">@item.PM</a>

       }

    </div>

       }

    //else Loop Modal Comments out
      else
      {
       <div class="col-lg-12">
       <div class="articles card">
       <div class="card-header d-flex align-items-center">
       <h2 class="h3">Comments</h2></div>
       </div>
        //Loop Comments
       @foreach (var item in Model.Comment_Lists)
       {
        if (item.PM == "")
       {
         <div style="display:none;" class="card-body no-padding">
         <div class="item d-flex align-items-center">
         <div class="image"><img src="~/assist_Admin/img/avatar-1.jpg" class="img-fluid 
         rounded-circle"></div>
        <div class="text">
        <h3 class="h5">@item.PM</h3>

        </div>                                                 
        </div>


</div>
  }

  else
   { 
     <div class="card-body no-padding">
     <div class="item d-flex align-items-center">

    <div class="text">
    <h3 class="h5">@item.PM</h3>
    <span id="DeleteRow">
    <a style="color:red; font-size:15px;" title="Slet" id="@item.ID" 
    href="/User/DeleteComments/@item.ID">
    <i class="fa fa-trash-o" aria-hidden="true"></i>
   </a>
 </span>
  </div>                                                 
</div>

    //UPDATE

   <div class="expandContent">                                                                  
   <span>update</span>
    <div class="showMe">
   <input type="text" name="SMS" id="SMS" value="@item.PM " />
   <input type="hidden" name="ID" id="ID" value="@item.ID " />
   @*<input type="hidden" name="RMAID" id="RMAID" value="@item.RMAID " />*@
   </div>
  <button type="button" id="SaveCommentsRecord">send</button>
   </div>
   </div>
   </div>

   </div>

   }
     } // End of Loop

   </div>
    </div>
}

AJAX:

<script>
    $("#SaveCommentsRecord").click(function (e) {
        e.preventDefault();
        var model = {

            ID: $("#ID").val(),
            SMS: $("#SMS").val(),
           //RMAID: $("#RMAID").val

        }
        $.ajax({
            type: "POST",
            url: "/User/UpdateComments",
            data: JSON.stringify(model),
            success: function (result) {

                if (!$.trim(result)) {
                    alert("What follows is blank: ");
                }
                else {
                    result.ID = model.ID;
                    result.MSG = model.SMS;
                    //result.RMAID = model.RMAID;

                    console.log("Send");

                }



            },
            error: function () {
                console.log('something went wrong - debug it!');
            }
        })
    })
</script>

Comments & UpdateComments Action:

    //List Of Comments Action
    public ActionResult RMA(int? id, VMRMA model)
        {


                    var queryTwo = (from RH in db.RMA_History

                           join Com in db.Comments on RH.Id equals Com.RMAID

                           where Com.RMAID == id

                           select new VMRMA.Comment_List
                           {
                               PM = Com.MSG,
                               ID = Com.ID,
                               Date = Com.Date,
                               Forfatter = Com.Writer,
                               whos = Com.Seen,
                               RMAID = Com.RMAID

                           });

        }


       model.Comment_Lists = queryTwo.ToList();

       return View(model);

UpdateComments Action:

   //UpdateComments Action

public JsonResult UpdateComments(VMRMA model)
{


    var u = db.Comments.Where(a => a.ID == model.ID).FirstOrDefault();

    if (u != null)
    {

        u.MSG = model.SMS;
        u.ID = model.ID;
        db.SaveChanges();

    }

    return Json(u, JsonRequestBehavior.AllowGet);

}

VM and Comment_List ViewModel:

  public class VMRMA

    { 
        public int ID { get; set; }
        public string SMS{ get; set; }
        public int? RMAID { get; set; }

        public List<Comment_List> Comment_Lists { get; set; }

        public class Comment_List
        {

            public Comment_List()
            {

            }

            public Comment_List(string PM, int? ID, DateTime Date, string Forfatter , string whos , int? RMAID)
            {
                this.PM = PM;
                this.ID = ID;
                this.Date = Date;
                this.Forfatter = Forfatter;
                this.whos = whos;
                this.RMAID = RMAID;

            }

            public int? ID { get; set; }

            public string PM { get; set; }

            public DateTime Date { get; set; }

            public string Forfatter { get; set; }

            public string whos { get; set; }

            public int? RMAID { get; set; }


        }


     }
7 seconds
  • 133
  • 2
  • 17
  • I'm not seeing in your posted code how your UpdateComments action can ever get called. Is there more code for the view that you haven't posted? Like the surrounding Form or javascript? – G_P Jul 13 '18 at 11:33
  • @G_P my bad i was forgat to add AJAX part sry , i just added – 7 seconds Jul 13 '18 at 11:38
  • if you look at the Ajax Post from the browser, are the values set there? If they are, you could try changing your UpdateComments method from UpdateComments(VMRMA model) to UpdateComments(int ID, string SMS) – G_P Jul 13 '18 at 11:44
  • @G_P yes its set and instead biniding in modal , pass value of parameters to controller i did before , but no luck when i debugged it tell me its null – 7 seconds Jul 13 '18 at 11:52
  • Try setting a break point in your UpdateComments action, and looking at the Request object in the debugger - are there any values in there? – G_P Jul 13 '18 at 12:01
  • @G_P nup :( its null – 7 seconds Jul 13 '18 at 12:03
  • If the values are there when looking at the POST in the network tab of your browser dev tools, they should part of the Request when it hits the controller... Try reviewing the accepted answer here: https://stackoverflow.com/questions/33947882/pass-model-to-controller-using-jquery-ajax for possible tips on additional options – G_P Jul 13 '18 at 12:19

0 Answers0