我正在尝试将用户所选值的值存储到控制器。但是,假定选择的值一直返回为空。该值甚至没有存储,如何获取所选值以进行存储?我不确定是否遗漏了什么或做错了什么。
型号: 在我的模型中,我创建了列表框的内容并存储了值。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace PAM_Dashboard_Project.Models
{
public class Vaults
{
public string Envs { get; set; }
}
public enum Envs
{
RTPprod,
OMA,
BG1,
BG2,
Cloud,
Workstation,
QA
}
}
查看:
<form asp-controller="CyberArk" asp-action="CyberArk" method="post" role="form" onsubmit="return confirm('Do you really want to execute this script?');" id="form1" style="display:none;">
<div id="accordion" role="tablist" aria-multiselectable="true">
@* Form 1 *@
<div class="card">
<div class="card-header" role="tab" id="headingTwo">
<h5 class="mb-0">
<a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo" style="font-size:15px;">
Vault Status
</a>
</h5>
</div>
<div id="collapseTwo" class="collapse" role="tabpanel" aria-labelledby="headingTwo">
<div class="card-block">
<div class="form-group">
<div class="form-group">
<p> This script returns status of vault servers for the chosen environment. It is used to determine if any servers overlap in order to detect a split brain.</p>
</div>
@model PAM_Dashboard_Project.Models.Vaults
@Html.DropDownList("Environments",
new SelectList(Enum.GetValues(typeof(Envs))),
"Select Enivronment" ,
new {@class = "form-control"})
<button type="submit">Submit</button>
</div>
</div>
</div>
</div>
</form>
控制器: 忽略[HttpPost]之前的任何内容,因为上面的内容无关。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PAM_Dashboard_Project.Models;
namespace PAM_Dashboard_Project.Controllers
{
public class CyberArkController : Controller
{
public IActionResult CyberArk()
{
return View();
}
[HttpPost]
public string CyberArk(Vaults newVault)
{
string SelectedValue = newVault.Envs;
return(SelectedValue);
}
}
}
答案 0 :(得分:0)
只需修改
@Html.DropDownList("Envs", new SelectList(Enum.GetValues(typeof(Envs))), "Select Enivronment" , new {@class = "form-control"})
您应将生成的选择标记的名称设置为与操作参数名称相同(在您的情况下为 Envs )