MVC Razor 如何从表单获取选项值并将 viewmodel 属性设置为选定值

填写表格时,用户会收到从下拉菜单中选择特定选项的提示。所选选项应与其余输入一起发送到我的控制器以处理数据。


现在我不知道如何发送所选选项。


我想获取所选站并将其 ID 发送到我的控制器以用于创建新工厂。


我想要做的是这样的事情: <input asp-for="stationId" value=@selectedStation.Id">但是对于我的生活,我无法弄清楚如何获取选定的选项站并在发布表单时将模型的 stationId 值设置为它。


<form asp-controller="Plant" asp-action="Create" method="post">

    <div class="form-group">

        <label for="name">Name:</label>

        <input id="name" asp-for="Name" class="form-control" />

        <span asp-validation-for="Name"></span>

    </div>

    <div class="form-group">

        <label>Station</label>

        <select class="form-control" id="stationId">

            @foreach (var station in Model.Stations)

            {

                <option>@station.Name</option>

            }

        </select>

    </div>

    <button type="submit" class="btn btn-primary">Submit</button>

</form>

用于表单的视图模型:


 public string Name { get; set; }

        public string Description { get; set; }

        public int StationId { get; set; }

        public List<Station> Stations { get; set; }

最后是控制器动作:


[Authorize]

        [HttpPost]

        public IActionResult Create(CreatePlantViewModel createPlant)

        {

            var plant = new Plant

            {

                StationId = createPlant.StationId,

                Description = createPlant.Description,

                Name = createPlant.Name

            };

            _context.Add(plant);

            _context.SaveChanges();

            return RedirectToAction("Index");

        }

编辑:在堆栈溢出用户的帮助下,这是在站的选择元素中需要更改的内容:


<div class="form-group">

        <label>Station</label>

        <select class="form-control" id="stationId" asp-for="StationId">

            @foreach (var station in Model.Stations)

            {

                <option value="@station.Id">@station.Name</option>

            }

        </select>

    </div>


慕桂英546537
浏览 186回答 2
2回答

守着星空守着你

您遇到的问题与没有值被发送到控制器的事实有关。以下 :<option>@station.Name</option>应该 ://Assuming that your station class has an id<option value = "@station.id">@station.Name</option>&nbsp;你也可以这样做:<select id="stationId" asp-for="stationId" asp-items=@(new SelectList(Model.Stations, "stationId", "Name")) class="form-control"></select>

至尊宝的传说

您也可以为此使用 Razor,我发现它有助于跟踪您在做什么:@Html.DropDownListFor(m => m.StationId, --Value will be assigned to this variable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new SelectList(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Model.Stations,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"stationId",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Name"), --List of values will come from here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"-Select Station-", --The default value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new {id="stationId",class="Form-control" } -- Attributes you would assignin HTML&nbsp; &nbsp; )
打开App,查看更多内容
随时随地看视频慕课网APP