Failed to create new data into the database using the popup form

Viewed 72

I was trying to create a popup system to create new data into the database. when I click"Create" in my index view then I find a popup form.but when I click "Save" for data save then it does not work anything. suddenly it works and saves the data into the database. but it happened just one time. but I don't understand why it's not working smoothly.

Here is my code:

Model

namespace Practise.Models
{
    public class Category
    {
        public int Id { get; set; }
        public string CategoryName { get; set; }
    }
}

Controller:

namespace Practise.Controllers
{
    public class CategoryController : Controller
    {
        private ApplicationDbContext _db;

        public CategoryController(ApplicationDbContext db) //input parameter
        {
            _db = db;
        }



        public IActionResult Index()
        {
            return View(_db.Category.ToList());
        }


        [HttpGet]

        public IActionResult Create()
        {

            Category ca = new Category();
            return PartialView("_CreatePartial", ca);
            //return View();
        }



        [HttpPost]
        public IActionResult Create(Category ca)
        {
            _db.Category.Add(ca);
            _db.SaveChanges();
            return PartialView("_CreatePartial", ca);

        }

    }
}

Index.cshtml

@model IEnumerable<Practise.Models.Category>

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

<div id="PlaceHolderHere"></div>


<button type="button" class="btn btn-primary" data-toggle="ajax-model" data-target="#addEmployee" data-url="@Url.Action("Create")">Create</button>
</br></br>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CategoryName)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.CategoryName)
                </td>
                <td>
                    <partial name="_ButtonPartial" model="@item.Id" />
                </td>
            </tr>
        }
    </tbody>
</table>
@section scripts{
    <script src="//cdn.jsdelivr.net/npm/alertifyjs@1.13.1/build/alertify.min.js"></script>

    <script type="text/javascript">
        $(function(){
            var save = '@TempData["save"]'

            if (save!= null) {
                alertify.success(save);
            }

        })

    </script>

}

_CreatePartial view:



@model Category

<div class="modal fade" id="#addEmployee">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="#addEmployeeLabel">Add Category</h4>
                <button type="button" class="close" data-dismiss="modal">
                    <span>x</span>
                </button>
            </div>
            <div class="modal-body">
                <form action="Create">
                    <div class="form-group">
                        <label asp-for="CategoryName"> </label>
                        <input asp-for="CategoryName" class="form-control" />
                        <span asp-validation-for="CategoryName" class="text-danger"></span>
                    </div>
                </form>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" data-save="modal">Save</button>
            </div>
        </div>
    </div>
</div>




JS(site.js)

$(function () {
    var PlaceHolderElement = $('#PlaceHolderHere');
    $('button[data-toggle="ajax-model"]').click(function (event) {
        var url = $(this).data('url');
        $.get(url).done(function (data) {
            PlaceHolderElement.html(data);
            PlaceHolderElement.find('.modal').modal('show');
        })
    })
    PlaceHolderElement.on('click', '[data-save="modal"]', function (event) {

        var form = $(this).parents('.modal').find('form'); 
          var actionUrl = form.attr('action');
          var sendData = form.serialize();
            $.post(actionUrl, sendData).done(function (data) {
            PlaceHolderElement.find('.modal').modal('hide');
        })
    })
})

My Output:

enter image description here

when I press the save button I want it to save the data to DB.but it does not work.

1 Answers

I have to change something like that for save data:

  public IActionResult Create()
        {

            //Category ca = new Category();
            //return PartialView("_CreatePartial", ca);
            return View();
        }



        [HttpPost]
        public IActionResult Create(Category ca)
        {
            _db.Category.Add(ca);
            _db.SaveChanges();
            return RedirectToAction("Index");

        }

Create View:

@model Practise.Models.Category
@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

    <div class="modal fade" id="#addEmployee">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="#addEmployeeLabel">Add Category</h4>
                    <button type="button" class="close" data-dismiss="modal">
                        <span>x</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form asp-action="Create" method="post">
                        <div class="form-group">
                            <label asp-for="CategoryName"> </label>
                            <input asp-for="CategoryName" class="form-control" />
                            <span asp-validation-for="CategoryName" class="text-danger"></span>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
                            @*<button type="button" class="btn btn-primary" data-save="modal">Save</button>*@
                            <input type="submit" class="btn btn-primary" value="Submit"/>
                        </div>
                    </form>
                </div>
               

            </div>
        </div>
    </div>



then successfully will save data.

Related