如何在MVC中将两种不同的模型归为单视图 [英] How can accssed two different model into single view in MVC

查看:83
本文介绍了如何在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 {...})
...
}



(除非您参加特殊步骤 [ ^ ],部分视图中的 EditorFor 扩展名不包含父视图模型中的前缀。)


@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 the EditorFor fields are prefixed with the name of the property from Bigviewmodel:

<input name="Event.Event_title" ... />


This would work if your POST action expected a Bigviewmodel parameter. But since it expects an Event parameter, the model binder is looking for field names that match the properties on the Event class, optionally prefixed with the parameter name. So either Event_title or value.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 your Bigviewmodel. (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 the event 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[^], the EditorFor extensions in a partial view don't include the prefix from the parent viewmodel.)


这篇关于如何在MVC中将两种不同的模型归为单视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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