【问题标题】:How to catch the model sent with the partial view from the ajax call?如何从 ajax 调用中捕获使用部分视图发送的模型?
【发布时间】:2023-03-16 19:55:01
【问题描述】:

我有一个返回我需要的部分视图的部分视图,但我需要捕获模型,因为 json.encode 获取了与主视图一起发送的主要模型,因为没有提交到表单。

我需要使用部分视图对发送的模型进行建模,以便我可以使用它而不是 json.encode

   public ActionResult addField(List<Destination> model)
    {
        model.Add(new Destination
        {
            path = String.Empty,
        });

        return PartialView("_ChampDestination",model);
    }

这里是ajax调用

  function addField(event) {

        event.preventDefault();
        var model = @Html.Raw(Json.Encode(Model.RepertoireDestinationMultiple));
        $.ajax({
            type: 'POST',
            contentType: 'application/json',
            url: '@Url.Action("addField", "Flux")',

            data: JSON.stringify(model),
            success: function (response) {
                debugger;
                var x = response.model;
                $(".destinationMultiple").html(response);
            }
            , error: function(xhr, textStatus, error){
                console.log(xhr.statusText);
                console.log(textStatus);
                console.log(error);}
        });

    }

我添加的新动作没有在主模型中注册。

【问题讨论】:

  • 是读取表单并将其传递给操作的问题吗?据我所知,其他一切似乎都很好。为了清楚起见,也许包括一些示例HTMLJSON
  • 局部视图渲染得很好,我想要的是捕获随它发送的模型,这样如果我想添加其他字段我可以使用它,因为在第一次添加后有一个模型发送到查看有 2 个字段,我想使用那个
  • 你已经在var model 中捕获了它,我倾向于将json 存储在隐藏字段或data 属性字段中,以便我以后可以读取/修改它。这就是你所追求的吗?
  • 是的,我想用用于创建局部视图 @Steve0 的数据填充该变量
  • 顺便说一句,var 模型什么都没有,只是在尝试它是否有效。 @史蒂夫0

标签: jquery asp.net ajax model-view-controller


【解决方案1】:

这是我用来修改这样的列表然后提交回服务器的常用方法。

请注意,我实际上并没有在此 sn-p 中调用您的 ajax 函数,因为您表示它工作正常。这个答案显示了某人可能如何操纵字段的内容以供以后提交。

//changed your function to simply submit data
function submitData(event) {

  event.preventDefault();
  $.ajax({
    type: 'POST',
    contentType: 'application/json',
    url: '@Url.Action("addField", "Flux")',

    data: $("input#destinations").val(),
    success: function(response) {
      debugger;
      var x = response.model;
      $(".destinationMultiple").html(response);
    },
    error: function(xhr, textStatus, error) {
      console.log(xhr.statusText);
      console.log(textStatus);
      console.log(error);
    }
  });

}

function addDestination(dest) {
  var data = $.parseJSON($("input#destinations").val() || "[]");
  var obj = {
    path: dest
  }
  data.push(obj);
  $("input#destinations").val(JSON.stringify(data));
  $("#output").text($("input#destinations").val());
}

$("#add").click(function(e) {
  addDestination($("#addIt").val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input id='destinations' type='hidden' value='' /> <!-- original list in JSON goes in the value here -->
<input id='addIt' type='text' value='YOW' />
<button id='add'>ADD</button>
<div id='output'></div>

如果,如您对此答案的评论所示,这需要在服务器端进行修改 input#destinations 需要在服务器端填写,而不是简单地将该输入包含在 partial-view 中并从那里读取并忽略在客户端修改它的代码。

【讨论】:

  • 但是我的修改已经在服务器端完成并通过ajax调用推送到客户端,不能这样做吗?因为我觉得你正在修改客户端并保存它
猜你喜欢
  • 2014-05-26
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 2021-12-14
  • 2018-05-20
  • 1970-01-01
  • 1970-01-01
  • 2015-03-17
相关资源
最近更新 更多