【问题标题】:MVC Dropdownlistfor returns null selected valueMVC Dropdownlistfor 返回 null 选定值
【发布时间】:2019-11-20 21:26:45
【问题描述】:

this question 和许多其他类似,我无法从@Html.Dropdownlistfor 中检索选定的值。但我想我避免了常见的错误,所以我不确定我做错了什么。这是我的控制器:

public class AdministrationController : Controller
{
    public ActionResult ParamStatique()
    {
        ParamStatiqueViewModels psvm = new ParamStatiqueViewModels()
        {
            a_NEquipe = "1"
        };

        using (Dal dal = new Dal())
        {
            psvm.EquipesTravaux = dal.GetEquipesTravaux();
        }
        return View(psvm);
    }

    [HttpPost]
    public ActionResult ParamStatique(ParamStatiqueViewModels psvm)
    {

        Debug.WriteLine("NEquipe : " + psvm.a_NEquipe);

        using (Dal dal = new Dal())
        {
            psvm.EquipesTravaux = dal.GetEquipesTravaux();
        }

        return View(psvm);
    }
}

我的模型视图如下所示:

public class ParamStatiqueViewModels
{
    public List<EQUIPE_TRAVAUX> EquipesTravaux { get; set; }

    [Display(Name = "N° Équipe")]
    public string a_NEquipe { get; set; }
}

这是我的观点:

@using (Html.BeginForm("ParamStatique", "Administration", null, FormMethod.Post, new { id = "modalform", role = "form" }))
{
    <div class="modal-body" id="modal-body">
        <div class="form-group" id="aNEquipe">
            @Html.LabelFor(m => m.a_NEquipe, new { @class = "col-form-label" })
            @Html.DropDownListFor(m => m.a_NEquipe, new SelectList(Model.EquipesTravaux, "TRAV_SEQ", "TRAV_CODE"), new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.a_NEquipe, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="modal-footer">
        <button type="submit" class="btn btn-primary">Enregistrer</button>
    </div>
}

我的 EQUIPE_TRAVAUX 类是从 EF6 生成的:

public partial class EQUIPE_TRAVAUX
{
    public short TRAV_SEQ { get; set; }
    public string TRAV_CODE { get; set; }
}

下拉列表已正确填充,我可以选择我想要的选项,但是当我单击“Enregistrer”提交按钮时,它会在我的模型中返回一个空 a_NEEquipe 字段,即使我正确指定了它在 Html Helper 中:

@Html.DropDownListFor(m => m.a_NEquipe, new SelectList(Model.EquipesTravaux, "TRAV_SEQ", "TRAV_CODE"), new { @class = "form-control" })

Debug.WriteLine("NEquipe : " + psvm.a_NEquipe); 的结果始终为 null,如下所示:

NEEquipe:

如果我将 [Required] 标签添加到我的模型中,客户端验证会阻止表单回发调用。

如何检索预期变量 a_NEEquipe 中的选定值?

【问题讨论】:

  • 我认为a_NEquipe需要在模型中声明为public IEnumerable
  • 在我找到的关于这个主题的所有帖子或教程中,存储所选值的变量始终是一个 int 或一个字符串。无论如何,我试图将 a_NEquipe 声明为 IEnumerable 但它仍然为空
  • 在您的视图中将@Html.DropDownListFor(m => m.a_NEEquipe, Model.EquipesTavaux), 在模型IEnumerable a_NEQuipe, string EquipesTavaux
  • 尝试将 a_NEquipe 从字符串更改为 int,因为这是下拉列表的索引器
  • @JamesS 我也尝试使用 int,当您在模型中使用不可为空的类型时,我必须将其设为可空(int?)以避免 [Required] 标签的自动化。实际上,即使我使用字符串或 int,我的变量仍然是空的

标签: c# asp.net-mvc razor


【解决方案1】:

尝试像这样指定文本和值字段:

@Html.DropDownListFor(model =&gt; model.a_NEquipe, Model.EquipesTravaux.Select(x =&gt; new SelectListItem { Text = x.TRAV_SEQ, Value = x.TRAV_CODE }), new { @class = "form-control"})

【讨论】:

  • 我尝试了这样的解决方案:@Html.DropDownListFor(model => model.a_NEquipe, Model.EquipesTravaux.Select(x => new SelectListItem { Value = x.TRAV_SEQ + "", Text = x.TRAV_CODE }),新的 { @class= "form-control" })。 Dropdown 仍按预期填充,但 model.a_NEEquipe 仍然为空...
  • 你能写一个javascript函数在警告框中显示选中的值吗?然后我们就会知道选择的项目是否正确。
  • 是的,我也有同样的想法!我添加了这段代码: document.getElementById('a_NEquipe').addEventListener('change', function () { var e = document.getElementById('a_NEquipe') alert(e.options[e.selectedIndex].text) ; });并且在警报中显示正确的值,似乎在回发期间丢失了值
  • 你可以试试 e.options[e.selectedIndex].value 代替 e.options[e.selectedIndex].text 吗?
  • 别抱歉,这就是 stackoverflow 的用途。如果答案有帮助,我希望您不介意接受并投票赞成答案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多