首页 > 解决方案 > MVC Core试图通过选择第一列来填充表格行

问题描述

我对编程很陌生,我真的很困惑如何做到这一点..

我正在尝试做的是一个表格,用于在挑选我想要的材料后创建报价。当我选择不同的材料时,价格会发生变化。最后,我将保存报价,总价等。

我有一个产品型号列表。在我看来,我想使用它的 FullName 在“第一列”中选择我想要的产品模型,并且我想用我刚刚选择的模型的预定义详细信息填充同一行中的其他列。

我做了一个研究如何做到这一点。提到了 Jquery、Ajax,但我将不得不添加更多带有更多 Viewbag 的下拉列表。那么我不必为每个下拉列表编写一个新的 Ajax 吗?

我真的希望以我应该尝试这样做的方式得到指导。

public class Model:BaseEntity
{
    public TypeOfPart TypeOfPart { get; set; }
    public string BrandName { get; set; }
    public string ModelName { get; set; }
    public string VersionName { get; set; }
    public decimal? ListPriceTL { get; set; }
    public decimal? ListPriceForeign { get; set; }
    public ForeignCurrency? ForeignCurrency { get; set; }
    public decimal CurrentDiscount { get; set; } = 0.00M;

    public int? CertificateId { get; set; }
    public Certificate Certificate { get; set; }

    public string FullName => $"{BrandName} {ModelName} {VersionName}";

我的控制器

public IActionResult Create()
    {
        int amountOfElevators = Convert.ToInt32(TempData["Amount"]);
        ViewBag.amount = amountOfElevators;


        ViewData["Machines"] = new SelectList(_context.ModelRepo.GetAll()
            .Where(x => x.TypeOfPart 
==TypeOfPart.Machine),"Id","FullName");
        
   

        ViewData["ConstructionCompanyId"] = new 
SelectList(_context.ConstructionCompanyRepo.GetAll(), "Id", "Name","Id");
        ViewData["EmployeeId"] = new 
SelectList(_context.EmployeeRepo.GetAll(), "Id", "FullName");
        return View();
    }

这是我需要的更多描述的图像

编辑:我的视图模型

public class InstallationOfferVM
{
    public InstallationOfferVM()
    {
        OfferedElevators = new List<OfferedElevator>();
        Models = new List<Model>();
    }
    public int Id { get; set; }
    [Display(Name = "Toplam ₺&quot;)]
    public decimal TotalPriceTL { get; set; }
    [Display(Name = "Toplam €&quot;)]
    public decimal TotalPriceEuro { get; set; }
    [Display(Name = "Açıklama")]
    public string Description { get; set; }
    [Display(Name = "Müşteri Firma")]
    public int ConstructionCompanyId { get; set; }
    [Display(Name = "Müşteri Firma")]
    public string CompanyName { get; set; }
    public ConstructionCompany ConstructionCompany { get; set; }
    [Display(Name = "Teklif Tarihi")]
    [DataType(DataType.Date)]
    public DateTime DateOfProposal { get; set; }
    [Display(Name = "Teklifi Veren")]
    public int EmployeeId { get; set; }
    [Display(Name = "Teklifi Veren")]
    public string EmployeeFullName { get; set; }
    public Employee Employee { get; set; }
    [Display(Name = "Teklif Durumu")]
    public ProposalStatus ProposalStatus { get; set; }
    [Display(Name = "İletme Şekli")]
    public DeliveredBy DeliveredBy { get; set; }
    [Display(Name = "Asansör Adedi")]
    public int AmountOfElevators { get; set; }


    public List<OfferedElevator> OfferedElevators { get; set; }
    public OfferedElevator OfferedElevator { get; set; }


    public int InstallationOfferId { get; set; }
    [Display(Name = "Kapasite")]
    public int Capacity { get; set; }
    [Display(Name = "Durak")]
    public int StopCount { get; set; }
    [Display(Name = "Halat Adedi")]
    public int MachineRopeCount { get; set; }
    [Display(Name = "Askı Tipi")]
    public int HangStyle { get; set; } = 1;
    [Display(Name = "Tahrik Tipi")]
    public DriveType DriveType { get; set; }
    [Display(Name = "Ortalama Kat Yüksekliği")]
    public decimal AvgFloorHeight { get; set; } = 3.00M;
    [Display(Name = "Giriş Adedi")]
    public int EntranceCount { get; set; } = 1;
    [Display(Name = "Ekstra Kapı")]
    public int AdditionalDoor { get; set; } = 1;
    [Display(Name = "Ağırlık Yanda İse")]
    public bool WeightInSide { get; set; }
    [Display(Name = "Kapı Yüksekliği 2100 ise")]
    public bool DoorHeightIs2100 { get; set; } = false;
    [Display(Name = "Euro Kuru")]
    public decimal CurrentEuro { get; set; }
    public List<Model> Models { get; set; }
    public Model Model { get; set; }
}

我的观点

@model Ace.ViewModels.InstallationOfferVM
@for (int i = 0; i < ViewBag.amount; i++)
{
<table class="table-sm table-striped table-dark col-md-9">
            <thead>
                <tr>
                    <th></th>
                    <th>Malzeme Cinsi</th>
                    <th>Adet</th>
                    <th>Liste Euro</th>
                    <th>Liste TL</th>
                    <th>İskonto</th>
                    <th>Toplam</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td></td>
                    <td><select asp-for="Model.Id" class="form-control" asp- 
items="ViewBag.Machines"></select></td>
                    <td>1</td>
                    <td></td>
                </tr>
            </tbody>
        </table>
    </div>
    <hr />
}
</form>
<div>
<a asp-action="Index">Back to List</a>
</div>

标签: c#asp.net-core-mvc

解决方案


我建议以这种方式更改您的控制器方法:

public IActionResult Create()
{
    var offeredElevators = //query here to get the offeredElevators
    var viewmodel = new InstallationOfferVM 
    {
       OfferedElevators = offeredElevators,
       Models = new List<Model>()
    };

    return View(viewmodel);
}

这样你至少可以把你的数据放到视图中,它应该是如何完成的。我不确定我是否同意你的模型/视图模型..

希望这至少有一点帮助


推荐阅读