繁体   English   中英

我的代码有什么问题,我的下拉列表不起作用

[英]What is wrong in my code that my dropdown list isn't working

这是我的索引操作,我将所有产品都放在我的视图中

public async Task<IActionResult> Index()
{
    return View(await _productRepo.GetAllAsync());
}

这是我的创建操作,我选择了所有类别 ID 和名称来查看。类别

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,SKU,Description,Price,OldPrice,Status,IsMenuItem,Count,CategoryId")] Product productModel)
{
    ViewBag.Categories = new SelectList(await _categoryrepo.GetAllAsync() , "Id" , "Name");

    if (ModelState.IsValid)
    {
        await _productRepo.CreateAsync(productModel);
        return RedirectToAction(nameof(Index));
    }

    return View(productModel);
}

这是我的看法

@model OnlineShopArmenia.Models.Product

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

<h1>Create</h1>

<h4>Product</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="SKU" class="control-label"></label>
                <input asp-for="SKU" class="form-control" />
                <span asp-validation-for="SKU" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Description" class="control-label"></label>
                <input asp-for="Description" class="form-control" />
                <span asp-validation-for="Description" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Price" class="control-label"></label>
                <input asp-for="Price" class="form-control" />
                <span asp-validation-for="Price" class="text-danger"></span>
            </div>
            <div class="form-group form-check">
                <label class="form-check-label">
                    <input class="form-check-input" asp-for="Status" /> @Html.DisplayNameFor(model => model.Status)
                </label>
            </div>
            <div class="form-group form-check">
                <label class="form-check-label">
                    <input class="form-check-input" asp-for="IsMenuItem" /> @Html.DisplayNameFor(model => model.IsMenuItem)
                </label>
            </div>
            <div class="form-group">
                <label asp-for="Count" class="control-label"></label>
                <input asp-for="Count" class="form-control" />
                <span asp-validation-for="Count" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label>Categories :</label>
                <select name="Id" asp-for="Id" class="formcontrol" asp-items="ViewBag.Categories">
                    <option value="@ViewBag.Categories"></option>
                </select>
                <span asp-validation-for="Id" class="text-danger"></span>
            </div>

            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

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

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

这是产品 Model

public class Product
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public string SKU { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        public bool Status { get; set; }
        public bool IsMenuItem { get; set; }
        public int Count { get; set; }
        public int? CategoryId { get; set; }
        public Category Category { get; set; }
    }

这是类别 Model

public class Category
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
    }

这是结果

我想在下拉列表中查看类别,我该怎么做? 我认为我们必须更改<select><option>标记中的属性,请帮我修复它

  1. 您使用了错误的操作方法。 对于创建操作,您的 controller 应该有两种操作方法 - GET方法和POST方法。 您现在使用的那个(标有[HttpPost] )用于从您的视图接收数据,一旦您尝试保存它们。 要将数据传递给视图(在创建视图时),请使用标有[HttpGet]属性(或者,根本没有标有任何属性)的操作方法。 以下GET方法应该足以创建您的视图 -
[HttpGet]  // this attribute is optional
public IActionResult Create()
{
    ViewBag.Categories = new SelectList(await _categoryrepo.GetAllAsync() , "Id" , "Name");
    return View();
}
  1. 您将select标签标记错误,它应该代表外键属性CategoryId ,而不是产品的Id -
<select asp-for="CategoryId" class ="form-control" asp-items="ViewBag.Categories"></select>

您不需要option标签,除非您想要一个显示值,例如Please select a Category或其他东西 -

<select asp-for="CategoryId" class ="form-control" asp-items="ViewBag.Categories">
    <option value="">Please choose user category:</option>
</select>

编辑:
I'm not sure what version of ASP.NET MVC or, ASP.NET Core MVC you are using, but the razor syntax above works in .NET 5 .

您应该有 2 个操作 - 创建或编辑视图并保存数据。

您创建视图的操作应如下所示:

[HttpGet]  // this attribute is optional
public async Task<IActionResult> Create()
{
  
    return  await Edit(0);
}
[HttpGet("{id?}")]
public async Task<IActionResult> Edit(int id)
{
      ProductModel model=null;
if id=0 model=new Product();
else model= await _productRepo.GetAsync(id);
 ViewBag.Categories = new SelectList(await _categoryrepo.GetAllAsync() 
, "Id" , "Name");

    return View(model);
}

并修复您的 select:

 <select name="categoryId" asp-for="CategoryId" class="formcontrol" asp-items="@ViewBag.Categories">
 </select>

暂无
暂无

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

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