-1

I need help for passing hidden control values from the ASP.NET Core razor view page using jQuery.

jQuery is used to fetch dynamic control selected values:

@section scripts{
    <script>
        $(function () {
            $("button[type='submit']").click(function () {
                event.preventDefault();
                var properties = [];
                $("#tb_properties tr").each(function (index, item) {
                    var $row = $(item), $td = $row.find('td');
                    $td.each(function (i, td) {
                        var propertyname = $td.find("input[type='text']").val();
                        var selctedvalue = $td.find("select").val();
                        properties.push('"' + propertyname + '":"' + selctedvalue + '"');
                    })

                });
                var jsonstr = '{' + properties.join(",") + '}';
                var jsobject = JSON.parse(jsonstr);
                console.log(JSON.stringify(jsonstr));
                console.log(jsonstr);
                $.ajax({
                    type: "Post",
                    url: "/Home/Insert",
                    //data: jsobject,
                    data: jsonstr,
                    contentType: "application/json",
                    success: function (response) {
                        toastr.info(response.status + "<br>" + "<br>" + response.message);
                        $("#tb_properties select").val("");
                        $("#partial_div").load(window.location.href + " #partial_div");
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        console.log('in error');
                    }
                });

            });
        });
    </script>
}

This jQuery is working fine. Need to pass one more hidden control value through jQuery to the below controller HTTP post insert method. Can anybody please help me?

 [HttpPost]
        public IActionResult Insert([FromBody] JObject jsonModel)
        {
            if (jsonModel != null)
            {
                List<K360mapMaster> K360mapListObj = new List<K360mapMaster>();

                foreach (JProperty prop in jsonModel.Children())
                {
                    string key = prop.Name.ToString();
                    string value = prop.Value.ToString();
                    //!string.IsNullOrWhiteSpace(text)                    
                    if (!string.IsNullOrEmpty(value))
                    {
                        K360mapListObj.Add(new K360mapMaster() { ClientCatalog = key, K360catalog = value });
                    }
                }
                if (K360mapListObj.Count > 0)
                {
                    try
                    {
                        _context.K360mapMasters.AddRange(K360mapListObj);
                        _context.SaveChanges();
                        return Json(new { Status = "Success", Message = "No conflicts. Catalogs mapped." });
                    }
                    catch (Exception ex)
                    {
                        TempData["ExceptionmsgM"] = ex;                        
                        throw;
                    }
                }

                else
                {
                    return Json(new { Status = "Error", Message = "Catalogs not selected." });
                }
            }
            return RedirectToAction("Index");

        }
2
  • Can you share the example data that you want to send through ajax request? Commented Mar 8, 2021 at 8:14
  • @FeiHan sorry for the late response. I have changed my approach and find the solution. thanks Commented Mar 10, 2021 at 0:53

1 Answer 1

0

Use this to get data from View in Controller

form.request['name of element'];
Sign up to request clarification or add additional context in comments.

1 Comment

I need through jQuery. Moreover your suggestion I tried but it's not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.