MVC Email Building

I come from web forms and am still very new to MVC. I want to create a contact form that just sends me the contact information,

eg:

  • Firstname
  • Lastname
  • Email
  • Age
  • Company

I need to collect about a dozen different areas of information.

In web forms, it was easy to create an email body by simply calling TextBox.Text

What is the best way to create an email body, in addition to passing a long-ass parameter:

[HttpPost]
Public ActionResult Contact(string firstName, string lastName, string Email, int Age, string Company, ...)
{
    // ...
}

Thanks in advance.

+3
source share
4 answers
[HttpPost]
Public ActionResult Contact(EmailMessage message)
{
    // ...
}

and your Model object looked like this:

public class EmailMessage
{
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public string Email { get; set; }
     ....
}

you can automatically bind it to your action method if your form elements match the EmailMessage model

<% using (Html.BeginForm()) { %>
    First Name: <input type="text" id="FirstName" />
    Last Name: <input type="text" id="LastName" />
    Email <input type="text" id="Email" />
    ....
<% } %>

, [DisplayName] MVC.

public class EmailMessage
{
     [DisplayName("First Name")]
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public string Email { get; set; }
     ....
}

<% using (Html.BeginForm()) { %>
    <%: LabelFor(m => m.FirstName) %><%: EditorFor(m => m.FirstName) %>
    <%: LabelFor(m => m.LastName) %><%: EditorFor(m => m.LastName) %>
    <%: LabelFor(m => m.Email) %><%: EditorFor(m => m.Email) %>
<% } %>
+3

HTML-- . .

+1

(MVC 1) FormCollection , , .

ThatSteveGuy MVC 2.

0

Source: https://habr.com/ru/post/1759615/


All Articles