Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!