1

I want to create a registering page with multiple data from user

e.g. 1 - I got a userViewModel with basic ID and Name and got a model for this View;

[Key]
public int CandidatoId { get; set; }

[Required(ErrorMessage = "Preencher o campo Nome")]
[MaxLength(150, ErrorMessage = "Máximo {1} caracteres")]
[MinLength(2, ErrorMessage = "Mínimo {1} caracteres")]
public string Nome { get; set; }


[Required(ErrorMessage = "Preencher o campo CPF")]
[MaxLength(15, ErrorMessage = "Máximo {1} caracteres")]
[MinLength(2, ErrorMessage = "Mínimo {1} caracteres")]
//Criar Datatype de CPF
public string CPF { get; set; }

2 - Got a Perssonal Data too:

public class DadosPessoaisViewModel
{
    [Key]
    public int CandidatoId { get; set; }

    public char Sexo { get; set; }

    [Required(ErrorMessage = "Preencher o campo Endereço")]
    [MaxLength(500, ErrorMessage = "Máximo {0} caracteres")]
    [MinLength(2, ErrorMessage = "Mínimo {0} caracteres")]
    public string Endereco { get; set; }

    public virtual Candidato Candidato { get; set; }
}

And I will have more about 5 or More data from user, like family info etc.

For the user interface I'm planning to make it inside a bootstrap carousel.

So I create a generic ViewModel for all data ( WithoutFisical model ) and one View model for each one kind of user Data:

//User ViewModel
public class DadosCandidatoViewModel
{
    [Key]
    public int CandidatoId { get; set; }

    //User ViewModel
    public virtual CandidatoViewModel Candidato { get; set; }
    //Pessoal Data Info ViewModel
    public virtual DadosPessoaisViewModel DadosPessoais { get; set; }

    //will Be more data from user here
}

//User Pessoal Data VewModel ( Sample )
public class DadosPessoaisViewModel
{
    [Key]
    public int CandidatoId { get; set; }

    public char Sexo { get; set; }

    [Required(ErrorMessage = "Preencher o campo Endereço")]
    [MaxLength(500, ErrorMessage = "Máximo {0} caracteres")]
    [MinLength(2, ErrorMessage = "Mínimo {0} caracteres")]
    public string Endereco { get; set; }

    public virtual Candidato Candidato { get; set; }
}

So I created an Edit View that will have all the user data separated by partial Views :

@model Gestao_RH.MVC.ViewModels.DadosCandidatoViewModel
@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <!-- Div na qual o "carousel" será aplicado. -->
    <div id="div-carousel" class="carousel slide">
        <div class="carousel-inner">
            <!-- Divs com efeito de transição. -->
            <div class="item active">

                @Html.Partial("~/Views/DadosPessoais/Edit.cshtml" ,  Neeed Passs DadosPessoaisViewModel Here???? )
            </div>
            <div class="item">
                Conteúdo da DIV 2.
            </div>
        </div>
    </div>
    <div class="row">
        <!-- Botões de navegação -->
        <div id="div-1" class="span2">
            <a id="a-1" class="btn" href="#div-carousel" data-slide="prev"><i class="icon-chevron-left"></i>Voltar para DIV 1</a>
        </div>
        <div id="div-2" class="span2">
            <a id="a-2" class="btn" href="#div-carousel" data-slide="next">Avançar para DIV 2<i class="icon-chevron-right"></i></a>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Now there's a problem...

All of my partial views are strongly typed like sample above:

@model Gestao_RH.MVC.ViewModels.DadosPessoaisViewModel

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>DadosPessoaisViewModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.CandidatoId)

        <div class="form-group">
            @Html.LabelFor(model => model.Sexo, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Sexo, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Sexo, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Endereco, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Endereco, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Endereco, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                @Html.ActionLink("Save", "Edit", "DadosPessoais")
                @*<input type="submit" value="Save" class="btn btn-default" />*@
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

The problem is this View expects a DadosPessoaisViewModel ( User Perssonal data) but I have only DadosCandidatoViewModel in this context to sent.

e.g for my Controller:

public class DadosCandidatoController : Controller
{
    private readonly ICandidatoAppService _candidato;

    public DadosCandidatoController(ICandidatoAppService candidato)
    {
        _candidato = candidato;
    }

    public ActionResult Edit(int id)
    {
        var cargo = _candidato.GetById(id);
        var DadosCandidatoViewModel = Mapper.Map<Candidato, DadosCandidatoViewModel>(cargo);

        return View(DadosCandidatoViewModel);
    }
}
ekad
  • 14,436
  • 26
  • 44
  • 46
Paulo Jardim
  • 91
  • 11

1 Answers1

0

If i understand your question correctly you have two view models that are dependant on each other but distinct partial views. The best way to solve this would be to have the dependant property be party of the overall page view model so both views can access the properties at the same time.

The question of how to access one property of the view model from multiple partial views can be found here

Community
  • 1
  • 1
theDarse
  • 749
  • 5
  • 13
  • Not Exactly... I need sent another model to partial view. Like : @Html.Partial("~/Views/DadosPessoais/Edit.cshtml" , DadosPessoaisViewModel ). – Paulo Jardim Feb 02 '15 at 20:07