Viewmodel with INSERT in two controllers

Asked

Viewed 68 times

1

I own two Controllers:

1 - Register the CPF (CPF, Name)

2 - CPF Phone Registration (CPF, DDD, Telephone)

Viewmodel:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;

namespace WebAutenticado.ViewModel
{
    public class NovoDevedorViewModel
    {
        [Key]
        [StringLength(14)]
        public string CPF_DEV { get; set; }

        [StringLength(100)]
        public string NOME_DEV { get; set; }

        [StringLength(20)]
        public string CONTRATO_FIN { get; set; }

        public byte COD_CLI { get; set; }

        public byte COD_CAR { get; set; }

        public int? COD_BOR { get; set; }

        [StringLength(10)]
        public string TIPO_FIN { get; set; }

        public double? VALOR_FIN { get; set; }

        [Required]
        [StringLength(1)]
        public string MOEDA_FIN { get; set; }

        public int COD_TEL { get; set; }

        [Required]
        [StringLength(5)]
        public string DDD_TEL { get; set; }

        [StringLength(10)]
        public string TEL_TEL { get; set; }

        public byte COD_TIPO { get; set; }
    }
}

The Control of the Person (CPF), works the guy inserts in the base, but the click on the REGISTER it only passes in the Create of the first control (Person).

I need to make when entering the Person (CPF) in the base after it goes to the Telephone Control and register the Phone with the CPF registered in Control Person.

View by Viewmodel:

@model WebAutenticado.ViewModel.NovoDevedorViewModel

@{
    ViewBag.Title = "Enviar Devedores";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Novo Cliente</h2>


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

    <div class="form-horizontal">
        <h4>Cadastro de novo devedor</h4>
        <hr />
        <blockquote>Dados do Devedor</blockquote>

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

        <div class="form-group">
            @Html.LabelFor(model => model.NOME_DEV, "Razao Social", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.NOME_DEV, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NOME_DEV, "", new { @class = "text-danger" })
            </div>
        </div>
        <hr />
        <blockquote>Telefones de Contato</blockquote>
        <div class="form-group">
            @Html.LabelFor(model => model.COD_TEL, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.COD_TEL, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.COD_TEL, "", new { @class = "text-danger" })
            </div>
        </div>

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

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

            <div class="form-group">
                @Html.LabelFor(model => model.COD_TIPO, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.COD_TIPO, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.COD_TIPO, "", new { @class = "text-danger" })
                </div>
            </div>
        <blockquote>Endereços de Contato</blockquote>
        <hr />
        <blockquote>Dados do Contrato</blockquote>
        <hr />
        <blockquote>Dados de Título</blockquote>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Próximo" class="btn btn-warning" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Retornar", "Index", null, new { @class = "btn btn-xl btn-success" })
</div>

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

Registry Controller (CPF):

    public ActionResult Create([Bind(Include = "CPF_DEV,NOME_DEV,NASC_DEV,RG_DEV,EXP_DEV,ORG_DEV,MAE_DEV,PAI_DEV,CONJNOME_DEV,CONJCPF_DEV,PROFISSAO_DEV,RENDA_DEV,COD_SEXO,COD_ESTCIV,COD_TIPESS,NAC_DEV,EMAIL_DEV,RESID_DEV,OCUPA_DEV,TEMPORESID_DEV,TPDOC_DEV,NATURA_DEV,COD_UF,CPF_AUX,EMPRESA_DEV,DDD_DEV,DTNASC_DEV,VIRCOBWEBENVIO_DEV,ESCOLARIDADE_DEV,DTSALARIO_DEV,DTENVIODEBT_DEV,VIRCOBWEBSENHA_DEV")] CAD_DEV cAD_DEV)
    {
        if (ModelState.IsValid)
        {
            db.CAD_DEV.Add(cAD_DEV);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(cAD_DEV);
    }

Telephone Control:

        public ActionResult Create([Bind(Include = "CPF_DEV,COD_TEL,DDD_TEL,TEL_TEL,PERC_TEL,COD_TIPO,OBS_TEL,STATUS_TEL,COD_CLI,COD_REF,CLASSIFICACAO,COD_TIPO_DISCADOR,PRIORIDADE_TEL,EFICAZ_TEL,COD_HORARIOLIGACAO,COD_ORIGEM,RECUP_CONFIRM,RECUP_NEGATIV,DTNEGATIV_TEL,DTCONFIRM_TEL,OPERADORA,COD_OPERADORA,BLOQUEIO_TEL,OBSIMP_TEL,ORDEMPRIORIDADE_TEL,APTOSMS_TEL,POSSUIWHATSAPP_TEL,CELULARSN_TEL,DTINCLUSAO_TEL,SCORE_TEL")] CAD_DEVT cAD_DEVT)
        {
            if (ModelState.IsValid)
            {
                db.CAD_DEVT.Add(cAD_DEVT);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(cAD_DEVT);
        }

Model of the Telephone:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;

namespace WebAutenticado.Models
{


    public partial class CAD_DEVT
    {
        [Key]
        [Column(Order = 0)]
        [StringLength(14)]
        public string CPF_DEV { get; set; }

        [Key]
        [Column(Order = 1)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int COD_TEL { get; set; }

        [Required]
        [StringLength(5)]
        public string DDD_TEL { get; set; }

        [StringLength(10)]
        public string TEL_TEL { get; set; }

        public int PERC_TEL { get; set; }

        public byte COD_TIPO { get; set; }

        [StringLength(100)]
        public string OBS_TEL { get; set; }

        public int? STATUS_TEL { get; set; }

        public int? COD_CLI { get; set; }

        public int? COD_REF { get; set; }

        public int? CLASSIFICACAO { get; set; }

        [StringLength(6)]
        public string COD_TIPO_DISCADOR { get; set; }

        public int? PRIORIDADE_TEL { get; set; }

        public int? EFICAZ_TEL { get; set; }

        public int? COD_HORARIOLIGACAO { get; set; }

        public int? COD_ORIGEM { get; set; }

        public int? RECUP_CONFIRM { get; set; }

        public int? RECUP_NEGATIV { get; set; }

        public DateTime? DTNEGATIV_TEL { get; set; }

        public DateTime? DTCONFIRM_TEL { get; set; }

        [StringLength(60)]
        public string OPERADORA { get; set; }

        public int? COD_OPERADORA { get; set; }

        public byte? BLOQUEIO_TEL { get; set; }

        [StringLength(1000)]
        public string OBSIMP_TEL { get; set; }

        public int? ORDEMPRIORIDADE_TEL { get; set; }

        [StringLength(3)]
        public string APTOSMS_TEL { get; set; }

        public byte? POSSUIWHATSAPP_TEL { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public int? CELULARSN_TEL { get; set; }

        public DateTime? DTINCLUSAO_TEL { get; set; }

        public byte? SCORE_TEL { get; set; }
    }
}

Mappers:

using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebAutenticado.Models;
using WebAutenticado.ViewModel;

namespace WebAutenticado.Mappers
{
    public class DomainToViewModelMappingProfile : Profile
    {
        // Não realizar este override na versão 4.x e superiores
        public override string ProfileName
        {
            get { return "DomainToViewModelMappings"; }
        }

        protected override void Configure()
        {
            Mapper.CreateMap<CAD_DEV, NovoDevedorViewModel>();
            Mapper.CreateMap<CAD_DEVF, NovoDevedorViewModel>();
            Mapper.CreateMap<CAD_DEVE, NovoDevedorViewModel>();
            Mapper.CreateMap<CAD_DEVT, NovoDevedorViewModel>();

        }
    }
}

using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebAutenticado.ViewModel;
using WebAutenticado.Models;

namespace WebAutenticado.Mappers
{
    public class ViewModelToDomainMappingProfile : Profile
    {
        // Não realizar este override na versão 4.x e superiores
        public override string ProfileName
        {
            get { return "ViewModelToDomainMappings"; }
        }

        protected override void Configure()
        {
            Mapper.CreateMap<NovoDevedorViewModel, CAD_DEV>();
            Mapper.CreateMap<NovoDevedorViewModel, CAD_DEVF>();
            Mapper.CreateMap<NovoDevedorViewModel, CAD_DEVT>();
            Mapper.CreateMap<NovoDevedorViewModel, CAD_DEVE>();
        }
    }
}

I thought you’d be like this:

        public ActionResult Create([Bind(Include = "CPF_DEV,NOME_DEV,NASC_DEV,RG_DEV,EXP_DEV,ORG_DEV,MAE_DEV,PAI_DEV,CONJNOME_DEV,CONJCPF_DEV,PROFISSAO_DEV,RENDA_DEV,COD_SEXO,COD_ESTCIV,COD_TIPESS,NAC_DEV,EMAIL_DEV,RESID_DEV,OCUPA_DEV,TEMPORESID_DEV,TPDOC_DEV,NATURA_DEV,COD_UF,CPF_AUX,EMPRESA_DEV,DDD_DEV,DTNASC_DEV,VIRCOBWEBENVIO_DEV,ESCOLARIDADE_DEV,DTSALARIO_DEV,DTENVIODEBT_DEV,VIRCOBWEBSENHA_DEV")] CAD_DEV cAD_DEV)
        {
            if (ModelState.IsValid)
            {
                db.CAD_DEV.Add(cAD_DEV);
                db.SaveChanges();
                return RedirectToAction("Create", "CadastroDevedorTelefone", new { id = cAD_DEV.CPF_DEV });
            }

            return View(cAD_DEV);
        }
  • please add phone model source code

  • Updated, need something else?

2 answers

0

Dude, I would say that what you want to do would be unnecessary. You can use the same controller to add the two records smoothly.

Example:

public ActionResult Create([Bind(Include = "CPF_DEV,NOME_DEV,NASC_DEV,RG_DEV,EXP_DEV,ORG_DEV,MAE_DEV,PAI_DEV,CONJNOME_DEV,CONJCPF_DEV,PROFISSAO_DEV,RENDA_DEV,COD_SEXO,COD_ESTCIV,COD_TIPESS,NAC_DEV,EMAIL_DEV,RESID_DEV,OCUPA_DEV,TEMPORESID_DEV,TPDOC_DEV,NATURA_DEV,COD_UF,CPF_AUX,EMPRESA_DEV,DDD_DEV,DTNASC_DEV,VIRCOBWEBENVIO_DEV,ESCOLARIDADE_DEV,DTSALARIO_DEV,DTENVIODEBT_DEV,VIRCOBWEBSENHA_DEV")] CAD_DEV cAD_DEV)
        {
            if (ModelState.IsValid)
            {
                db.CAD_DEV.Add(cAD_DEV);
                db.OUTRODBSET.Add(OutroObjeto);
                db.SaveChanges();
                return RedirectToAction("Create", "CadastroDevedorTelefone", new { id = cAD_DEV.CPF_DEV });
            }

            return View(cAD_DEV);
        }

If you need to go to the other controller, you can use it as follows:

return RedirectToAction("Action", "Controller", new { model = ModelQueVoceQuerPassar });

remembering that the "model" has to have the same name as your action parameter that you will access.

  • You’re right, I turned around.

  • @Marcosrebitte if you can evaluate my answer, I would be grateful :D

  • Done, thank you!

0

The solution found was the following:

public ActionResult Create(NovoDevedorViewModel devedorViewModel)
        {
            if (ModelState.IsValid)
            {
                var cadDevViewModel = Mapper.Map<CAD_DEV>(devedorViewModel);
                var cadDevTViewModel = Mapper.Map<CAD_DEVT>(devedorViewModel);
                var cadDevEViewModel = Mapper.Map<CAD_DEVE>(devedorViewModel);
                var cadDevFViewModel = Mapper.Map<CAD_DEVF>(devedorViewModel);

                db.CAD_DEV.Add(cadDevViewModel);
                db.CAD_DEVT.Add(cadDevTViewModel);
                db.CAD_DEVE.Add(cadDevEViewModel);
                db.CAD_DEVF.Add(cadDevFViewModel);

                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(devedorViewModel);
        }

  • credit to Fabricio Oliveira (@gnomoskt).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.