我有这个案子:
[Serializable]
public class FormBuscarCorteReligacaoViewModel
{
public CorteReligacao CorteReligacao { get; set; }
}
[Serializable]
public class OrdensFinalizadasViewModel : FormBuscarCorteReligacaoViewModel
{
public Manutencao Manutencao { get; set; }
public DateTime? DataInicio { get; set; }
public DateTime? DataFim { get; set; }
public OrdensFinalizadasViewModel()
{
}
}
所以这里的孩子是OrdensFinalizadasViewModel
。我有这个:
@model ProjetoPath.OrdensFinalizadasViewModel
@using (Html.BeginForm("Buscar", "OrdensFinalizadas", FormMethod.Post, new { id = "FormBuscarOrdensFinalizadas" }))
{
@Html.Partial("~/Views/Shared/_FormBusca.cshtml", Model)
}
我的控制员:
[HttpPost]
public ActionResult Buscar(OrdensFinalizadasViewModel modelOrdensFinalizadas)
{
//DO STUFF
}
我想要的是能够访问CorteReligacao
FormBuscarCorteReligacaoViewModel
。但它只填充了OrdensFinalizadasViewModel
属性。
我的观点~/Views/Shared/_FormBusca.cshtml
:
@using Capgemini.Telemetria.STCortes.Domain.ViewModel.Common
@model Capgemini.Telemetria.STCortes.Domain.ViewModel.Common.FormBuscarCorteReligacaoViewModel
@{
var modelOrdensFinalizadas = Model as ProjectPath.OrdensFinalizadasViewModel;
}
<table class="tabela_filtro" style="border: 1px solid #ccc; margin-bottom: 5px; font-size: 11px" />
<thead>
[...]
</thead>
<tbody>
@if(modelOrdensFinalizadas != null)
{
<tr>
<td>
<label>
Data
</label>
</td>
<td>
@Html.TextBoxFor(model => modelOrdensFinalizadas.DataInicio, new { id = "dataInicio", @class = "campoData", @placeholder = "Data Inicio" })
</td>
[...]
</tr>
}
<tr>
<td>
<label class="label_filtro">
CP
</label>
</td>
<td>
@Html.EditorFor(model => model.CorteReligacao.CoreCp, new { style = "width: 20px;" })
<span class="validation-message">@Html.ValidationMessageFor(model => model.CorteReligacao.CoreCp)</span>
</td>
[...]
<input type="image" src="~/Content/img/icons/enviar.gif" value="Buscar" title="Buscar" />
</td>
</tr>
</tbody>
有可能吗?我在asp.net MVC上有点新鲜。 非常感谢任何帮助/消化。
此致
答案 0 :(得分:1)
在您的视图中,您告诉它您的模型是FormBuscarCorteReligacaoViewModel
类型。如果您将其更改为
@using Capgemini.Telemetria.STCortes.Domain.ViewModel.Common
@model Capgemini.Telemetria.STCortes.Domain.ViewModel.Common.OrdensFinalizadasViewModel
并删除此部分
@{
var modelOrdensFinalizadas = Model as ProjectPath.OrdensFinalizadasViewModel;
}
你应该可以使用
@Html.EditorFor(model => model.CorteReligacao.CoreCp, new { style = "width: 20px;" })
您需要将文本框更改为
@Html.TextBoxFor(model => model.DataInicio, new { id = "dataInicio", @class = "campoData", @placeholder = "Data Inicio" })
您是否尝试在模型中添加构造函数以初始化子属性?
[Serializable]
public class FormBuscarCorteReligacaoViewModel
{
public FormBuscarCorteReligacaoViewModel()
{
CorteReligacao = new CorteReligacao();
}
public CorteReligacao CorteReligacao { get; set; }
}
[Serializable]
public class OrdensFinalizadasViewModel : FormBuscarCorteReligacaoViewModel
{
public Manutencao Manutencao { get; set; }
public DateTime? DataInicio { get; set; }
public DateTime? DataFim { get; set; }
public OrdensFinalizadasViewModel()
: base()
{
Manutencao = new Manutencao();
}
}
答案 1 :(得分:0)
是的可能,我建议你做一些改变
第一: 从部分视图中删除
titleLabel
将此部分更改为您的视图并将其从部分删除,将modelOrdensFinalizadas重命名为Model,
@{
var modelOrdensFinalizadas = Model as ProjectPath.OrdensFinalizadasViewModel;
}
然后在您的局部视图中,只需将表格的其余部分放入
<table class="tabela_filtro" style="border: 1px solid #ccc; margin-bottom: 5px; font-size: 11px" />
<thead>
[...]
</thead>
<tbody>
@if(Model!= null)
{
<tr>
<td>
<label>
Data
</label>
</td>
<td>
@Html.TextBoxFor(model => Model.DataInicio, new { id = "dataInicio", @class = "campoData", @placeholder = "Data Inicio" })
</td>
[...]
</tr>
}
<tr>
//here your partial view
@Html.Partial("~/Views/Shared/_FormBusca.cshtml", Model)
</tr>
</tbody>
现在在您的控制器中,您将可以访问模型(OrdensFinalizadasViewModel和FormBuscarCorteReligacaoViewModel)
你在控制器中变为空,因为
<td>
<label class="label_filtro">
CP
</label>
</td>
<td>
@Html.EditorFor(model => model.CorteReligacao.CoreCp, new { style = "width: 20px;" })
<span class="validation-message">@Html.ValidationMessageFor(model => model.CorteReligacao.CoreCp)</span>
</td>
[...]
<input type="image" src="~/Content/img/icons/enviar.gif" value="Buscar" title="Buscar" />
</td>
不是部分视图中模型(FormBuscarCorteReligacaoViewModel)的一部分
希望这可以帮助你或者至少给你一个想法