Begin form doesn't submit inherited viewmodel ASP.NET MVC4

I have this case:

[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()
    {
    }
}

so my child here is OrdensFinalizadasViewModel. I have this:

@model ProjetoPath.OrdensFinalizadasViewModel

@using (Html.BeginForm("Buscar", "OrdensFinalizadas", FormMethod.Post, new { id = "FormBuscarOrdensFinalizadas" }))
{
    @Html.Partial("~/Views/Shared/_FormBusca.cshtml", Model)
}

And my controller:

    [HttpPost]
    public ActionResult Buscar(OrdensFinalizadasViewModel modelOrdensFinalizadas)
    {
        //DO STUFF
    }

what I want is to have access to CorteReligacao of FormBuscarCorteReligacaoViewModel. But its only filled with OrdensFinalizadasViewModel properties.

My view ~/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>

Is it possible to do that? I'm kinda new on asp.net MVC. Would appreciate any help / sugestion.

Regards

Answers


in your view you are telling it that your model is type FormBuscarCorteReligacaoViewModel. If you change that to

@using Capgemini.Telemetria.STCortes.Domain.ViewModel.Common
@model Capgemini.Telemetria.STCortes.Domain.ViewModel.Common.OrdensFinalizadasViewModel 

and delete this section

@{
    var modelOrdensFinalizadas = Model as ProjectPath.OrdensFinalizadasViewModel;
}

you should be able to use

@Html.EditorFor(model => model.CorteReligacao.CoreCp, new { style = "width: 20px;" })

you need to change your textboxfor to

@Html.TextBoxFor(model => model.DataInicio, new { id = "dataInicio", @class = "campoData", @placeholder = "Data Inicio" })

have you tried adding constructors to your models to initialize the child properties?

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

yes is possible, just I recommend you a few changes

first: delete from your partial view

         @{
         var modelOrdensFinalizadas = Model as          ProjectPath.OrdensFinalizadasViewModel;
       }

changes this from your partial to your view and delete it from partial, rename modelOrdensFinalizadas to Model,

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

and then in your partial view just put the resto of the table that you need

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

and now in you controller you will have access to the model(OrdensFinalizadasViewModel and FormBuscarCorteReligacaoViewModel)

you are getting null in you controller because

   @Html.TextBoxFor(model => modelOrdensFinalizadas.DataInicio

is not part of the model (FormBuscarCorteReligacaoViewModel) in the partial view

hope this can help you or at least give you an idea


Need Your Help

what is the use of timezone in datetimepicker

javascript php jquery mysql datetime

I am using Datetimepicker for date and time input. But i am not able to understand what is the use of timezone option in it. I mean when i submit the input to datetime datatype in mysql and then se...

How to make a synchronous GET request with volley library? And how to parse it afterwards?

android get synchronous android-volley

I've seen answer to both of these questions, however, when I tried to put them together, I couldn't make it work. The problem itself is pretty simple: I want to get a string from one site and use i...