Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/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 3 MVC3中模型类的Razor视图下拉列表_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 MVC3中模型类的Razor视图下拉列表

Asp.net mvc 3 MVC3中模型类的Razor视图下拉列表,asp.net-mvc-3,Asp.net Mvc 3,我在MVC3中有两个模型类,一个用于具有这些属性的服务 public int ID { get; set; } public string Name { get; set; } public string Description { get; set; } public string Image { get; set; } public int ChildOf { get; set; } 它还有一个Entityframework的DB表 另一个模型是Quata,它具有这些属性 public i

我在MVC3中有两个模型类,一个用于具有这些属性的
服务

public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public int ChildOf { get; set; }
它还有一个Entityframework的DB表

另一个模型是
Quata
,它具有这些属性

public int ID { get; set; }
public string Sender_Name { get; set; }
public string Description { get; set; }
.....
......
public Services Service_ID { get; set; }
它还有一个Entityframework的DB表

我想创建一个Razor(C#)视图(
用于Quata
)其中,用户可以通过填写html表单发送quata,但我想显示一个下拉列表,其中
服务ID
下拉值
服务名称
下拉文本,这也是动态来自服务数据库表


我的问题是如何通过
@Html.DropDownListFor
创建动态的下拉列表?并将从该下拉列表中选择的数据发送到控制器?

查看
@Html.DropDownListFor
试试这个

控制器:

 public ActionResult Create()
    {
        var Services = new Services();

        Services.Load(); //load services..

        ViewBag.ID = new SelectList(Services.ToList(), "Id", "Name");


        return View();
    }

[HttpPost]
public ActionResult Create(Quata Quata)
    {
        //save the data 
    }
强类型视图:(使用Razor)

@model Quata
@使用(Html.BeginForm()){
夸塔
@LabelFor(model=>model.Service\u ID.ID,“服务”)
@Html.DropDownList(“ID”,String.Empty)

}
假设您的viewmodel有一个所述服务的列表

下面是一些可能对您有用的东西(您可能不需要for循环,编辑器应该消除这一点,但我遇到了一些奇怪的绑定问题)

在指向viewmodel(@model Quata,假设Quata是您的viewmodel)的顶层视图中,有以下代码:

@For i = 0 To Model.DropdownListInput.Count - 1
                Dim iterator = i
                @Html.EditorFor(Function(x) x.DropdownListInput(iterator), "EnumInput")
        Next
在编辑器模板中(在视图文件夹下创建一个子文件夹,此下拉列表将位于编辑器模板中,并根据您的需要命名模板,我的是EnumInput)

在编辑器模板中,应该指向服务模型(@modelservices)的代码类似于以下代码(用适当的变量名替换):

@
@LabelFor(函数(v)v.value,Model.DisplayName)
@
@DropDownListFor(函数(v)v.value,新选择列表(Model.ParamEnums,“ValueForScript”,“EnumValue”),“--请选择一个值--”)
@Html.ValidationMessageFor(函数(v)v.value)
将列表替换为您的列表,将lambda值替换为您的(@Html.DropDownListFor(x=>x.id,New SelectList(x.ServiceList,“id”,“Name”),“--请选择一个值--”)或类似内容


请注意,这段代码是在VB中编写的,但它应该提供一个粗略的指南。

太好了!有一个选项不使用ViewBag,但您需要更改您的模型。真的!这是什么?您能告诉我这个想法吗?ViewModel模式,它允许控制器打包生成适当HTML响应所需的所有信息。请参见:
@For i = 0 To Model.DropdownListInput.Count - 1
                Dim iterator = i
                @Html.EditorFor(Function(x) x.DropdownListInput(iterator), "EnumInput")
        Next
@<div class="editor-label">
    @Html.LabelFor(Function(v) v.value, Model.DisplayName)
</div>
@<div class="editor-field">
    @Html.DropDownListFor(Function(v) v.value, New SelectList(Model.ParamEnums, "ValueForScript", "EnumValue"), "--Please Select A Value--")
    @Html.ValidationMessageFor(Function(v) v.value)
</div>