简体   繁体   English

通过ajax将模型和其他值传递给控制器

[英]Passing Model and other values via ajax to Controller

Codes are roughly written as I cannot bring them home. 代码写得很粗,因为我无法带回家。

I am currently working on this application where it has a lot of processes. 我目前正在处理这个应用程序,它有很多流程。 In this scenario, I have controller that makes calls to the server, does some processing, stores data into a model and passes it to the view. 在这种情况下,我有一个控制器,该控制器调用服务器,进行一些处理,将数据存储到模型中并将其传递给视图。

In the view site, I hide some parts of the model so I can pass it again to the controller when needed (I don't wanna make multiple calls to the server). 在查看站点中,我隐藏了模型的某些部分,因此可以在需要时将其再次传递给控制器​​(我不想多次调用服务器)。

Then, when a certain actions happens at the view, the hidden values are then passed to the controller via ajax. 然后,当视图上发生某些动作时,隐藏值然后通过ajax传递给控制器​​。

I know how to pass integers, dates and stuff like that. 我知道如何传递整数,日期和类似的东西。 The problem now is that how do I pass in the model that I got from the first controller? 现在的问题是,如何传递从第一个控制器获得的模型? I really do not know what I am doing wrong. 我真的不知道我在做什么错。 Below are my codes: 以下是我的代码:

Models 楷模

public class ViewFacilitiesModel(){
    public int id {get;set;}
    public List<TimeSlotsModel> tsm {get;set;}
    public List<UsersModel> um {get;set;}
}

public class TimeSlotsModel(){
    public int id {get;set;}
    public TimeSpan openHour {get;set;}
}

public class UsersModel(){
    public int userId {get;set;}
    public string email {get;set;}
}

Controller 调节器

public virtual ActionResult ViewFacilities(){
    ViewFacilitiesModel vfm = new ViewFacilitiesModel();
    vfm.id = 1;
    vfm.tsl = someList;
    vfm.um = anotherList;

    return View(vfm);
}

public ActionResult GetTimings(DateTime testdate, ViewFacilitiesModel vfm){
    //do some other stuff here
}

ViewFacilities.cshtml ViewFacilities.cshtml

@using Project.Models

@Html.HiddenFor(m => m.tsm)
@Html.HiddenFor(m => m.um)


$.ajax({
    url: Url.Action("GetTimings", "Booking"),
    data: {
        'testdate': $("#datepicker").datepicker("getDate"),
        'vfm': JSON.stringify(@Model)
    }
});

The data 'testdate' was passed successfully. 数据“ testdate”已成功传递。 I just really do not know how to pass in the model. 我只是真的不知道如何传递模型。 It comes out null every time. 每次都为空。

Update I've tried passing in the hidden values one by one but I cannot seem to get the original data type to pass through. 更新我尝试过一个一个地传递隐藏值,但是我似乎无法传递原始数据类型。 For example, if the hidden value has a type List , I can't seem to pass it to the controller via ajax. 例如,如果隐藏值的类型为List ,我似乎无法通过ajax将其传递给控制器​​。

You can't do it like you do: 'vfm': JSON.stringify(@Model) . 您无法像执行以下操作那样: 'vfm': JSON.stringify(@Model)

Easiest way that I know is to put all your model values in form and then serialize it with jQuery. 我知道最简单的方法是将所有模型值放入form ,然后使用jQuery序列化。 like this: 像这样:

@using Project.Models

<form id="my-hidden-form">
   @Html.HiddenFor(m => m.tsm)
   @Html.HiddenFor(m => m.um)
   //here your other properties
</form>

$.ajax({
    url: Url.Action("GetTimings", "Booking"),
    data: {
        'testdate': $("#datepicker").datepicker("getDate"),
        'vfm': $(#my-hidden-form).serialize() //here you serialize
    }
});

You need to serialize the form that contains the controls bound to your model items. 您需要序列化包含绑定到模型项的控件的表单。

 var myData = $('#myForm').serialize();

then perform an Ajax post 然后执行Ajax发布

  $.ajax({
        type: "POST",
        url: Url.Action("GetTimings", "Booking"),
        data: myData,
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
    });

this worked for me... so hope it helps or at least points you in the right direction. 这对我有用...所以希望它对您有所帮助或至少为您指明了正确的方向。

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

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