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 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.
public async Task<IActionResult> Create([Bind("IdCustomer, FirstName, LastName, Username, Password, Email, Phone")] Customers customers)
      if (ModelState.IsValid)
          await _context.SaveChangesAsync();
          return RedirectToAction(nameof(Index));

      return View(customers);

and this is the HTML create.cshtml

@model Licenta.Entityes.Hotels

    ViewData["Title"] = "Create";


<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>
            <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>
            <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>
            <div class="form-group">
                <input type="submit" value="Addhotel" class="btn btn-default" />

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

@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