【问题标题】:How do I pass an MVC Model to a Controller action如何将 MVC 模型传递给控制器​​操作
【发布时间】:2018-12-27 12:19:10
【问题描述】:

我正在尝试将 MVC 模型对象从 JavaScript 传递给控制器​​操作,但是当我已经尝试了一天多时,它要么是 null 要么是错误。

Controller 参数 JC 始终为 null

我的 JavaScript 代码:

$(function() {
  $(".dialog-modal").dialog({
    height: 140,
    modal: true
  });
});

$(function() {
  $("#job-save").dialog({
    height: 240,
    modal: true
  });
});

$("#job-save").dialog({
  close: function(event, ui) {
    window.location.href = "/EquipmentAll";
  }
});

$(function() {
  $("#ContactKey").change(function() {
    var t = $(this).val();

    if (t !== "") {
      $.post("@Url.Action("GetContactDefaults")?val=" + t + "&JC=" + @Model,
        function(res) {
          if (res.Success === "true") {
            //enable the text boxes and set the value
            $("#ContactEmail").prop('disabled', false).val(res.Data.EmailAddress);
            $("#Height").prop('disabled', false).val(res.Data.Height);
          } else {
            alert("Error getting data!");
          }
        }
      )
    } else {
      //Let's clear the values and disable :)
      $("input.editableItems").val('').prop('disabled', true);
    }
  });
});

我的控制器动作代码是:

[HttpPost]
public ActionResult GetContactDefaults(string val, JobCreate JC)
{
    //Contact tContact = JC.ContactList.FirstOrDefault(c => c.ContactKey == val);
    if (val != "")
    {
        // Values are hard coded for demo. you may replae with values 
        // coming from your db/service based on the passed in value ( val.Value)
        return Json(new { Success = "true", Data = new { EmailAddress = "col@ss", TelAreaCode = 345, TelNo = 100, ACa = "444" } });
    }
    return Json(new { Success = "false" });
}

【问题讨论】:

    标签: javascript model-view-controller model controller


    【解决方案1】:

    您应该序列化您的模型并使用Html.Raw 插入。我建议在 POST 正文中传递对象而不是 GET 参数:

    1. 在您看来,将对象放入请求正文:

      var callback = function(res) {
      
         if (res.Success === "true") {
      
          //enable the text boxes and set the value
      
          $("#ContactEmail").prop('disabled', false).val(res.Data.EmailAddress);
          $("#Height").prop('disabled', false).val(res.Data.Height);
      
         } else {
          alert("Error getting data!");
         }
       });
      
      
      $.post("@Url.Action("GetContactDefaults")?val=" + t, "@Html.Raw(Json.Encode(Model))", callback, 'json');
      
    2. 在控制器中添加参数属性:

      [HttpPost]
      public ActionResult GetContactDefaults([FromQuery]string val, [FromBody]JobCreate JC)
      

    【讨论】:

    • 感谢您的快速回复。我现在得到一个 CS0246 找不到类型或命名空间名称“FromQueryAttribute”。缺少 Using 指令或程序集参考?我在这里错过了什么?
    • @Col 您的 ASP.NET MVC 版本是多少?核心,4 还是 5?
    • @Col 尝试将 FromQuery 更改为 FromUri 或将 string val 保留为不带属性
    • System.Web.Mvc 配置为 5.2.3.0
    • 我确实通过删除属性对其进行了进一步处理。但是它现在崩溃了 - 我认为是因为 json 编码数据中有一个 @ 符号,但会看​​得更远。
    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    相关资源
    最近更新 更多