MVC DropDownListFor空值 [英] MVC DropDownListFor Null Values

查看:377
本文介绍了MVC DropDownListFor空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC中dropdownlistfor的HtmlHelper有问题。当回发发生时没有任何选择,并在模型列表中的值,以及所选择的项目是空的。

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.

下面是我的模型:

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; }

    }
}

控制器:

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();
        }


    }
}

和查看:

    @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:

任何人都可以阐明一些轻我在做什么错了?

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

推荐答案

您正在运行到的问题是,MVC不知道如何在下拉列表中选择的值(这是一个字符串)转换为对象成员

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.

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

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

新的客户类别:

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--")

这会从下拉列表中返回所选值到属性了selectedValue。

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

这是你的成员名单返回null是因为HTML没有在下拉列表中返回选项的原因,它只返回选定的值。因此,当信息进入的方法,它仅给出的输入形式的值。

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.

如果你想看到什么样的信息被发送回你可以使用开发者控制台,并获取返回的Http请求的服务器

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

和/或

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

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

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

  return View();
}

这篇关于MVC DropDownListFor空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆