简体   繁体   English

MVC 6使用ajax将模型从视图传递到控制器到部分视图

[英]MVC 6 passing a model from view to controller to partial view using ajax

What I am trying to do is create a gui object that can be used from all the different views. 我想要做的是创建一个可以在所有不同视图中使用的gui对象。 The idea is to add the component as a partial view on the main view by placing the component into a placeholder div. 我们的想法是通过将组件放入占位符div来将组件添加为主视图的局部视图。

Here is my model 这是我的模特

    public class Client
    {
        public int clientid { get; set; }
        public string clientname { get; set; }
    }

The java script that I am using to insert the component... Ideally I would prefer to just use the Model data that is passed to this view by a direct call to it, but that was not working which is why I have been troubleshooting with the var model below... 我用来插入组件的java脚本...理想情况下,我更喜欢使用通过直接调用传递给此视图的模型数据,但这不起作用,这就是为什么我一直在进行故障排除下面的var模型......

var model = {
    clientid: 23,
    clientname: "This one"
};
$.ajax({
    type: "POST",
    data: JSON.stringify(model),
    url: "../shared/ClientProgramSelector",
    contentType: "application/json"
}).done(function(result) {
    $("#clientProgramSelectorPlaceholder").html(result);
});

The controller that is being called is here 被调用的控制器在这里

[HttpPost]
public PartialViewResult ClientProgramSelector([FromBody] Client passingData )
{
    return PartialView("ClientProgramSelector", passingData);
}

and finally, here is the div i am trying to place the component into 最后,这是我试图将组件放入的div

<div id="#clientProgramSelectorPlaceholder"></div>

The code is showing no errors, but nothing is rendering on the page. 代码显示没有错误,但页面上没有任何内容呈现。 Does anyone see what I am doing wrong? 有谁看到我做错了什么? Also, is there a way to just use the passed model instead of having to recreate the data? 另外,有没有办法只使用传递的模型而不必重新创建数据?

Thanks in advance 提前致谢

Your ajax success handler is trying to set the result to the jQuery element returned by $("#clientProgramSelectorPlaceholder") . 您的ajax成功处理程序正在尝试将结果设置为$("#clientProgramSelectorPlaceholder")返回的jQuery元素。 This will return the element with Id value "clientProgramSelectorPlaceholder". 这将返回Id值为“clientProgramSelectorPlaceholder”的元素。 But your HTML, you do not have any! 但是你的HTML,你没有! What you have is an element with Id "#clientProgramSelectorPlaceholder" 你拥有的是一个带有"#clientProgramSelectorPlaceholder"的元素

Your div id should be without the # and it should work as long as your ajax call is returning a 200 OK response. 你的div id应该没有# ,只要你的ajax调用返回200 OK响应它就应该工作。

<div id="clientProgramSelectorPlaceholder"></div>

If you want to render the partial view with the modal data passed to main view, you can call the Html.Partial method, Assuming your main view is also strongly typed to the same view model( Client ) object 如果要使用传递给主视图的模态数据渲染局部视图,可以调用Html.Partial方法,假设您的主视图也强类型为同一视图模型( Client )对象

@model Client 

<div id="clientProgramSelectorPlaceholder">
  @Html.Partial("ClientProgramSelector",Model)
</div>

If you want to pass the original model as the ajax post data, you can use JsonConvert.SerializeObject method in your javascript code block. 如果要将原始模型作为ajax post数据传递,可以在javascript代码块中使用JsonConvert.SerializeObject方法。

var model = @Html.Raw(JsonConvert.SerializeObject(Model));
$.ajax({
    type: "POST",
    data: JSON.stringify(model),
    url: "@Url.Action("ClientProgramSelector","YourControllerNameHere")",
    contentType: "application/json"
}).done(function(result) {
    $("#clientProgramSelectorPlaceholder").html(result);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM