无法将下拉列表中的值添加到数据库

时间:2019-05-05 16:53:53

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

我创建了一个表格来存储有关客户及其会员类型的信息。为此,我使用下拉列表保存成员资格类型的值。但是在提交表单时,成员资格类型的value(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" })

提交表单后,应将所有值添加到数据库中,包括下拉列表成员类型的值

2 个答案:

答案 0 :(得分:0)

您可以尝试通过这种方式进行操作:

//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>

答案 1 :(得分:0)

您需要显示更多关于模型之间的关系(一对一,一对多)。

发布操作的参数需要与视图模型相对应,请使用NewCustomerviewModel而不是Customers

下拉列表显示名称和传递ID的类型作为操作的值,因此您需要为ID或ID列表设置下拉列表的asp-for

请参阅我的演示,该演示使用多选将MembershipTypes的ID列表传递给操作。

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" })