【问题标题】:Access hidden value from View to Controller in ASP.NET Core MVC在 ASP.NET Core MVC 中访问从视图到控制器的隐藏值
【发布时间】:2021-03-06 01:31:09
【问题描述】:

我需要帮助才能使用 jQuery 从 ASP.NET Core razor 视图页面传递隐藏的控件值。

jQuery 用于获取动态控件选择值:

@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>
}

这个 jQuery 工作正常。需要通过 jQuery 向下面的控制器 HTTP post 插入方法再传递一个隐藏的控件值。有人可以帮帮我吗?

 [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");

        }

【问题讨论】:

  • 你能分享你想通过ajax请求发送的示例数据吗?
  • @FeiHan 抱歉回复晚了。我改变了方法并找到了解决方案。谢谢

标签: jquery asp.net-core asp.net-core-mvc


【解决方案1】:

使用它从Controller中的View获取数据

form.request['name of element'];

【讨论】:

  • 我需要通过 jQuery。此外,我尝试过您的建议,但没有成功
猜你喜欢
  • 2010-11-27
  • 2017-01-17
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
相关资源
最近更新 更多