试图获取下拉列表的选择
本文关键字:选择 下拉列表 获取 | 更新日期: 2023-09-27 17:54:15
我试图从下拉列表中获得选择并将其保存到数据库中作为其相应的主键。
当用户进行选择时,我无法使用该选择。我想我没有在控制器中收集正确的信息。
这是我的观点:
<div class="form-group">
@Html.LabelFor(model => model.Type.TypeName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Types", ViewBag.Types as SelectList, new { htmlAttributes = new { @class = "form-control" } } )
@Html.ValidationMessageFor(model => model.Type.TypeName, "", new { @class = "text-danger" })
</div>
</div>
这是我的控制器
public ActionResult Create(CreateCommunicationViewModel commmodel){
...
var typeIDquery = db.Types.Where(g => g.TypeName == commmodel.Type.TypeName).Select(g => g.TypeID);
typeID = typeIDquery.AsEnumerable().FirstOrDefault().Trim().ToString();
我不确定你是否发布了你的视图的完整代码,但我认为你错过了HTML形式:
@using (Html.BeginForm("Create", "YourControllerName", FormMethod.Get)) {....}
在你的Create方法中你的参数名必须匹配你想要捕获的下拉列表的名称:
public ActionResult Create(string Types){....}
操作方法中的'Types'参数应该是从下拉列表中选择的值