[英]Should I write action methods of the partial view in the controller of the parent view?
[英]What the controller should receive when I am sending a view that contains multiple partial view with different models?
當我發送包含具有不同模型的多個部分視圖的視圖時,控制器應該接收什么。
我有一個呈現多個/動態局部視圖的視圖(這些視圖的選擇取決於用戶之前選擇的包),所有這些局部視圖都有不同的模型,我需要以一個大的“父”形式提交它們。 我主要關心的是控制器應該收到什么? 以及如何處理表單中的所有信息,因為我有從表單發送的多個可變模型(取決於用戶選擇的內容)。
這是我的父視圖,它呈現所有部分視圖,具體取決於用戶選擇的包(即父視圖可以呈現 1 到 7 個表單。它們都有不同的模型)。 IEnumerable 字符串包含一個字符串列表,我將用它來吐出帶有視圖和模型的字典,以便稍后在此視圖中呈現它們。
@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 個不同的模型,以便我可以使用用戶填充的數據。
這是我的控制器:
[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());
}
...
}
我主要關心的是控制器應該收到什么? 我嘗試了以下方法:
[HttpPost]
[SaveModelState]
public virtual RedirectToRouteResult Index(IEnumerable<ICustomModel> models)
但經過一些研究,這是不可能的,我也嘗試將所有可能的模型傳遞給控制器
[HttpPost]
[SaveModelState]
public virtual RedirectToRouteResult Index(Model1 model1, ..., ModelN modeln)
這也不起作用,我最近的嘗試是創建一個包含所有可能模型的主模型並將其傳遞給控制器,這是我的主模型
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
來處理這個問題。
使用包含要呈現的每個表單的模型的視圖模型
查看模型
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
的第三個參數。 這將為控件名稱添加前綴,因此如果Model1
包含屬性string Name
,控件將生成為<input name="Model1.Name" ...>
允許您回發並綁定到MasterVM
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.