Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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 从分部类内的数据库加载项列表_Asp.net Mvc_Entity Framework - Fatal编程技术网

Asp.net mvc 从分部类内的数据库加载项列表

Asp.net mvc 从分部类内的数据库加载项列表,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,这似乎是个愚蠢的问题,但我来了 我有一个表Shops(已经添加到我的.edmx文件中),我创建了一个分部类Shops,以便添加验证内容,结果如下: [MetadataType(typeof(ShopsMetaData))] public partial class Shops { } public class ShopsMetaData { [HiddenInput(DisplayValue = false)] public int ID { get; set; }

这似乎是个愚蠢的问题,但我来了

我有一个表Shops(已经添加到我的.edmx文件中),我创建了一个分部类Shops,以便添加验证内容,结果如下:

[MetadataType(typeof(ShopsMetaData))]
public partial class Shops
{

}

public class ShopsMetaData
{
    [HiddenInput(DisplayValue = false)]
    public int ID { get; set; }

    [Display(Name = "Shop name")]
    [Required(ErrorMessage = "The field shop name can't be empty")]
    public string ShopName { get; set; }

    [Required(ErrorMessage = "The field address can't be empty")]
    public string Address { get; set; }

    [Display(Name = "State")]
    [Required(ErrorMessage = "The field state can't be empty")]
    public string State { get; set; }
}
ViewBag.States = (from state in context.States
                  select new SelectListItem {          
                                Text = state.Name, 
                                Value = state.Id.ToString()}
                  ).ToList();
问题在于“State”属性:我可以简单地留下一个文本框,用户可以键入State的缩写,但这并不明智。我可以用预定义的状态放一个下拉列表,但我需要从数据库中获取它们


是否有任何方法可以使用可测试代码从此类内的数据库中获取状态列表?

我通常会在操作中设置一个动态属性来保存状态列表,如下所示:

[MetadataType(typeof(ShopsMetaData))]
public partial class Shops
{

}

public class ShopsMetaData
{
    [HiddenInput(DisplayValue = false)]
    public int ID { get; set; }

    [Display(Name = "Shop name")]
    [Required(ErrorMessage = "The field shop name can't be empty")]
    public string ShopName { get; set; }

    [Required(ErrorMessage = "The field address can't be empty")]
    public string Address { get; set; }

    [Display(Name = "State")]
    [Required(ErrorMessage = "The field state can't be empty")]
    public string State { get; set; }
}
ViewBag.States = (from state in context.States
                  select new SelectListItem {          
                                Text = state.Name, 
                                Value = state.Id.ToString()}
                  ).ToList();
然后我的观点是这样的

<div class="editor-label">
    @Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
    @Html.DropDownListFor(model => model.State, (IList<SelectListItem>)ViewBag.States)
</div>

@LabelFor(model=>model.State)
@DropDownListFor(model=>model.State,(IList)ViewBag.States)

这假设您的模型包含一个
状态
属性。

我通常做的是在我的操作中设置一个动态属性来保存状态列表,如下所示:

[MetadataType(typeof(ShopsMetaData))]
public partial class Shops
{

}

public class ShopsMetaData
{
    [HiddenInput(DisplayValue = false)]
    public int ID { get; set; }

    [Display(Name = "Shop name")]
    [Required(ErrorMessage = "The field shop name can't be empty")]
    public string ShopName { get; set; }

    [Required(ErrorMessage = "The field address can't be empty")]
    public string Address { get; set; }

    [Display(Name = "State")]
    [Required(ErrorMessage = "The field state can't be empty")]
    public string State { get; set; }
}
ViewBag.States = (from state in context.States
                  select new SelectListItem {          
                                Text = state.Name, 
                                Value = state.Id.ToString()}
                  ).ToList();
然后我的观点是这样的

<div class="editor-label">
    @Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
    @Html.DropDownListFor(model => model.State, (IList<SelectListItem>)ViewBag.States)
</div>

@LabelFor(model=>model.State)
@DropDownListFor(model=>model.State,(IList)ViewBag.States)
这假设您的模型包含
状态
属性