简体   繁体   English

asp.net core 如何从表行发布类别和子类别

[英]asp.net core how can i post category and subcategory from table row

Here is my Category.cs,这是我的 Category.cs,

public class Category
{
    public int Id { get; set; }
    [Required, StringLength(100),Display(Name="Category Name")]
    public string Name { get; set; }
    [Display(Name="Category Description")]
    public string Description { get; set; }
    public bool IsActive { get; set; }
    public IList<Style> Style { get; set; }
    public IList<SubCategory> SubCategories { get; set; }
}
public class SubCategory
{
    public int Id { get; set; }
    public string Value { get; set; }
}
public class IndexModel : PageModel
{
    [Microsoft.AspNetCore.Mvc.BindProperty]
    public Category ViewModel { get; set; }
    public void OnGet()
    {
        // You can fill your ViewModel property here.
    }
    public void OnPost()
    {
        // You can read your posted ViewModel property here.
    }
}

And DbContext.cs和 DbContext.cs

public DbSet<Category> Categories { get; set; }
public DbSet<SubCategory> SubCategory { get; set; }

And AddCategory.cshtml和 AddCategory.cshtml

@model UretimPerformans.Entity.Admin.IndexModel
@{
ViewData["Title"] = Localizer["addca"];
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}

@section Scripts{
    <link href="~/dist/plugins/bootstrap-switch/css/bootstrap3/bootstrap-switch.min.css" rel="stylesheet" />
    <script src="~/js/jquery-3.5.1.js"></script>
    <style>
        td, tr {
            text-align: center;
            vertical-align: middle;
        }
    </style>
}

    @section Scripts2{
    <script src="~/dist/plugins/bootstrap-switch/js/bootstrap-switch.js"></script>

    <script>
        $("#btnSearch").click(function () {
            var t = null;
            t = $("#tab_logic tbody tr").get().map(function (tr) {
                return $(tr).find("input").get().map(function (input) {
                    return input.value
                })
            })

            $("#regTitle").html(t);
        });
    </script>

    <script>
        $(document).ready(function () {
            var i = 1;
            $("#add_row").click(function () {
                $('#addr' + i).html("<td>" + (i + 1) + "</td><td><input name='name" + i + "' required type='text' placeholder='SubCategory-" + i + "' class='form-control input-md'  /> </td><td><input  name='mail" + i + "' type='checkbox' placeholder='IsActive'  class='form-control'></td>");

                $('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
                i++;
            });
            $("#delete_row").click(function () {
                if (i > 1) {
                    $("#addr" + (i - 1)).html('');
                    i--;
                }
            });
        });


        $(function () {

            $("input[data-bootstrap-switch]").each(function () {
                $(this).bootstrapSwitch('state', $(this).prop('checked'));
            });

        })
    </script>
}
<section class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1>@Localizer["addca"]</h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a asp-action="Index" asp-controller="Home">@Localizer["anasayfa"]</a></li>
                    <li class="breadcrumb-item"><a asp-action="Index" asp-controller="Catalog">@Localizer["cat"]</a></li>
                    <li class="breadcrumb-item"><a asp-action="Categories" asp-controller="Catalog">@Localizer["ca"]</a></li>
                    <li class="breadcrumb-item active">@Localizer["addca"]</li>
                </ol>
            </div>
        </div>
    </div>
</section>
<section class="content">
    <div class="card">
        <div class="card-body">
            <form asp-action="AddCategories" asp-controller="Catalog" method="post">
                <div class="form-group row">
                    <span class="col-md-3">@Localizer["name"]</span>
                    <div class="col-md-9">
                        <input type="text" class="form-control" asp-for="ViewModel.Name" required />
                    </div>
                </div>
                <div class="form-group row">
                    <span class="col-md-3">@Localizer["desc"]</span>
                    <div class="col-md-9">
                        <textarea type="text" id="regTitle" class="form-control" asp-for="ViewModel.Description" required></textarea>
                    </div>
                </div>
                <div class="form-group row">
                    <span class="col-md-3">@Localizer["aktif"]</span>
                    <div class="col-md-9">
                        <input type="checkbox" asp-for="ViewModel.IsActive" checked data-bootstrap-switch data-off-color="danger" data-on-color="success">
                    </div>
                </div>
                <fieldset class="module">
                    <h4>@Localizer["alt"]</h4>

                    <button id="btnSearch">Click</button>
                    <a id="add_row" class="btn btn-default pull-left">Add Row</a>
                    <a id='delete_row' class="pull-right btn btn-default">Delete Row</a>
                    <hr />
                    <div class="row clearfix">
                        <div class="col-md-12 column">
                            <table class="table table-bordered table-hover" id="tab_logic">
                                <thead>
                                    <tr>
                                        <th class="text-center">
                                            #
                                        </th>
                                        <th class="text-center">
                                            SubCategoryName
                                        </th>
                                        <th class="text-center">
                                            IsActive
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>                                    
                                        <tr id='addr0'>
                                            <td>1</td>                                            
                                            <td>
                                                <input asp-for="ViewModel.SubCategories[1].Value" />
                                                @Model.ViewModel.SubCategories[1].Value
                                            </td>
                                        </tr>
                                    
                                    <tr id='addr0'>
                                        <td>
                                            1
                                        </td>
                                        <td>
                                            <input type="text" name='name0' placeholder='Name' class="form-control" />
                                        </td>
                                        <td>
                                            <input type="checkbox" name='mail0' placeholder='Mail' class="form-control" />
                                        </td>
                                    </tr>
                                    <tr id='addr1'></tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </fieldset>
                <div class="card-footer">
                    <button type="submit" class="btn btn-primary">@Localizer["kaydet"]</button>
                </div>
            </form>
        </div>
    </div>
</section>

And CategoryController,和类别控制器,

        [HttpGet]
        public IActionResult AddCategories()
        {
            return View();
        }
        [HttpPost]
        public IActionResult AddCategories(Category entity)
        {
            if (ModelState.IsValid)
            {
                categoryRepository.AddCategory(entity);
                TempData["success"] = "Başarıyla kaydedildi.";
                return RedirectToAction("Categories");
            }
            else
            {
                return View(entity);
            }
        }

My codes are as above.我的代码如上。 What I want to do is add subcategories from the table at the bottom of the category when adding a category.我想要做的是在添加类别时从类别底部的表格中添加子类别。 but I could not set up the model.但我无法设置模型。 and I could not write the post method and reading method in indexmodel.而且我无法在indexmodel中编写post方法和reading方法。 Please can you help me?请你能帮帮我吗?

Thank you Kind Regards谢谢你的问候

I don't understand the meaning of name0 and name1..., neither Category nor SubCategory have this attribute.我不明白 name0 和 name1... 的含义,Category 和 SubCategory 都没有这个属性。 In addition, if you add SubCategoryName by adding a row in the table, this SubCategory class does not have the attribute 'IsActive'.此外,如果您通过在表中添加一行来添加 SubCategoryName,则此 SubCategory 类没有属性“IsActive”。 If you only want to add SubCategories through the rows of the table, you can follow this example.如果你只想通过表格的行添加子类别,你可以按照这个例子。 Here I simulate some data.这里我模拟了一些数据。

public class IndexModel : PageModel
{
    [Microsoft.AspNetCore.Mvc.BindProperty]
    public Category ViewModel { get; set; }
    public void OnGet()
    {
        ViewModel = new Category
        {
            Id = 1,
            Description = "desc",
            IsActive = false,
             Name="name",
              SubCategories=new List<SubCategory>
              {
                  new SubCategory
                  {
                       Id=10,Value="val"
                  },
                  new SubCategory
                  {
                       Id=20,Value="val2"
                  },
                  new SubCategory
                  {
                       Id=30,Value="val3"
                  },
              }
        };
        // You can fill your ViewModel property here.
    }
}

I find discorrect asp-controller here.我在这里发现不正确的 asp 控制器。

Because the name in the form starts with 'ViewModel', the mapped model should be modified like this.因为表单中的名称以'ViewModel'开头,所以映射模型应该像这样修改。

[HttpPost]
    public IActionResult AddCategories(Category ViewModel)
    {
        //...
        return Json(ViewModel);
    }

Then, the result is as follows.然后,结果如下。 在此处输入图片说明

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

相关问题 如何将字符串从 Angular 发布到 ASP.NET 核心操作方法? - How can I post a string to an ASP.NET Core action method from Angular? 使用 asp.net 核心 2.2 创建类别和子类别相关列表框 - creating category and subcategory dependent list box using asp.net core 2.2 如何在 asp.net core 3.1 mvc 中的另一个表中显示类别名称而不是 categoryId? - How to show category name instead of categoryId in view from another table in asp.net core 3.1 mvc? 如何用 ASP.NET Core MVC 中的下拉列表中的值填充表格 - How can i fill a table with values from a dropdownlist in ASP.NET Core MVC Asp.net 核心 -&gt; 如何使用 pKey(invoice_id, row_id) 从表中删除行 - Asp.net core -> How to Delete Row from table with pKey(invoice_id, row_id) 如何在自定义构建的 ASP.NET Core Razor Pages 可编辑网格中发布行 ID? - How do I post the row id in a custom built ASP.NET Core Razor Pages editable Grid? 如何在Asp.Net Core中动态更新表的每一行 - How to update each row of a table dynamically in a Asp.Net Core 如何使用 ASP.NET 核心和 Html 模式查看每个表格行的完整详细信息 - How do I Watch full details for each table row with ASP.NET Core and Html Modal 如何在asp.net中选择子类别 - How to select subcategory in asp.net 如何在 Asp.Net Core 中获取来自 Angular 的请求 POST - How to get in Asp.Net Core a request POST from Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM