locked
Send values separately RRS feed

  • Question

  • User1670624291 posted

    Hi
    I am using the following code to send values to my database

    $("#ordertable").on("click", ".Editbtn", function () {
                    var status = new Object();
                    status.Talao = $(this).closest('tr').find(".Talao").val();
                    status.Status = $(this).closest('tr').find(".Status").val();
                    status.Obs = $(this).closest('tr').find(".Obs").val();
                    if (status != null) {
                        $.ajax({
                            type: "POST",
                            url: "/Home/EditPost",
                            data: JSON.stringify(status),
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (response) {
                                if (response != null) {
                                } else {
                                    alert("Something went wrong");
                                }
                            },
                            failure: function (response) {
                                alert(response.responseText);
                            },
                            error: function (response) {
                                alert(response.responseText);
                            }
                        });
    
                    <td style="width: 80px;">
                        @foreach (var item4 in item2.status)
                        {
                            @Html.HiddenFor(m => @item4.ID_Programa, new { @class = "ID_Programa" })
                            @Html.HiddenFor(m => @item4.ID_Linha_Cor, new { @class = "ID_Linha_Cor" })
                            @Html.HiddenFor(m => @item4.ID_Programa_Malha, new { @class = "ID_Programa_Malha" })
                            @Html.HiddenFor(m => @item4.ReturnDate, new { @class = "Data_mod" })
    
                            <!--Talão-->
                            @Html.TextBoxFor(m => @item4.Talao, new { @class = "Talao form-control form-control-sm font-weight-bold", disabled = true, style = "width: 80px;" })                      
                        }
                    </td>
                            <td id="Status">
                                @foreach (var item4 in item2.status)
                                {
                                    <!--Html.TextBoxFor(m => item4.Status, new { class = "Status", disabled = true })-->
                                    @Html.DropDownListFor(m => @item4.Status, new List<SelectListItem>
                                             {
                                           
                                           new SelectListItem { Text = "Sem malha", Value = "Sem malha"},
                                           new SelectListItem { Text = "No gabinete (dos moldes)", Value = "No gabinete (dos moldes)"},
                                           new SelectListItem { Text = "A testar corte", Value = "A testar corte"},
                                           new SelectListItem { Text = "A bordar", Value = "A bordar"},
                                           new SelectListItem { Text = "Para cortar", Value = "Para cortar"},
                                           new SelectListItem { Text = "Em corte", Value = "Em corte"},
                                           new SelectListItem { Text = "A estampar", Value = "A estampar"},
                                           new SelectListItem { Text = "Para colocar", Value = "Para colocar"},
                                           new SelectListItem { Text = "Em confeção", Value = "Em confeção"},
                                           new SelectListItem { Text = "Para aparar", Value = "Para aparar"},
                                           new SelectListItem { Text = "Para lavar", Value = "Para lavar"},
                                           new SelectListItem { Text = "Para tingir", Value = "Para tingir"},
                                           new SelectListItem { Text = "Para ferros", Value = "Para ferros"},
                                           new SelectListItem { Text = "Para arranjos", Value = "Para arranjos"},
                                           new SelectListItem { Text = "Para embalagem", Value = "Para embalagem"},
                                           new SelectListItem { Text = "Embalado", Value = "Embalado"},
                                           new SelectListItem { Text = "Para controle", Value = "Para controle"},
                                               }, @item4.Status, new { @class = "Status form-control form-control-sm font-weight-bold", disabled = true })
                                }
                            </td>
                            <td id="Obs">
                                @foreach (var item4 in item2.status)
                                {
                                    @Html.TextAreaFor(m => @item4.Obs, new { @class = "Obs form-control form-control-sm font-weight-bold", disabled = true, style = "width: 400px;", rows = "1" })
                                }
                            </td>
                            <td style="text-align:right;">
                                <button class="btn ToEditbtn btn btn-outline-success btn-sm"><span aria-hidden="true"></span>Editar</button>
                                <button class="btn Editbtn btn btn-outline-success btn-sm" style="display:none"><span aria-hidden="true">Guardar</span></button>
                                <p style="font-size:10px;">
                                    @foreach (var item4 in item2.status)
                                    {
                                        @item4.Data_mod
                                    }
                                </p>
                            </td>

    Controller

    public ActionResult Index()
    {
         // more code
         return view(result);
    }
    [HttpPost]
            public JsonResult EditPost(Status statusData)
            {
                Status status = new Status()
                {
                    Talao = statusData.Talao,
                    Status = statusData.Status,
                    Obs = statusData.Obs,
                };
                db.Entry(status).State = EntityState.Modified;
                db.SaveChanges();
                return Json(status, JsonRequestBehavior.AllowGet);
            }

    If sending the values together is fine, but my problem is if you insert a separate value, the remaining fields are inserted in white.

    Any command that avoids this type of situation or is it a jquery structure problem?

    Thanks,

    Thursday, February 11, 2021 5:36 PM

Answers

  • User475983607 posted

    Can you provide the markup and tell us what framework?  That will give us a complete picture of what you are trying to do.  Also, use dev tools to debug your code.  Usually the bug jumps out when you take a close look at what the code is doing.

    Below is a working example using ASP.ENT Core.

    public class PostModel
    {
        public int Id { get; set; }
        public string Talao { get; set; }
        public string Status { get; set; }
        public string Obs { get; set; }
    }
    [HttpGet]
    public IActionResult EditPost()
    {
        List<PostModel> model = PopulateData();
        return View(model);
    }
    
    [HttpPost]
    public IActionResult EditPost([FromBody] PostModel model)
    {
        return Json(model);
    }
    @model List<MvcDemo.Controllers.PostModel>
    
    @{
        ViewData["Title"] = "EditPost";
    }
    
    <h1>EditPost</h1>
    
    <p>
        <a asp-action="Create">Create New</a>
    </p>
    <table class="table" id="ordertable">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => Model[0].Talao)
                </th>
                <th>
                    @Html.DisplayNameFor(model => Model[0].Status)
                </th>
                <th>
                    @Html.DisplayNameFor(model => Model[0].Obs)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.Count(); i++)
            {
                <tr>
                    <td>
                        @Html.TextBoxFor(modelItem => Model[i].Talao, null, new { @class = "Talao" })
                    </td>
                    <td>
                        @Html.TextBoxFor(modelItem => Model[i].Status, null, new { @class = "Status" })
                    </td>
                    <td>
                        @Html.TextBoxFor(modelItem => Model[i].Obs, null, new { @class = "Obs" })
                    </td>
                    <td>
                        <button value="@Model[i].Id" class="Editbtn">Edit</button>
                    </td>
                </tr>
            }
        </tbody>
    </table>
    
    @section scripts {
        <script>
            $("#ordertable").on("click", ".Editbtn", function () {
                var status = new Object();
                status.talao = $(this).closest('tr').find(".Talao").val();
                status.status = $(this).closest('tr').find(".Status").val();
                status.obs = $(this).closest('tr').find(".Obs").val();
                console.log(status);
    
                if (status != null) {
                    $.ajax({
                        type: "POST",
                        url: "/Home/EditPost",
                        data: JSON.stringify(status),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            console.log(response);
                        },
                        failure: function (response) {
                            console.log('failure', response);
                        },
                        error: function (response) {
                            console.log('error', error);
                        }
                    });
                }
            });
        </script>
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 11, 2021 8:58 PM

All replies

  • User475983607 posted

    Can you provide the markup and tell us what framework?  That will give us a complete picture of what you are trying to do.  Also, use dev tools to debug your code.  Usually the bug jumps out when you take a close look at what the code is doing.

    Below is a working example using ASP.ENT Core.

    public class PostModel
    {
        public int Id { get; set; }
        public string Talao { get; set; }
        public string Status { get; set; }
        public string Obs { get; set; }
    }
    [HttpGet]
    public IActionResult EditPost()
    {
        List<PostModel> model = PopulateData();
        return View(model);
    }
    
    [HttpPost]
    public IActionResult EditPost([FromBody] PostModel model)
    {
        return Json(model);
    }
    @model List<MvcDemo.Controllers.PostModel>
    
    @{
        ViewData["Title"] = "EditPost";
    }
    
    <h1>EditPost</h1>
    
    <p>
        <a asp-action="Create">Create New</a>
    </p>
    <table class="table" id="ordertable">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => Model[0].Talao)
                </th>
                <th>
                    @Html.DisplayNameFor(model => Model[0].Status)
                </th>
                <th>
                    @Html.DisplayNameFor(model => Model[0].Obs)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.Count(); i++)
            {
                <tr>
                    <td>
                        @Html.TextBoxFor(modelItem => Model[i].Talao, null, new { @class = "Talao" })
                    </td>
                    <td>
                        @Html.TextBoxFor(modelItem => Model[i].Status, null, new { @class = "Status" })
                    </td>
                    <td>
                        @Html.TextBoxFor(modelItem => Model[i].Obs, null, new { @class = "Obs" })
                    </td>
                    <td>
                        <button value="@Model[i].Id" class="Editbtn">Edit</button>
                    </td>
                </tr>
            }
        </tbody>
    </table>
    
    @section scripts {
        <script>
            $("#ordertable").on("click", ".Editbtn", function () {
                var status = new Object();
                status.talao = $(this).closest('tr').find(".Talao").val();
                status.status = $(this).closest('tr').find(".Status").val();
                status.obs = $(this).closest('tr').find(".Obs").val();
                console.log(status);
    
                if (status != null) {
                    $.ajax({
                        type: "POST",
                        url: "/Home/EditPost",
                        data: JSON.stringify(status),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            console.log(response);
                        },
                        failure: function (response) {
                            console.log('failure', response);
                        },
                        error: function (response) {
                            console.log('error', error);
                        }
                    });
                }
            });
        </script>
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 11, 2021 8:58 PM
  • User1670624291 posted

    Hi, is webapp in MVC  on framework 4.7. I'll check the example soon. Thanks

    Thursday, February 11, 2021 10:57 PM
  • User1670624291 posted

    the solution I found was to change my dopdownlist, to load the value into the table.

    thanks for hep.

    Monday, February 15, 2021 10:54 AM