selectList razor标签助手asp.netCore

时间:2018-06-08 03:32:37

标签: entity-framework linq razor asp.net-core

我想在“订单”表单中提供“预告片”和“客户”的下拉列表。我可以使用Html标记帮助程序将Trailer数据从数据库传递到“Order”表单中的视图,但我无法对使用razor select标记帮助程序的客户执行相同的操作。为什么剃刀选择标记帮助器没有将值从数据库传递到视图?以下是我的代码片段。我很困惑为什么它不起作用

预告片类

public class Trailer
{
    public string SerialNumber { get; set; }
    public string TrailerNumber { get; set; }
    public string TrailerStatus { get; set; }

    public int TrailerID { get; set; }

    public virtual Order OrderforTrailer { get; set; }

    public Trailer()
    {
        TrailerStatus = "Available";
    }
}

客户类

public class Customer
{
    public string CustomerName { get; set; }
    public string StreetNumber { get; set; }
    public string StreetName { get; set; }
    public string ZipCode { get; set; }
    public string State { get; set; }
    public int CustomerID { get; set; }
    public IList<Order> CustomerOrders { get; set; }
}

订单类

public class Order
{
    public string OrderNumber { get; set; }
    public string OrderStatus { get; set; }
    public int OrderID { get; set; }

    public int TrailerForLoadID { get; set; }
    public virtual Trailer TrailerForLoad { get; set; }

    public int CustomerOrdersID { get; set;}
    public virtual Customer CustomerOrders { get; set; }

    public Order()
    {
        OrderStatus = "Available";
    }

}

AddOrderViewModel

public string OrderNumber { get; set; }

    public int TrailerID { get; set; }
    public List<SelectListItem> TrailersForLoad { get; set; }

    public int CustomerID { get; set; }
    public List<SelectListItem> CustomersOrder { get; set; }

    public AddOrderViewModel()
    {
    }

    public AddOrderViewModel(IEnumerable<Trailer> trailersForLoad, IEnumerable<Customer> customersOrder)
    {

        TrailersForLoad = new List<SelectListItem>();

        foreach (var trailer in trailersForLoad)

        {
            TrailersForLoad.Add(new SelectListItem

            {
                Value = (trailer.TrailerID).ToString(),
                Text = trailer.TrailerNumber
            });
        };

        CustomersOrder = new List<SelectListItem>();

        foreach (var customer in customersOrder)

        {
            CustomersOrder.Add(new SelectListItem

            {
                Value = (customer.CustomerID).ToString(),
                Text = customer.CustomerName
            });
        };

    }

}

订单控制器

public IActionResult Add()
    {
        IList<Trailer> trailerForLoad = context.Trailers.Where
        (c => c.TrailerStatus == "Available").ToList();

        IList<Customer> customerOrder = context.Customers.ToList();

        AddOrderViewModel addOrderViewModel = 
        new AddOrderViewModel(trailerForLoad, customerOrder);

        return View(addOrderViewModel);
    }

    [HttpPost]
    public IActionResult Add(AddOrderViewModel addOrderViewModel)
    {
        if (ModelState.IsValid)
        {
            Order newOrder = new Order()
            {
                OrderNumber = addOrderViewModel.OrderNumber,

                TrailerForLoad = context.Trailers.
                Where(x => x.TrailerID == addOrderViewModel
                .TrailerID).Single(),

                CustomerOrders = context.Customers
                .Single(x => x.CustomerID==addOrderViewModel.CustomerID)
            };
            context.Orders.Add(newOrder);

        trailerSelected = context.Trailers.Where(x => 
        x.TrailerID == addOrderViewModel.TrailerID).Single();

        trailerSelected.TrailerStatus = "Unavailable";   
        context.SaveChanges();
        return Redirect("/Order");
        }
        return View(addOrderViewModel);
    }

视图中的表单应显示客户列表

<form asp-controller="Order" asp-action="Add" method="post">

<fieldset>

    <div class="form-group">
        <label asp-for="OrderNumber">Order number </label>
        <input class="form-control" asp-for="OrderNumber" />
        <span asp-validation-for="OrderNumber"></span>
    </div>

    <div class="form-group">
        <label asp-for="TrailersForLoad">Trailer</label>
        @Html.DropDownListFor(x => x.TrailerID, Model.TrailersForLoad)
        <span asp-validation-for="TrailersForLoad"></span>
    </div>

    <div class="form-group">
        <label asp-for="CustomerID">Customers Name</label>
        <select asp-for="CustomerID" 
        asp-items="Model.CustomersOrder"></select>
        <span asp-validation-for="CustomerID"></span>
    </div>

    <div>
        <input type="submit" value="Submit" name="submitButton" />
    </div>

</fieldset>

1 个答案:

答案 0 :(得分:0)

您正在错误地使用SELECT标记帮助程序。在您当前的代码中,您正在使用自我结束标记方法!相反,您应该使用明确的</SELECT>结束标记

这应该有效

<select asp-for="CustomerID" asp-items="Model.CustomersOrder"></select>