首页 > 解决方案 > 选定的列表框项目不存储并返回 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);
        }

    }
}

标签: asp.net-mvc

解决方案


只需编辑

@Html.DropDownList("Envs", new SelectList(Enum.GetValues(typeof(Envs))), "Select Enivronment" , new {@class = "form-control"})

您应该将生成的选择标签的名称设置为与操作参数的名称相同(在您的情况下为Envs


推荐阅读