asp.net-mvc - 选定的列表框项目不存储并返回 null
问题描述
我正在尝试将用户选择的值的值存储到控制器中。但是应该选择的值不断返回为空。该值甚至没有存储,如何获取要存储的选定值?我不确定我是否遗漏了什么或者我做错了什么。
模型:在我的模型中,我创建了列表框的内容并存储了值。
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);
}
}
}
解决方案
只需编辑
@Html.DropDownList("Envs", new SelectList(Enum.GetValues(typeof(Envs))), "Select Enivronment" , new {@class = "form-control"})
您应该将生成的选择标签的名称设置为与操作参数的名称相同(在您的情况下为Envs)
推荐阅读
- java - 如何快速检查字符串属于哪个范围
- c# - 如何在 .Net Core 5 api 中使用 ws-security soap 服务?
- flutter - 错误:一个文件不能属于多个库
- android - AndroidManifest.xml 此处不允许使用该属性
- python - python,tkinter,创建条形图时出现问题
- reactjs - 使多台机器可以访问流
- javascript - React js将对象转换为数组以供useState
- node.js - 使用 GridView/BoxView 组件时反应 nodegui 调用堆栈超出错误
- php - 在 PHP 中创建基于优先级的多维数组
- javascript - 如何在下一个 js SSR 的标头 axios 中发送 deviceId 或 userId