Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.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# 如何在mvc4中进行dropdownlist绑定_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 如何在mvc4中进行dropdownlist绑定

C# 如何在mvc4中进行dropdownlist绑定,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我的家庭控制器动作如下: public ActionResult Index() { ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application."; List<SelectListItem> oList = new List<SelectListItem>(); oList.Add(new SelectListItem() { Text = "

我的家庭控制器动作如下:

 public ActionResult Index()
 {
   ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
   List<SelectListItem> oList = new List<SelectListItem>();
   oList.Add(new SelectListItem() { Text = "Rest1", Value = "1" });
   oList.Add(new SelectListItem() { Text = "Rest2", Value = "2", Selected=true});
   oList.Add(new SelectListItem() { Text = "Rest3", Value = "3" });
   Person p = new Person() { PossibleSchools = oList };
   return View(p);
 }
在视图中,我有以下代码::

<div class="content-wrapper">
<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
    <h2>@ViewBag.Message</h2>
</hgroup>
<form action="Home/ModelBinding" method="post">
    <p>
  Name :: <input type="text" name="Name"/>
  Restaurant :: @Html.DropDownList("PossibleSchools");
        <input type="submit" />
</p>
</form>    
</div>
我的模型是:

public class Person
{
    public string Name { get; set; }
    public List<SelectListItem> PossibleSchools { get; set; }
}
问题是,每当我尝试调试这个应用程序时,我都会看到名称字段是绑定的,但可能的学校计数是零


因此,dropdownlist未被绑定。

在第一行视图中使用以下代码:

@model Person
<div class="content-wrapper">
<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
    <h2>@ViewBag.Message</h2>
</hgroup>
<form action="Home/ModelBinding" method="post">
    <p>
  Name :: <input type="text" name="Name"/>
  Restaurant :: @Html.DropDownListFor(model => model.Name, Model.PossibleSchools)
        <input type="submit" />
</p>
</form>    
</div>

以下是一个可能会有所帮助的重构版本:

在控制器中:

public ActionResult Index()
{
    ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";   
    Person p = new Person();
    return View(p);
}

public void ModelBinding(Person p)
{
    // Perform action to process the request
}
您的型号:

public class Person
{
    public string Name { get; set; }
    public string SchoolName { get; set; }
    public List<SelectListItem> PossibleSchools 
    { 
        get
        {
            return new List<SelectListItem>()
                {
                    new SelectListItem() { Text = "Rest1", Value = "1" }),
                    new SelectListItem() { Text = "Rest2", Value = "2" }),
                    new SelectListItem() { Text = "Rest3", Value = "3" })
                };
        }
    }
}
最后,在你看来:

@model Person
<div class="content-wrapper">
<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
    <h2>@ViewBag.Message</h2>
</hgroup>
<form action="Home/ModelBinding" method="post">
    <p>
  Name :: @Html.TextBoxFor(m => m.Name)
  Restaurant :: @Html.DropDownListFor(model => model.SchoolName, Model.PossibleSchools)
    <input type="submit" />
</p>
</form>    
</div>
希望这有帮助

@model Person
<div class="content-wrapper">
<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
    <h2>@ViewBag.Message</h2>
</hgroup>
<form action="Home/ModelBinding" method="post">
    <p>
  Name :: @Html.TextBoxFor(m => m.Name)
  Restaurant :: @Html.DropDownListFor(model => model.SchoolName, Model.PossibleSchools)
    <input type="submit" />
</p>
</form>    
</div>