简体   繁体   English

使用ajax MVC传递复杂对象

[英]Pass complex objects using ajax MVC

I have a view with multiple sections. 我有一个包含多个部分的视图。 i would like to update sections individually using partial views and ajax. 我想使用部分视图和ajax单独更新部分。

I have this so far: 到目前为止我有这个:

Controller: 控制器:

[HttpPost]
public PartialViewResult userdetailssettings(UserDetails model)
{ .... }

View Html: 查看Html:

<div id="userDetailsPartial">
    @Html.Partial("_user_details", Model.userdetails)         
</div>

Partial Html: 部分Html:

<div class="form-group">
    <div class="col-md-12">
        @Html.TextBoxFor(x => x.Forename, new { @class = "form-control", placeholder = "Enter your forename" })
        @Html.ValidationMessageFor(x => x.Forename)
    </div>
</div>
<div class="form-group">
    <div class="col-md-12">
        @Html.TextBoxFor(x => x.Surname, new { @class = "form-control", placeholder = "Enter your surname" })
        @Html.ValidationMessageFor(x => x.Surname)
    </div>
</div>

Javascript on View: Javascript on View:

var detailsUrl = "@Url.Action("userdetailssettings", "UserLogin")";
var detailsmodel = JSON.stringify(@Html.Raw(Json.Encode(@Model.userdetails)));

$(document).on('click touchstart', '#saveDetails', function () {      
        $.ajax({
            type: "POST",
            dataType: 'json',
            data: detailsmodel,
            url: detailsUrl,
            contentType: "application/json"
        }).done(function (res) {
            $("#userDetailsPartial").html(res);
            addresssearch();
        });
    });

The model is being passed to the controller by the ajax, however the values are not that of the inputs. 模型由ajax传递给控制器​​,但值不是输入的值。 They are the original values passed from the controller to open the view. 它们是从控制器传递的原始值以打开视图。

I have tried wrapping the partial in tags and also tried adding form tags inside the partial. 我尝试包装部分标签,并尝试在部分内部添加表单标签。

I have also tried putting this code: 我也尝试过这段代码:

var detailsUrl = "@Url.Action("userdetailssettings", "UserLogin")";
var detailsmodel = JSON.stringify(@Html.Raw(Json.Encode(@Model.userdetails)));

Inside the click function. 在点击功能里面。

Nothing I do passes the updated values from the inputs. 我没有做任何事情从输入传递更新的值。

I have thought of creating a new instance of the model from the inputs in the javascript ie 我曾想过从javascript中的输入创建一个新的模型实例,即

var detailsmodel = [ { Forename: $('#Forename').val(), Surname: $('#Surname').val() } ];

But if I am just creating json why can I not just convert the bound model to json. 但是,如果我只是创建json,为什么我不能只将绑定模型转换为json。

why can I not just convert the bound model to json 为什么我不能只将绑定模型转换为json

This is because you are using MVC, not MVVM. 这是因为您使用的是MVC,而不是MVVM。

The "bound model" is one way from the controller to the view via the model; “约束模型”是通过模型从控制器到视图的一种方式; it's possible you're mixing the term "bound model" with "model" and "binding". 你可能会将术语“绑定模型”与“模型”和“绑定”混合在一起。

If you POST the form, you'll get the model in the Action (based on parameters of course), but if you pass via ajax, you'll need to get the current values from the form (as in your comment 'creating a new instance of the model from the inputs'). 如果您发布表单,您将在Action中获取模型(当然基于参数),但如果您通过ajax传递,则需要从表单中获取当前值(如在评论中创建一个来自输入的新模型实例')。

You can generate data to pass via AJAX in various ways, such as: 您可以通过各种方式生成要通过AJAX传递的数据,例如:

var data = $("form").serialize();

rather than adding every input manually. 而不是手动添加每个输入。

var detailsmodel = JSON.stringify... is set when the view is generated and will not change automatically using MVC. var detailsmodel = JSON.stringify...在生成视图时设置,并且不会使用MVC自动更改。

That's because the data you're passing is statically set at page load, based on @Html.Raw(Json.Encode(@Model.userdetails)) . 这是因为你传递的数据是在页面加载时静态设置的,基于@Html.Raw(Json.Encode(@Model.userdetails))

You would need to use something like $form.serialize() , or otherwise create the post body from the actual fields on the page. 您需要使用$form.serialize()类的东西,或者从页面上的实际字段创建帖子正文。

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

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