Use 2 models in a View, but separate them in the form submission

Asked

Viewed 100 times

1

I’m getting the following error:

'Addemployeeviewmodel' does not contain a Definition for 'Officeemployee' and no Extension method 'Officeemployee' Accepting a first argument of type 'Addemployeeviewmodel' could be found (are you Missing a using Directive or an Assembly Reference?)

Within the View, there is a button that opens a modal so that the user can register a Cargo that will popular a Select Box, that position he is registered individually, only on the same screen that is registered the employee.

So I wonder how I can separate the model from Cargo and Funcionario, since I use the same controller both for Cargo how much to Funcionario.

inserir a descrição da imagem aqui

This is my AddEmployeeViewModel:

public class AddEmployeeViewModel
{
    [Required(ErrorMessage = "Nome é um campo obrigatório")]
    [StringLength(100, ErrorMessage = "O {0} deve ter pelo menos {2} e no máximo {1} caracteres.", MinimumLength = 3)]
    [Display(Name = "Nome")]
    public String Name { get; set; }

    [RegularExpression(@"/^[0-9]{3}.?[0-9]{3}.?[0-9]{3}-?[0-9]{2}/", ErrorMessage = "CPF está em um formato inválido.")]
    public String Cpf { get; set; }

    [RegularExpression(@"^\([1-9]{2}\) (?:[2-8]|9[1-9])[0-9]{3}\-[0-9]{4}$", ErrorMessage = "Telefone está em um formato inválido.")]
    public String Telephone { get; set; }

    [Required(ErrorMessage = "Email é um campo obrigatório")]
    [RegularExpression(@"^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$", ErrorMessage = "Email está em um formato inválido.")]
    public String Email { get; set; }

    public long Office { get; set; }

    [Required(ErrorMessage = "Cor é um campo obrigatório")]
    public String Color { get; set; }

    public decimal Commission { get; set; }

    public IFormFile Pic { get; set; }

    public Nullable<DateTime> BirthDate { get; set; }

    [RegularExpression(@"^\d{5}-\d{3}$", ErrorMessage = "CEP está em um formato inválido.")]
    [Display(Name = "CEP")]
    public String CEP { get; set; }

    public String UF { get; set; }

    public String Neighborhood { get; set; }

    public String City { get; set; }

    public String Address { get; set; }

    public String Number { get; set; }

    public String Complement { get; set; }
}

public class Office
{
    public String OfficeEmployee { get; set; }
}

and that’s the part of View that’s making the mistake:

<div class="modal fade none-border" id="cadastra-cargo">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <h4 class="modal-title">Novo Cargo</h4>
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        </div>
        <div class="modal-body">
            <form asp-route-returnUrl="@ViewData["ReturnUrl"]" id="cadastraCargoFuncionario" asp-action="AddOffice" asp-controller="Employee" method="post">
                <div class="form-body">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group row">
                                <label class="control-label text-right col-md-3">Cargo: </label>
                                <div class="col-md-8">
                                    <input type="text" asp-for="OfficeEmployee" class="form-control nome">
                                    <span asp-validation-for="OfficeEmployee" class="text-danger"></span>
                                </div>
                            </div>
                        </div>
                    </div>
                 </div>
            </form>
        </div>
        <div class="modal-footer">
            <div class="col-md-12">
                <button type="button" class="btn btn-info float-r" data-dismiss="modal" onclick="$('#cadastraCargoFuncionario').submit();">Salvar</button>
                <button type="button" class="btn btn-default float-l" data-dismiss="modal">Cancelar</button>
            </div>
        </div>
    </div>
</div>

  • Dude, if your view model is AddEmployeeViewModel, he doesn’t really own the property OfficeEmployee. Try to put the asp-for to the Office.OfficeEmployee.

  • What do you mean by separating them in the upload? will have two Forms in the view or two ajax posts for different actions?

  • You have 2 Forms in the View, for two different Actions within the Function Controller. Example AddFuncionario and AddCargo.

  • Take a look at these links: https://www.codeproject.com/Articles/1108855/ways-to-Bind-Multiple-Models-on-a-View-in-MVC and https://www.c-sharpcorner.com/UploadFileff2f08/multiple-models-in-single-view-in-mvc/

No answers

Browser other questions tagged

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