Home Why my controller does not access the post method that should put data into database
Reply: 0

Why my controller does not access the post method that should put data into database

user3320
1#
user3320 Published in April 23, 2018, 9:43 am

I am using Entity Framework to connect to the database (management studio) and after I connect, I auto-generate the controller class via EF and the connection.

I can get the details and the list (index) data but when I try to post and save into the database, it does not access the public async Task<IActionResult> Create method and I try to modify the name of the method, I try to change the asp-action from the HTML file but it still accesses the method that gives me the view so it just refreshes

public IActionResult Create()
{
    return View();
}

// POST: Customers/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("IdCustomer, FirstName, LastName, Username, Password, Email, Phone")] Customers customers)
{
      if (ModelState.IsValid)
      {
          _context.Add(customers);
          await _context.SaveChangesAsync();
          return RedirectToAction(nameof(Index));
      }

      return View(customers);
}

and this is the HTML create.cshtml

@model Licenta.Entityes.Hotels

@{
    ViewData["Title"] = "Create";
}

<h2>Create</h2>

<h4>Hotels</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create" method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Stars" class="control-label">Stars</label><br />
                <input asp-for="Stars" class="form-control" />
                <span asp-validation-for="Stars" class="text-danger"></span>
                @Html.ValidationMessage("Stars")
            </div>
            <div class="form-group">
                <label asp-for="HotelName" class="control-label">Hotel name</label><br />
                <input asp-for="HotelName" class="form-control" />
                <span asp-validation-for="HotelName" class="text-danger"></span>
                @Html.ValidationMessage("HotelName")
            </div>
            <div class="form-group">
                <label asp-for="DescriptionTable" class="control-label">Description hotel</label><br />
                <input asp-for="DescriptionTable" class="form-control" />
                <span asp-validation-for="DescriptionTable" class="text-danger"></span>
                @Html.ValidationMessage("DescriptionTable")
            </div>
            <div class="form-group">
                <input type="submit" value="Addhotel" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

I know there are 2 create methods so that might be the problem but I try to change that and it did noting.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.485541 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO