Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 来自数据库的mvc 5 dropdownlistfor_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 来自数据库的mvc 5 dropdownlistfor

Asp.net mvc 来自数据库的mvc 5 dropdownlistfor,asp.net-mvc,Asp.net Mvc,使用MVC5,似乎无法将dropdownlistfor绑定到我的模型。DisplayNameFor正确绑定。这是我的看法 <div class="row"> <h2>@ViewBag.Title</h2> <hr /> <div class="col-lg-12" id="div1"> <form class="form-horizontal" role="form" method="pos

使用MVC5,似乎无法将dropdownlistfor绑定到我的模型。DisplayNameFor正确绑定。这是我的看法

<div class="row">
    <h2>@ViewBag.Title</h2>
    <hr />
    <div class="col-lg-12" id="div1">
        <form class="form-horizontal" role="form" method="post" action="@Url.Action("GetKeys", "Encryption")">
            <div class="form-group">
              @Html.DisplayNameFor(model => model.CarrierID)
                <div class="col-sm-9">
                    @Html.DropDownListFor(model => model.CarrierID, new SelectList(ViewBag.Carriers, "id", "Name"), "----Select a  Carrier----")
                    @Html.ValidationMessageFor(model => model.CarrierID)
                </div>
            </div>
        </form>
    </div>
</div>

@视图包。标题

@DisplayNameFor(model=>model.CarrierID) @Html.DropDownListFor(model=>model.CarrierID,新选择列表(ViewBag.Carriers,“id”,“Name”),“----选择一个承运人----”) @Html.ValidationMessageFor(model=>model.CarrierID)
模型看起来像这样

[Key]
        public System.Guid EncryptionID { get; set; }

        [Required]
        [Display(Name = "Carrier Name:")]
        public string CarrierID { get; set; }
        public IEnumerable<SelectListItem> CarrierNames { get; set; }

        [Required]
        [Display(Name = "Text to Encrypt:")]
        public string txtText { get; set; }

        public string Thumbprint { get; set; }
[Key]
public System.Guid EncryptionID{get;set;}
[必需]
[显示(Name=“承运人名称:”)]
公共字符串载体ID{get;set;}
公共IEnumerable载体名称{get;set;}
[必需]
[显示(Name=“要加密的文本:”)]
公共字符串txtText{get;set;}
公共字符串指纹{get;set;}

intellisense显示该模型不包含CarrierID、Thinks?的定义。

我刚刚测试了您的代码,该属性显示在intellisense中,对我来说很好VS2013 premium

也许这是你的VS的问题

值得一提的是,我测试了表单post,在将viewbag selectlist更改为以下内容时,可以看到下拉列表中的值发生了变化:

@Html.DropDownListFor(model => model.CarrierID, MyModel.MySelectListItems())


    public static IList<SelectListItem> MySelectListItems()
    {
        return new List<SelectListItem>{
            new SelectListItem {Text = "option1", Value = "1"},
            new SelectListItem{Text = "option2", Value = "2"}
        };
    }
@Html.DropDownListFor(model=>model.CarrierID,MyModel.MySelectListItems())
公共静态IList MySelectListItems()
{
返回新列表{
新建SelectListItem{Text=“option1”,Value=“1”},
新建SelectListItem{Text=“option2”,Value=“2”}
};
}

所以,我找到了解决方案。这是元素被引用的方式

                @Html.DropDownListFor(model => model.ElementAt<Professional_Services_Tools.Models.t_ConfigBasedEncryption>(0).CarrierID, new SelectList(ViewBag.Carriers, "Value", "Text"), "----Select a  Carrier----")
                @Html.ValidationMessageFor(model => model.ElementAt<Professional_Services_Tools.Models.t_ConfigBasedEncryption>(0).CarrierID)
@Html.DropDownListFor(model=>model.ElementAt(0.CarrierID),新的选择列表(ViewBag.Carriers,“Value”,“Text”),“----选择一个载体--”)
@Html.ValidationMessageFor(model=>model.ElementAt(0.CarrierID)

不,我在运行时也遇到了同样的错误。另外,要注意的是,这是从DbSet中提取的。因此,视图上的模型是IEnumerablet,而不是解决方案。当您提交时,它将永远不会绑定到您的模型-您将生成一个与您的模型没有关系的
name
属性。