簡體   English   中英

無法將下拉列表中的值添加到數據庫

[英]Unable to add the value from dropdownlist to database

我創建了一個表單來存儲有關客戶及其成員類型的信息。 為此,我使用下拉列表來保存成員資格類型的值。 但是在提交表單時,會員類型的值(Id)不會添加到數據庫中

//Model Membership Types
public int Id { get; set; }
public string Name { get; set; }

//ViewModel NewCustomerviewModel
public IEnumerable<MembershipTypes> MembershipTypes { get; set; }
public Customers  Customers{ get; set; }

//Controler CustomerController
public IActionResult Index()
{
 var customers = _context.Customers.Include(c => c.MembershipTypes).ToList();
 return View(customers);
}

[HttpPost]// Create is the aciton for Submit Button
public IActionResult Create(Customers customers)
{
 _context.Customers.Add(customers);
 _context.SaveChanges();
 return RedirectToAction("Index", "Customers");
}


//View Model
@model Wes.ViewModels.NewCustomerviewModel;
 @Html.DropDownListFor(m => m.Customers.MembershipTypes, new SelectList(Model.MembershipTypes, "Id", "Name"),"Select Membership Type", new { @class = "form-control" })

表單被提交時,它應該將所有值添加到數據庫,包括下拉列表成員資格類型的值

您可以嘗試這樣做:

//model
public int Id { get; set; }
public string Name { get; set; }
public enum MembershipTypes 
    {
    Type1,
    Type2,
    Type3
    }
public MembershipTypes _membershipTypes {get; set; }
//controller
[HttpPost]
public IActionResult Create([Bind("Id","Name","_membershipTypes")] Customers customers)
    {
        if (ModelState.IsValid)
        {
            _context.Add(customers);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }    
    Return View(customers);    
}
//view
<div class="row">
    <div class="col-md-6">
        <form asp-action="Create">

            <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">
                @Html.DropDownList("_membershipTypes",
                    new SelectList(Enum.GetValues(typeof(MembershipTypes))),
                    "Select membership type",
                    new { @class = "form-control" })
            </div>

            <input type="submit" value="Submit!" />

        </form>
    </div>
</div>

您需要更多地展示模型的關系(一對一,一對多)。

您的帖子操作的參數需要與您的視圖模型相對應,使用NewCustomerviewModel而不是Customers

下拉列表顯示名稱的類型和傳遞ID作為值的操作,因此需要為id或id列表設置下拉列表的asp-for

請參閱我的演示,它使用多個select將MembershipTypes id列表傳遞給action。

1.我的ViewModel NewCustomerviewModel

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

public class NewCustomerviewModel
{
    public int[] SelectMembershipTypesId { get; set; }
    public Customers Customers { get; set; }
}

public class Customers
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public IEnumerable<MembershipTypes> MembershipTypes { get; set; }
}

2.創建GET動作

public IActionResult Create()
    {
        var model = new NewCustomerviewModel()
        {              
            Customers = new Customers()
            {
                MembershipTypes = _context.MembershipTypes.ToList()
            },

        };
        return View(model);
    }

3.創建POST操作

[HttpPost]
 public async Task<IActionResult> Create(NewCustomerviewModel viewmodel)
    {
        if (ModelState.IsValid)
        {
            viewmodel.Customers.MembershipTypes= _context.MembershipTypes
                                                 .Where(m =>viewmodel.SelectMembershipTypesId.Contains(m.Id))
                                                 .ToList();
            _context.Add(viewmodel.Customers);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(viewmodel);
    }

4.創建視圖

 @Html.DropDownListFor(m => m.SelectMembershipTypesId,
                    new SelectList(Model.Customers.MembershipTypes, "Id", "Name"), "Select Membership Type",
                    new { @class = "form-control", @multiple = "multiple" })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM