What should the controller should receive when I am sending a view that contains multiple partial view with different models.
I have a view that renders multiple/dynamic partial views (these views are selecting depending on the package that the user previously selected ), all of these partial views have different models and I need to submit them in one big "parent" form. My main concern is what should the controller receive? and how to handle all the information inside he form, since I have multiple and variable models (depending of what the user choose) that are being send from the form.
This is my my parent view, that renders all the partial views, depending of which package the user chose (i.e. The parent view can render from 1 to 7 forms. all of them have different models). IEnumerable string contains a list of strings what I will use to spit out a Dictionary with Views and Models to render them later in this view.
@using UI.Shared.Utils
@model IEnumerable<string>
@{
var forms = CustomFormUtil.GetCustomMetaPartial(Model);
}
@using (Html.BeginForm(MVC.Service.NewOrderRequest.Index(**I am confused what to pass here**), 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>
}
So let's say the user fills 4 partial views, that means I have to send 4 different models to the controller so I can use the data filled by the user.
This is my controller:
[HttpPost]
[SaveModelState]
public virtual RedirectToRouteResult Index(**What should I receive**)
{
if (!ModelState.IsValid)
{
ModelState.AddModelError("error", "Please fill out the required fields");
return RedirectToAction(MVC.Service.NewOrderRequestForms.Index());
}
...
}
My main concern is what should the controller receive? I have tried the following:
[HttpPost]
[SaveModelState]
public virtual RedirectToRouteResult Index(IEnumerable<ICustomModel> models)
but after some research this is not possible also I have tried to pass all the possible models to the controller
[HttpPost]
[SaveModelState]
public virtual RedirectToRouteResult Index(Model1 model1, ..., ModelN modeln)
This is also not working, and my latest attempt was to create a Master Model that contains all the possible models and pass it to the Controller, this is my Master Model
namespace UI.Next.Areas.Service.Models
{
public class TdlMasterModel : ICustomMetaModel
{
public TdlMasterModel()
{
}
public TdlMasterModel(Guid? accountId)
{
AccountId = accountId;
}
public Guid? AccountId { get; set; }
public Model1 model1{ get; set; }
...
public ModelN modeln{ get; set; }
}
}
Controller:
[HttpPost]
[SaveModelState]
public virtual RedirectToRouteResult Index(MasterModel masterModel)
So far, nothing of the proposed solutions had worked.
Is there any straight solution for this or I will have to create a ModelBinder
to handle this.