简体   繁体   English

从嵌套的局部视图向控制器发送复杂的视图模型-MVC3

[英]sending complex viewmodel to controller from nested partial views - MVC3

looking for a bit of Ajax help here... 在这里寻找一些Ajax帮助...

I have quite a complex view which is built on quite a complex ViewModel. 我有一个相当复杂的视图,它是基于相当复杂的ViewModel构建的。

On the page I have 3 columns where each column represents a full journey which then breaks down to show the individual flights. 在页面上,我有3列,每列代表完整的旅程,然后细分以显示各个航班。

Each flight is represented by a box containing details (start and end locations, departure time, arrival time, operator etc) The box also contains a dropdown list of all other possible flights that could be taken from this location (eg you might want to hang around the airport for an additional 6 hours, so you could choose a later flight!) 每个航班都由一个包含详细信息(开始和结束位置,出发时间,到达时间,操作员等)的框表示。该框还包含可以从该位置进行的所有其他可能的航班的下拉列表(例如,您可能想挂起在机场周围再飞行6个小时,因此您可以选择以后的航班!)

Anyway, by changing one possible flight, I'm going to have to update the entire column, because taking a later flight will mean subsequent flights might not be possible anymore. 无论如何,通过更改一个可能的航班,我将不得不更新整个专栏,因为进行后续航班将意味着不再可能进行后续航班。

Which leads to my question - is it possible for me to pass a complex viewmodel back to the controller via jQuery? 这就引出我的问题-我是否可以通过jQuery将复杂的视图模型传递回控制器? Can it be done using partial views that are nested inside foreach loops? 是否可以使用嵌套在foreach循环中的局部视图来完成? All I want to do is update the column via ajax, but I can't see how to pass the viewmodel back. 我要做的只是通过ajax更新列,但是我看不到如何将viewmodel传递回来。 I'm trying to avoid doing a full page reload because it will be quite a cumbersome search so ajax updates would be ideal 我正在尝试避免重新加载整个页面,因为这将是一个非常麻烦的搜索,因此ajax更新将是理想选择

Here is some cut-down code for the process: 这是该过程的一些简化代码:

Code for View (index.cshtml) - the jQuery is found here within the main view 视图代码(index.cshtml)-jQuery在主视图中找到

@foreach (var item in Model)
{
    <div id="column-@groupCount">
        @Html.Partial("_JourneyColumnPartial", item)
    </div>
}

<script type="text/javascript">
    $(".flightItem").change(function (event) {
        alert(event.target.id);
        var cSelected = $(this).val();
        alert(cSelected);
        $.ajax({
            url: "/Search/GetChangeInfo",
            contentType: "application/json",
            type: "POST",
            dataType: "json",
            data: ({ cID: cSelected }),
            success: function (display) {
            }
        });
    });
</script>

Code for first partial view (_JourneyColumnPartial.cshtml) 第一个局部视图的代码(_JourneyColumnPartial.cshtml)

<div class="routeContainer">
    @Html.Partial("_ColumnItemPartial", Model)
</div>  

Code for child partial view (_ColumnItemPartial.cshtml) 子局部视图的代码(_ColumnItemPartial.cshtml)

@{
    int journeyNumber = 1;
}
@foreach (var journey in Model.SelectedJourneys)
{
    List<JourneyDetails> list = Model.Journeys.Where(j => j.JourneyDateAndTimeID != journey && j.DepartureID == hub.DepartureID && j.ArrivalID == hub.ArrivalID).OrderBy(j => j.JourneyDateAndTime).ToList();
    <div class="box">
        <div class="journeytimes">
            <b>Depart</b> @hub.DepartureDate<br />
            <b>Arrive</b> @hub.ArrivalDate<br />
        </div>
        <div class="boxcontrols">
            <select class="flightItem" id="flightItem_@{<text>@Model.ColumnNumber</text><text>_</text><text>@journeyNumber</text>}">
                @foreach (var flight in list)
                {
                    <option value="@flight.JourneyID">@flight.ProviderName - @flight.JourneyDateAndTime</option>                                                    
                }                                            
            </select>

        </div>
        <!-- end of box -->
    </div>
    journeyNumber++;    
    <!-- end of box wrap -->
}

I know I've not included details about the ViewModel, but it is quite complex and includes generic lists of other classes so it is quite large. 我知道我没有包括有关ViewModel的详细信息,但是它非常复杂,并且包含其他类的通用列表,因此它很大。 The trigger for the ajax/jQuery is the change event of the dropdown list. Ajax / jQuery的触发器是下拉列表的change事件。

If this makes absolutely no sense, feel free to say and I'll try and clarify what I'm looking for! 如果这完全没有道理,请随时说,我将尽力澄清我要寻找的内容!

Thanks 谢谢

It is possible by sending your data as json in your jquery call. 可以通过在jquery调用中将数据作为json发送。

You can achieve this by simply create a javascript object like your ViewModel, and convert that javascript object to string by Json.Stringify method and set the string value to the data in the ajax call. 您可以通过简单地创建一个像ViewModel这样的javascript对象,然后通过Json.Stringify方法将该javascript对象转换为字符串并将字符串值设置为ajax调用中的data来实现。

我不确定您的问题是发送数据还是返回数据,但这将帮助您通过Ajax返回数据: http : //www.klopfenstein.net/lorenz.aspx/render-partial-view-to-string -asp-net-mvc

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

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