如何在MVC中将两种不同的模型归为单视图 [英] How can accssed two different model into single view in MVC
问题描述
@model layout_wise_minicrm.Models.Bigviewmodel
1st model(Task_info)
@using(Html.BeginForm(Task, crm,FormMethod.Post))
{
@ ** @
@ Html.EditorFor(model => model。 Taskinfo.Task_name,new {htmlAttributes = new {@class =form-control,placeholder =Task Name,required =required}})
}
第二个模特(活动)
@using(Html.BeginForm(Event, crm,FormMethod.Post))
{
@ Html.EditorFor(model => model.Event.Event_title,ne w {htmlAttributes = new {@class =form-control,placeholder =Event Title,required =required}})
}
我的尝试:
public class Bigviewmodel
{
公共活动事件{get;组; }
public Taskinfo Taskinfo {get;组; }
}
但是,在插入验证实体错误出现时。怎么解决呢
我假设你的POST动作类似于:
public ActionResult事件(事件值)...
public ActionResult Task(Taskinfo value )...
问题是EditorFor
字段的名称前缀为Bigviewmodel
中的属性名称:
< 输入 name = Event.Event_title ... < span class =code-keyword> / >
如果您的POST操作需要Bigviewmodel
参数,这将有效。但由于它需要Event
参数,因此模型绑定器正在查找与Event
类上的属性匹配的字段名称,可选地以参数名称为前缀。所以Event_title
或value.Event_title
。
有几种方法可以解决这个问题。
最简单的选择是更改方法上的参数名称以匹配上的属性名称Bigviewmodel
。 (它不区分大小写,因此您仍然可以使用带有Pascal外壳属性的驼峰参数名称。)
public ActionResult Event(Event @event)...
public ActionResult Task(Taskinfo taskInfo)...
注意:事件需要
参数名称,因为那是一个C#关键字。@
前缀
或者,您可以使用绑定前缀:
< pre lang =C#> public ActionResult事件([Bind(Prefix = 事件)]事件值)...
public ActionResult任务([Bind(Prefix = Taskinfo)] Taskinfo 价值)...
或者,你可以将您的两个表单分成部分视图:
@model layout_wise_minicrm.Models.Bigviewmodel
@ Html.Partial(Task_info,Model.Taskinfo)
@ Html.Partial(Event,Model.Event)
Task_info.cshtml :@model layout_wise_minicrm.Models .Taskinfo
@using(Html.BeginForm(Task,crm,FormMethod.Post))
{
...
@ Html.EditorFor (model => model.Task_name,new {...})
...
}
Event.cshtml :@model layout_wise_minicrm.Models.Event
@using(Html.BeginForm(Event,crm,FormMethod.Post))
{
。 ..
@ Html.EditorFor(model => model.Event_title,new {...})
...
}
@model layout_wise_minicrm.Models.Bigviewmodel
1st model(Task_info)
@using (Html.BeginForm("Task", "crm", FormMethod.Post))
{
@**@
@Html.EditorFor(model => model.Taskinfo.Task_name, new { htmlAttributes = new { @class = "form-control", placeholder = "Task Name", required = "required" } })
}
2nd model(Event)
@using (Html.BeginForm("Event", "crm", FormMethod.Post))
{
@Html.EditorFor(model => model.Event.Event_title, new { htmlAttributes = new { @class = "form-control", placeholder = "Event Title", required = "required" } })
}
What I have tried:
public class Bigviewmodel
{
public Event Event{ get; set; }
public Taskinfo Taskinfo { get; set; }
}
BUT, at the time of insertion validation entity error arrised. how can solve it
I'm assuming your POST actions looks something like:
public ActionResult Event(Event value) ... public ActionResult Task(Taskinfo value) ...
The problem is that the names for theEditorFor
fields are prefixed with the name of the property fromBigviewmodel
:
<input name="Event.Event_title" ... />
This would work if your POST action expected aBigviewmodel
parameter. But since it expects anEvent
parameter, the model binder is looking for field names that match the properties on theEvent
class, optionally prefixed with the parameter name. So eitherEvent_title
orvalue.Event_title
.
There are several ways to work around this.
The simplest option is to change the parameter names on your methods to match the property names on yourBigviewmodel
. (It's not case-sensitive, so you can still use camel-cased parameter names with Pascal cased properties.)
public ActionResult Event(Event @event) ... public ActionResult Task(Taskinfo taskInfo) ...
NB: You need the@
prefix on theevent
parameter name, since that's a C# keyword.
Alternatively, you can use a binding prefix:
public ActionResult Event([Bind(Prefix = "Event")] Event value) ... public ActionResult Task([Bind(Prefix = "Taskinfo")] Taskinfo value) ...
Or, you can move your two forms into partial views:
@model layout_wise_minicrm.Models.Bigviewmodel @Html.Partial("Task_info", Model.Taskinfo) @Html.Partial("Event", Model.Event)
Task_info.cshtml:@model layout_wise_minicrm.Models.Taskinfo @using (Html.BeginForm("Task", "crm", FormMethod.Post)) { ... @Html.EditorFor(model => model.Task_name, new { ... }) ... }
Event.cshtml:@model layout_wise_minicrm.Models.Event @using (Html.BeginForm("Event", "crm", FormMethod.Post)) { ... @Html.EditorFor(model => model.Event_title, new { ... }) ... }
(Unless you take special steps[^], theEditorFor
extensions in a partial view don't include the prefix from the parent viewmodel.)
这篇关于如何在MVC中将两种不同的模型归为单视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!