如何使用 MVC @html 帮助器将选择列表中的多个选择输入同一个 DB row/col

How to enter multiple selections from a selection list into same DB row/col using MVC @html helper

我的目标是制作我的 selection 列表,这些列表是加载状态的国家,也可以 select 多个,进入数据库中的相同 col/row 说每个 ID 之间有一个逗号。

我的预期结果是不只有用户输入的最后一个 selectMultiSelectionList 中的离子进入数据库,这是我目前的情况。

没有错误消息,但我对我能做的事情一无所知 try.This 是我第一次使用 MVC 和 HTML 助手,但我的代码如下.

    <tr>
    <td>
        <p>Location Countries (hold CTRL to select multiple countries):</p>
    </td>
    <td>
        @Html.DropDownListFor(model => model.Vendor.CompanyCountry, (MultiSelectList)ViewBag.Country, new Dictionary<string, Object> { { "data-stateId", "Vendor_CompanyState" }, { "class", "toFilter" }, { "multiple", "multiple" } })
    </td>
</tr>
<tr>
    <td>
        <p>Location States (hold CTRL to select multiple states):</p>
    </td>
    <td>
        @Html.DropDownListFor(model => model.Vendor.CompanyState, new MultiSelectList(ViewBag.StateOld, "Value", "Text", ViewBag.VendorState), "Select State", new { multiple = "multiple" })
    </td>
</tr>

具体输入数据库的行是

 @Html.DropDownListFor(model => model.Vendor.CompanyCountry, (MultiSelectList)ViewBag.Country, new Dictionary<string, Object> { { "data-stateId", "Vendor_CompanyState" }, { "class", "toFilter" }, { "multiple", "multiple" } })

这里

 @Html.DropDownListFor(model => model.Vendor.CompanyState, new MultiSelectList(ViewBag.StateOld, "Value", "Text", ViewBag.VendorState), "Select State", new { multiple = "multiple" })

我在想哪里

model => model

可能需要更改某些内容?

你是“post”字符串吗?考虑使用 String[].

// controller
public IActionResult UpdateData(string[] CompanyState)
{
}

我还会使用 html 标签和 asp-helpers 而不是 @Html.Helpers 来获得:

<select name="CompanyState" id="CompanyState" multiple="multiple">
    <option>Select</option> // empty value
    @foreach(var item in (MultiSelectList)ViewBag.VendorState)
    {
          <option value="@item.Value">@item.Text</option> // dont remember if it was good cast - please verify
    } 
</select>