Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.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
C# ASP.NET mvc DropDownList用于控制器的传递类型_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# ASP.NET mvc DropDownList用于控制器的传递类型

C# ASP.NET mvc DropDownList用于控制器的传递类型,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,如何将SelectedItem从DropDownList for传递到公共操作结果索引(IndexViewModel IndexViewModel)。提交后,我有emp=null。 我应该在IndexViewModel.ListOfEmployee=新选择列表(ListOfEmployee,“Number”,“Name”)中放置什么而不是“Number” 控制器: public class HomeController : Controller { // // GET: /Hom

如何将SelectedItem从DropDownList for传递到公共操作结果索引(IndexViewModel IndexViewModel)。提交后,我有
emp=null
。 我应该在
IndexViewModel.ListOfEmployee=新选择列表(ListOfEmployee,“Number”,“Name”)中放置什么而不是
“Number”

控制器:

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        List<Employee> ListOfEmployee = new List<Employee>();
        ListOfEmployee.Add(new Employee { Name = "Jhon", Number = 1 });
        IndexViewModel IndexViewModel = new IndexViewModel();
        IndexViewModel.ListOfEmployee = new SelectList(ListOfEmployee, "Number", "Name");

        return View(IndexViewModel);
    }
    [HttpPost]
    public ActionResult Index(IndexViewModel indexViewModel)
    {
        return View();
    }

}

您应该保留一个简单的值类型属性来存储选定的下拉选项值

public class IndexViewModel
{
    public SelectList ListOfEmployee { get; set; }    
    public int SelectedEmployeeId { get; set; }
}
在你看来

@Html.DropDownListFor(x=>x.SelectedEmployeeId, Model.ListOfEmployee)
在HttpPost操作中,访问此
SelectedEmployeeId
属性,该属性将为您提供与所选姓名对应的号码。如果需要完整的employee对象,应使用编号(id)再次查询db


您应该保留一个简单的值类型属性来存储选定的下拉选项值

public class IndexViewModel
{
    public SelectList ListOfEmployee { get; set; }    
    public int SelectedEmployeeId { get; set; }
}
在你看来

@Html.DropDownListFor(x=>x.SelectedEmployeeId, Model.ListOfEmployee)
在HttpPost操作中,访问此
SelectedEmployeeId
属性,该属性将为您提供与所选姓名对应的号码。如果需要完整的employee对象,应使用编号(id)再次查询db

无法将
元素绑定到复杂对象(typeof
Employee
)的可能重复项。您的属性
emp
必须是type
int
-
public-int-emp{get;set;}
可能的重复项无法将
元素绑定到复杂对象(typeof
Employee
)。您的属性
emp
必须是类型
int
-
public intemp{get;set;}
[HttpPost]
public ActionResult Index(IndexViewModel model)
{
    var selectedNumber= model.SelectedEmployeeId; 
    return View();
}