简体   繁体   English

列表框发布,但不填充模型

[英]Listbox POSTing but not populating model

I have a model that looks like this: 我有一个看起来像这样的模型:

    public List<SelectListItem> Interests { get; } = new List<SelectListItem>
    {
        new SelectListItem { Value = "Buyers Agent", Text = "Buyers Agent" },
        new SelectListItem { Value = "Showing Agent", Text = "Showing Agent" },
        new SelectListItem { Value = "Realtor Associate", Text = "Realtor Associate"  },
        new SelectListItem { Value = "Transaction Coordinator", Text = "Transaction Coordinator"  },
        new SelectListItem { Value = "Guest Blog Writer", Text = "Guest Blog Writer"  }
    };

The view looks like this: 该视图如下所示:

            <div>
                <label for="Interests" class="control-label col-sm-2" style="padding-left: 0px;">Your Interests: </label>
                <select asp-for="Interests" asp-items="Model.Interests"></select> 
            </div>

And renders to this: 并对此进行渲染:

<div>
   <label for="Interests" class="control-label col-sm-2" style="padding-left: 0px;">Your Interests: </label>
   <select id="Interests" multiple="multiple" name="Interests">
        <option value="Buyers Agent">Buyers Agent</option>
        <option value="Showing Agent">Showing Agent</option>
        <option value="Realtor Associate">Realtor Associate</option>
        <option value="Transaction Coordinator">Transaction Coordinator</option>
        <option value="Guest Blog Writer">Guest Blog Writer</option>
    </select> 
</div>

If items are selected, they are POSTed back, but controller call shows 0 items in the 'Interests' property of the model. 如果选择了项目,则将它们回发,但是控制器调用在模型的“兴趣”属性中显示0个项目。 The controller looks like this: 控制器如下所示:

    [HttpGet]
    [AllowAnonymous]
    public IActionResult Interest()
    {
        var model = new InterestFormViewModel();

        return View(model);
    }

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public IActionResult Interest(InterestFormViewModel model)
    {
        // A breakpoint and watch show model.Interests to have a count of 0.

        return View("HRThanks");
    }

I must be overlooking something obvious, can someone tell me what it might be? 我必须忽略一些显而易见的事情,有人可以告诉我这可能是什么吗? Thanks! 谢谢!

The <select> element posts back a single value (or an array of simple values in the case of a <select multiple> . You cannot bind your select to Interests because its is a collection of complex objects ( SelectListItem ). <select>元素回发单个值(如果是<select multiple> ,则回传简单值数组。您不能将select绑定到Interests因为它是复杂对象的集合( SelectListItem )。

Your model needs a property to bind the selected value(s) to. 您的模型需要一个属性来绑定所选值。 If you want a dropdownlist, then (say) 如果您想要一个下拉列表,那么(说)

public string SelectedItem { get; set; }

and in the view 并认为

<label for="SelectedItem" ...</label>
<select asp-for="SelectedItem" asp-items="Model.Interests"></select> 

or if you want a listbox, then 或者如果您想要一个列表框,那么

public IEnumerable<string> SelectedItems { get; set; }

and in the view 并认为

<select asp-for="SelectedItems" asp-items="Model.Interests"></select>

Note that of you set the value of SelectedItem (or SelectedItems ) in the GET method before you pass the model to the view, and its value(s) match one of the options, then that option(s) will be selected initially. 请注意,您需要在将模型传递给视图之前在GET方法中设置SelectedItem (或SelectedItems )的值,并且其值与选项之一匹配,然后将首先选择该选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM