C# MVC DropDownListFor Null 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16058539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:51:50  来源:igfitidea点击:

MVC DropDownListFor Null Values

c#asp.net-mvcrazor

提问by Christopher Townsend

I am having problems using the dropdownlistfor htmlhelper in MVC. When post back occurs there is nothing selected and the values in the model for the list, and the selected item are null.

我在 MVC 中使用 dropdownlistfor htmlhelper 时遇到问题。当发生回发时,没有选择任何内容,列表模型中的值以及所选项目为空。

Here are my Models:

这是我的模型:

namespace MvcTestWebApp.Models
{
    public class Customer
    {
        public string name { get; set; }

        public List<SelectListItem> members { get; set; }

        public Member selected { get; set; }
    }

    public class Member
    {
        public string name { get; set; }

    }
}

Controller:

控制器:

namespace MvcTestWebApp.Models
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/
        public ActionResult Index()
        {
            Customer cust = new Customer() { name = "Cust1" };
            cust.members = new List<SelectListItem>();

            cust.members.Add(new SelectListItem(){Text = "Bob"} );
            cust.members.Add(new SelectListItem(){Text = "Dave"} );

            return View(cust);
        }

        [HttpPost]
        public ActionResult Index(Customer customer)
        {

           return View();
        }


    }
}

And View:

并查看:

    @model MvcTestWebApp.Models.Customer

@{
    ViewBag.Title = "Customer";
    Layout = null;
}

<!DOCTYPE html>

<html>

<head>
    <meta name="viewport" content="width=device-width" />
    <title> Index </title>
</head>

    <body>


@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Customer Data</legend>

          @Html.HiddenFor(model => model.name)

          @Html.DropDownListFor(model => model.selected, Model.members, "--Select--")

        <p>
            <input type="submit" value="Save" />
        </p>

        </fieldset> 
}

    </body>

</html>

When I select something from the select list and click the submit button nulls are returned:

当我从选择列表中选择某些内容并单击提交按钮时,将返回空值:

nullselect

空选择

Can anyone shed some light on what I'm doing wrong?

任何人都可以阐明我做错了什么吗?

采纳答案by SBurris

The issue you are running into is that MVC does not know how to translate the selected value of the drop down list (which is a string) to the object Member.

您遇到的问题是 MVC 不知道如何将下拉列表的选定值(这是一个字符串)转换为对象成员。

What you should do is have a selectedValue property that is used to set the value in the dropdown and retrieve the returned value.

您应该做的是有一个 selectedValue 属性,用于在下拉列表中设置值并检索返回值。

New Customer Class:

新客户类别:

public class Customer
{
  public string name { get; set; }

  public List<SelectListItem> members { get; set; }

  public string selectedValue { get; set; }

  public Member selected { get; set; }
}

Updated Dropdown list control:

更新下拉列表控件:

@Html.DropDownListFor(model => model.selectedValue, Model.members, "--Select--")

This will return the selected value from the dropdown list into the property selectedValue.

这将从下拉列表中选择的值返回到属性 selectedValue。

The reason that your List of members is returned null is because HTML does not return the options in a drop down list, it only returns the selected value. So when the information comes into the method, it is only given the values of the input forms.

您的成员列表返回 null 的原因是因为 HTML 不返回下拉列表中的选项,它只返回选定的值。所以当信息进入方法时,它只给出输入表单的值。

If you want to see what information gets sent back to the server you can use the developer console and capture the return Http request

如果您想查看哪些信息被发送回服务器,您可以使用开发者控制台并捕获返回的 Http 请求

and/or

和/或

You can add FormCollection collectionto the parameters of the controller action to see what information MVC is using to build the objects it passes to the methods.

您可以添加FormCollection collection到控制器操作的参数中,以查看 MVC 使用哪些信息来构建它传递给方法的对象。

[HttpPost]
public ActionResult Index(Customer customer, FormCollection collection)
{

  return View();
}

回答by webdeveloper

cust.members.Add(new SelectListItem(){Text = "Bob"} );
cust.members.Add(new SelectListItem(){Text = "Dave"} );

Try to set value:

尝试设置值:

cust.members.Add(new SelectListItem(){Value = "Bob", Text = "Bob"} );
cust.members.Add(new SelectListItem(){Value = "Dave", Text = "Dave"} );

回答by Jerrime25

use jQuery prepend to fix the problem with ease.

使用 jQuery 前置轻松解决问题。

$("#idOfSelectTag").prepend('
                              <option selected="selected" value="null">
                                -- Select School --
                              </option>
                            ');