asp.net核心下拉列表未设置所选值

时间:2020-07-16 04:26:22

标签: asp.net-core asp.net-mvc-4 twitter-bootstrap-3

我有一个bootstrap下拉列表,正在使用标签助手输入渲染值并在发布时读取它们。 下面是代码

asp-for =“ CfgType”-根据将视图设置为选定值的方式将值设置为SIN或UN -并在页面上我需要从CfgType属性获取下拉列表的选定值。

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "alwaysStrict": true,
    "baseUrl": ".",
    "declaration": true,
    "declarationDir": "dist",
    "esModuleInterop": true,
    "importHelpers": true,
    "jsx": "preserve",
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ],
    "module": "esnext",
    "moduleResolution": "node",
    "noFallthroughCasesInSwitch": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "sourceMap": true,
    "strict": true,
    "strictFunctionTypes": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": true,
    "target": "esnext",
    "types": [
      "webpack-env"
    ]
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ]
}

以下是尝试使用控制器检索值的方式

//现在fvIdentViewModel.CfgType为空

                <div class="form-row">
                    <div class="col-md-12">
                        <div class="form-group form-group--float">
                            <div class="form-group form-group--float">
                                <select id="dwType" class="form-control  custom-select" asp-for="CfgType">
                                    @if(Model.VehFlg)
                                    {
                                        <option value="SIN">Serial Num</option>
                                    }
                                    
                                    @if(Model.VinFlg)
                                    {
                                        <option value="UN">Unit Number</option>
                                    }

                               </select>
                                <label> Configuration Type</label>
                                <div class="invalid-tooltip"><span asp-validation-for="CfgType"></span></div>
                            </div>
                            <i class="form-group__bar"></i>
                        </div>

1 个答案:

答案 0 :(得分:0)

这是一个工作示例:

型号:

public class FleetVehicleIdentificationViewModel
{
    public string CfgType { get; set; }
    public bool VehFlg { get; set; }
    public bool VinFlg { get; set; }
}

查看(Index.cshtml):

@model FleetVehicleIdentificationViewModel
<form action="/fv-identification/Save" method="post">

    @Html.AntiForgeryToken()

    <div class="form-row">
        <div class="col-md-12">
            <div class="form-group form-group--float">
                <div class="form-group form-group--float">
                    <select id="dwType" class="form-control  custom-select" asp-for="CfgType">
                        @if (Model.VehFlg)
                        {
                            <option value="SIN">Serial Num</option>
                        }

                        @if (Model.VinFlg)
                        {
                            <option value="UN">Unit Number</option>
                        }

                    </select>
                    <label> Configuration Type</label>
                    <div class="invalid-tooltip"><span asp-validation-for="CfgType"></span></div>
                </div>
                <i class="form-group__bar"></i>
            </div>
            <div>
                <input type="submit" value="create" />
            </div>
        </div>
    </div>
</form>

控制器:

为了便于测试,我手动设置了值以呈现Index.cshtml

public IActionResult Index()
{
    var model = new FleetVehicleIdentificationViewModel()
    {
        VehFlg = true,
        VinFlg = false
    };
    return View(model);
}

您的帖子操作:

[Route("/fv-identification/Save"), HttpPost, ValidateAntiForgeryToken]
public IActionResult Save(FleetVehicleIdentificationViewModel fvIdentViewModel)
{
    //do your stuff...
}

结果: enter image description here