0

I have this code in my html.And i want to post this information from form to controler and save in data base.

<form method="POST" class="contactme form-group">
     <input type="text" placeholder="Name" class="form-control inputcontact">
     <input type="text" placeholder="Surename" class="form-control inputcontact">
     <input type="email" placeholder="E-mail" class="form-control inputcontact">
     <input type="tel" pattern="[0-9]{5,10}" class="form-control inputcontact" placeholder="tel. number"><br>
     <input type="submit" class="btn btn-default buttonsend" value="Оставить заявку">
</form>

And i have this model:

 public int Id { get; }
 public string Firstname { get; set; }
 public string Lastname { get; set; }
 public string Email { get; set; }
 public string Phonenumber { get; set; }
hasan
  • 3,484
  • 1
  • 16
  • 23
M Yolo
  • 3
  • 3

2 Answers2

0

You have to tell it which Controller and which Action in the controller to look for like below:

using (Html.BeginForm("YourActionMethodHere", "YourControllerHere", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
       <input type="text" placeholder="Name" class="form-control inputcontact">
        <input type="text" placeholder="Surename" class="form-control inputcontact">
        <input type="email" placeholder="E-mail" class="form-control inputcontact">
        <input type="tel" pattern="[0-9]{5,10}" class="form-control inputcontact" placeholder="tel. number"><br>
        <input type="submit" class="btn btn-default buttonsend" value="Оставить заявку">
    }

This link should also help

jamiedanq
  • 967
  • 7
  • 12
  • And what code i should write in controllder? to get information from input's and save in database? – M Yolo Jun 12 '17 at 14:37
0

The MVC concept is quite straight forward for achieving what you need. It's usual to have a model that contains properties for the data that you need to display a "View" and also for the content entered by the user so the data can be bound to the model when posted. If you've not got any controller code yet then I would recommend the below approach.

First, write a controller to handle the displaying of your view and the posting of the views form content.

public class YourController : Controller
{
     [HttpGet]
     public ActionResult YourViewName()
     {
        var myViewModel = new YourViewModel();
        //Populate model data from services etc...

        return View("YourViewName", myViewModel);
     }
}

I find it easier to wrap my views data objects that it needs in a class.

public class YourViewModel
{
     public string Property1 { get; set; }
     public int Property2 { get; set; }
     \\etc...
}

Then in your view, wrap your controls in a Form and use Html helper controls to display and bind to the data in the model.

 @using (Html.BeginForm("ActionName", "YourController ", FormMethod.Post, new { id = "FormName"}))
 {
    @Html.TextBoxFor(x => x.YourModel.Property1, null, new { @class = "SomeCssClass"})
    \\Repeat for all properties that need displaying or for user input.
 }

You will also need a submit button on the form to post the form to your specified controller.

<button id="btnSubmitForm" type="Submit" class="SomeCssClass">Submit</button>

Then in your controller you create a method to receive the posted form (Model)

 [HttpPost]
 public ActionResult ActionName(YourViewModel postedContent)
 {

    //Handle saving etc.. here.
    var x = postContent.Property1;
    //Do something with data

    //Re populate model and show updated view.
    var myViewModel = new YourViewModel();

    return View("YourViewName", myViewModel);
 }

Thant should help you on your way. A lot of this is down to preference and opinions though.

Wheels73
  • 2,850
  • 1
  • 11
  • 20