亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

MVC Razor 如何從表單獲取選項值并將 viewmodel 屬性設置為選定值

MVC Razor 如何從表單獲取選項值并將 viewmodel 屬性設置為選定值

C#
慕桂英546537 2022-06-12 15:30:00
填寫表格時,用戶會收到從下拉菜單中選擇特定選項的提示。所選選項應與其余輸入一起發送到我的控制器以處理數據?,F在我不知道如何發送所選選項。我想獲取所選站并將其 ID 發送到我的控制器以用于創建新工廠。我想要做的是這樣的事情: <input asp-for="stationId" [email protected]">但是對于我的生活,我無法弄清楚如何獲取選定的選項站并在發布表單時將模型的 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>
查看完整描述

2 回答

?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

您遇到的問題與沒有值被發送到控制器的事實有關。以下 :


<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>



查看完整回答
反對 回復 2022-06-12
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

您也可以為此使用 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

    )


查看完整回答
反對 回復 2022-06-12
  • 2 回答
  • 0 關注
  • 141 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號