【问题标题】:MVC Razor how to get option value from form and set viewmodel property to selected valueMVC Razor 如何从表单获取选项值并将 viewmodel 属性设置为选定值
【发布时间】:2018-12-13 12:10:11
【问题描述】:

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

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

我想获取所选站并将其 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>

【问题讨论】:

    标签: c# razor asp.net-core .net-core


    【解决方案1】:

    您遇到的问题与没有向控制器发送值有关。 以下:

    <option>@station.Name</option>
    

    应该是:

    //Assuming that your station class has an id
    <option value = "@station.id">@station.Name</option> 
    

    你也可以这样做:

    <select id="stationId" asp-for="stationId" asp-items=@(new SelectList(Model.Stations, "stationId", "Name")) class="form-control"></select>
    

    【讨论】:

    • 谢谢!只设置值但没有这样做。我还必须在周围的选择元素中设置asp-for="StationId"。非常感谢您的帮助!
    • @Gurkmeja101 很高兴它有帮助
    【解决方案2】:

    你也可以使用 Razor,我发现它有助于跟踪你在做什么:

    @Html.DropDownListFor(m => m.StationId, --Value will be assigned to this variable
                         new SelectList(
                             Model.Stations, 
                             "stationId", 
                             "Name"), --List of values will come from here
                         "-Select Station-", --The default value
                         new {id="stationId",class="Form-control" } -- Attributes you would assignin HTML
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多