简体   繁体   English

jquery 弹出窗口在 asp.net 核心 2.2 中不起作用

[英]jquery popup not working in asp.net core 2.2

I am trying to create a popup form to add some employees or categories.but when I click the "Create" button, it's not work anything.我正在尝试创建一个弹出表单来添加一些员工或类别。但是当我单击“创建”按钮时,它不起作用。

Here is my code:这是我的代码:

namespace EasyBay.Areas.Admin.Controllers
{
    [Area("Admin")]
    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 ActionResult Create()
        {
            Category ca = new Category();
            return PartialView("_CatPartial", ca);

        }



        [HttpPost]
        [ValidateAntiForgeryToken]  //using for security purpose

        public async Task<IActionResult> Create(Category category)   //input parameter
        {
            if (ModelState.IsValid)
            {
                _db.Category.Add(category);
                await _db.SaveChangesAsync();
                TempData["save"] = "Product Type Save Successfully";  
                return RedirectToAction(nameof(Index));
            }

            return View(category);
        }
    }

Index.cshtml索引.cshtml

@model IEnumerable<EasyBay.Models.Category>

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

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

@*<button type="button" class="btn" id="c-btn" data-url="@Url.Action("Create")">Open Contact Form</button>*@
<button type="button" class="btn btn-primary" data-toggle="ajax-model" data-target="#addEmployee"  data-url= "@Url.Action("Create")">Create</button>

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CategoryName)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>

PartialView部分视图

@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" data-dismiss="modal" class="btn btn-primary">Close</button>
                <button type="button" data-save="modal" class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
</div>

js(site.js) js(网站.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');



        })
    })
    
})

Output: Output: 在此处输入图像描述

在此处输入图像描述

here I also showed where I create PartialView.when I click the "Create" button does nothing.在这里,我还展示了我创建 PartialView 的位置。当我单击“创建”按钮时,什么也不做。 I don't understand where is wrong.我不明白哪里错了。 I am beginner, please help anyone.我是初学者,请帮助任何人。

$(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');



        })
    })
    
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM