Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

99
Visualizações
How to combine two html form input tag's value and store it into model object of C#

This is my html form

@model Demo.Models.ContactU

<form asp-action="Create">
    <div class="col-md-6 col-12 bottom-margin-5">
        <input type="text" class="form-control" placeholder="First name" aria-label="First name">
    </div>
    <div class="col-md-6 col-12 bottom-margin-5">
        <input type="text" class="form-control" placeholder="Last name" aria-label="Last name">
    </div>
</form>

And this is my model

public partial class ContactU
{
    public string Name { get; set; }
}

I want to Combine First name and Last name into whole name and store it into my database

Controller method

[HttpPost]
public IActionResult Contact(ContactU contactU)
{
    _dbContext.ContactUs.Add(contactU);
    return RedirectToAction("Index");
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You have to add name attributes to your inputs: name="firstName" and name="lastName". Than you should add FirstName and LastName properties to your model. And if you want to access those as a single property, you can use get only property:

public class Contact
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string FullName => $"{FirstName} {LastName}";
}

What you want to do(map two inputs into single property) is also possible, but you will have to write custom ModelBinder, but it will be more complex and not sure if you want to go into that direction.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda