Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda