.net core 1.1 select taghelper pass mulitple select list items to controller总是显示计数0

时间:2017-05-16 13:19:52

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

我的select taghelper未将所选项目传递给控制器​​。计数始终为0.任何方向都将受到赞赏。 我的模特

    namespace SandBoxSelectList.Models
{

 public class Entry
 {
    [Key]
    public int EntryID { get; set; }

    public string EventName { get; set; }


    public List<EntryClass> EntryClasses { get; set; }

 }
 public class EntryClass
 {
    [Key]
    public int EntryClassId { get; set; }

    public string ClassName { get; set; }
    public int Classcode { get; set; }

 }
}

我的控制器

        // GET: Entries/Create
        var ec = new List<EntryClass>
        {
            new EntryClass
            {
                Classcode = 1,
                ClassName = "NameofOne",
                EntryClassId=1
            },
            new EntryClass
            {
                Classcode = 2,
                ClassName = "NameofRTwo",
                EntryClassId =2
            },
            new EntryClass
            {
                Classcode = 3,
                ClassName = "NameofRThree",
                EntryClassId = 3
            },
             new EntryClass
            {
                Classcode = 4,
                ClassName = "NameofFour",
                EntryClassId = 4
            },
        };
        ViewBag.FillForDropDown = ec.Select(x => new SelectListItem()
        {
            Text = x.ClassName,
            Value = x.EntryClassId.ToString()

        });
     return View();

post创建控制器条目返回EntryID = 0,EventName =&#34;是正确的&#34;,EntryClasses count = 0。

 // POST: Entries/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult>
    Create([Bind("EntryID,EventName,EntryClasses")] Entry entry)
    {
        if (ModelState.IsValid)
        {


            _context.Add(entry);
            await _context.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        return View(entry);
    }

Create.cshtml

 form asp-action="Create">
 <div class="form-horizontal">
    <h4>Entry</h4>
    <hr />
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="EventName" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="EventName" class="form-control" />
            <span asp-validation-for="EventName" class="text-danger"></span>
        </div>
    </div>

    <div asp-validation-summary="ModelOnly" class="text-danger"></div>

    <div class="form-group">
        <label asp-for="EntryClasses" class="col-md-2 control-label></label>

        <select class="col-md-10" 
        id="EntryClassID" asp-for="EntryClasses"asp-items="@ViewBag.FillForDropDown"
                name="EntryClasses" selected="selected" multiple="multiple">
        </select> 

        <span asp-validation-for="EntryClasses" class="text-danger"></span>

    </div>



    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
 </div>
</form>

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

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

1 个答案:

答案 0 :(得分:0)

无论我如何配置select taghelper,在我绑定到被调用条目的对象中都没有出现每个列表。 EntryClasses总是count = 0.作为解决方法,我在ModelState Values子项AttepemtedValue中找到了用户选择的项目。

我拆分它们,使用结果来查询记录的上下文。

 string attemptedvalue = ModelState.Values.Last().AttemptedValue;
            string[] b = attemptedvalue.Split(',');

看起来子键的顺序与绑定语句中的顺序相同。直到我找到一个有效的例子,我投票破了。