当我发送包含不同型号的多个局部视图视图控制器应该得到什么? [英] What the controller should receive when I am sending a view that contains multiple partial view with different models?

查看:137
本文介绍了当我发送包含不同型号的多个局部视图视图控制器应该得到什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该当我发送包含不同型号的多个局部视图视图控制器应该接受。

What should the controller should receive when I am sending a view that contains multiple partial view with different models.

我有一个生成多个/动态局部视图(这些观点都取决于用户previously选择的套餐选择),所有这些部分观点有不同型号的景色,我需要提交他们在一个大父的形式。我最关心的是什么都要控制器接收?以及如何处理所有的信息里面,他的形式,因为我有多个可变模型(取决于用户选择什么样的),它们是从形式发送。

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.

这是我的我的父视图,呈现所有的部分意见,根据该包的用户选择的(即父视图可以从1到7的形式呈现。所有的人有不同的型号)。 IEnumerable的字符串包含字符串列表什么,我会用它来吐出一个字典视图和模型后,使它们在这个视图。

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>
}

所以我们可以说用户填写4个部分的意见,这意味着我要送4不同型号的控制器,这样我就可以使用由用户填写的数据。

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.

这是我的控制器:

[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; }
  }
}

控制器:

[HttpPost]
[SaveModelState]
public virtual RedirectToRouteResult Index(MasterModel masterModel)

到目前为止,没什么提出的解决方案中工作过。
对此有任何直溶液或我将不得不创建一个 ModelBinder的来处理这个问题。

推荐答案

使用含有模型视图模型为每个表单要渲染

Use a view model containing the models for each form you want to render

查看型号

public class MasterVM
{
  public Model1 Model1 { get; set; }
  public Model2 Model2 { get; set; }
  ....
}

控制器

public ActionResult Index()
{
  MasterVM model = new MasterVM();
  // Set the values of only those models you want to display forms for
  model.Model2 = new Model2();
  return View(model);
}

[HttpPost]
public ActionResult Index(MasterVM model)
{
  if(!ModelState.IsValid)
  {
    return View();
  }
  if (model.Model1 != null)
  {
    // save Model1
  }
  else if (model.Model2 != null)
  {
    // save Model2
  }
  ....
  // redirect somewhere
}

_Model1.cshtml

@model Model1

@Html.LabelFor(m => m.SomeProperty)
@Html.TextBoxFor(m => m.SomeProperty)
@Html.ValidationMessageFor(m => m.SomeProperty)

@Html.LabelFor(m => m.AnotherProperty)
@Html.TextBoxFor(m => m.AnotherProperty)
@Html.ValidationMessageFor(m => m.AnotherProperty)

....

查看

@model MasterVM
@using Html.BeginForm())
{
  if(Model.Model1 != null)
  {
    @Html.Partial("_Model1", Model.Model1, new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "Model1" }})
  }
  if(Model.Model2 != null)
  {
    @Html.Partial("_Model2", Model.Model2, new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "Model2" }})
  }
  ....
  <input type="submit" />
}

请注意 @ Html.Partial 的第三个参数。这将添加一个preFIX到控制名称,因此如果型号1 包含属性字符串名称,控制系统将作为生成&LT;输入名称=Model1.Name...&GT; 让你回发并绑定到 MasterVM

Note the 3rd parameter of @Html.Partial. This will add a prefix to the control names, so if Model1 contains property string Name, the control will be generated as <input name="Model1.Name" ...> allowing you to post back and bind to MasterVM

这篇关于当我发送包含不同型号的多个局部视图视图控制器应该得到什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆