简体   繁体   中英

How to return a dynamic number of multiple partial views inside another partial view

I want to return a dynamic number of multiple partial views inside another partial view in the controller, and inject it to the DOM using an Ajax call. The user is going to select a package (radio buttons) and depending on this package I need to return X number of forms to be filled by the user.

This is my Ajax code:

$(function() {
    var serviceURL = "/NewOrderRequestForms/GetFormsToCreateNewOrderRequest";

    $(":radio").change(function() {
        $.ajax({
            url: serviceURL,
            type: "POST",
            data: { account: $("#AccountId").val(), serviceAvailabilityPackageId: $(":radio:checked").val() },
            success: function(xhrData) {
                populateNORForms(xhrData);
            },
            error: function() {
            alert("error");
        }
        });
    });
});

My controller method looks like the following:

public virtual ActionResult GetFormsToCreateNewOrderRequest(Guid account, int serviceAvailabilityPackageId)
{
    var customerNumber = _authorizationUtil.GetAccount(account).CustomerNumber;
    var result = _customFormService.GetFormsToCreateNewOrderRequest(customerNumber.Value,
        serviceAvailabilityPackageId).Select(x => x.FormKey);

    var forms = CustomFormUtil.GetCustomMetaPartial(result);

    //I am confused here
    //return PartialView(something)
}

CustomFormUtil.GetCustomMetaPartial(result) is going to return an IEnumerable<string> of 1 to 6 strings for example "form1, form3" or "form1, form2, form3, form6" etc. I am using this to return a Dictionary of View names and Models to add them in my ultimate partial view.

//Dictionary for Custom Meta Partialviews
public static Dictionary<string, CustomMetaPartialViewModel> CustomMetaPartial2 = new Dictionary<string, CustomMetaPartialViewModel>() 
{
    {"form1_v1", new CustomMetaPartialViewModel(MVC.Service.NewOrderRequestForms.Views.form1_v1, new form1_v1())},
    {"form2_v1", new CustomMetaPartialViewModel(MVC.Service.NewOrderRequestForms.Views._form2_v1,new form2_v1())},
    ...
    {"form7_v1",  new CustomMetaPartialViewModel(MVC.Service.NewOrderRequestForms.Views._form7_v1,new form7_v1())}
};

My question is: how can I add these partial Views with Models into another big partial view and send this one back to the View?

Ok, I have my answer for this, instead of using Ajax to call the List of string I am using to create a PartialView that renders everything I need.

This is my AJAX call

    $(function () {
    var serviceURL = "/NewOrderRequestForms/GetFormsToCreateNewOrderRequest";

    $(":radio").change(function () {
        $.ajax({
            url: serviceURL,
            type: "POST",
            data: { account: $("#AccountId").val(), serviceAvailabilityPackageId: $(":radio:checked").val() },
            success: function (xhrData) {
                $('#NORForms').html(xhrData);
            },
            error: function () {
                alert("error");
            }
        });
    });
});

And the function in the controller is the following:

        public virtual ActionResult GetFormsToCreateNewOrderRequest(Guid account, int serviceAvailabilityPackageId)
    {
        var customerNumber = _authorizationUtil.GetAccount(account).CustomerNumber;
        var result = _customFormService.GetFormsToCreateNewOrderRequest(customerNumber.Value,
            serviceAvailabilityPackageId).Select(x => x.FormKey);

        return PartialView(Views.GenericParentView, result);
    }

I am getting all the information I need from the View, however, instead of returning a List of Strings to the callback in Ajax, I am returning a PartialView having the list of Strings that I needed as the parameter that I need to render the partials Views. Here is the new Partial View that handles this.

    @using UI.Shared.Utils
@model IEnumerable<string>
    @{
        var forms = CustomFormUtil.GetCustomMetaPartial2(Model);
        var result = forms.Select(x => x.Model);  
    }


    @using (Html.BeginForm(MVC.Service.NewOrderRequest.Index(new TdlMasterModel()), FormMethod.Post))
        {
            foreach (var form in forms)
            {
                { Html.RenderPartial(form.View, form.Model); }
            }
            <p style="clear: both">
                <input id="submitNOR" type="submit" value="Submit" style="float: right" />
            </p>
        }

I am sending a list of Strings, and using it to spit out a Dictionary with Model,Views and I am rendering them in the partial view, using a foreach loop. Saving me at the end some extra Ajax calls, in resume I was tackling my problem the wrong way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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