Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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# 下拉列表为';行不通_C#_Asp.net Mvc 3_Drop Down Menu - Fatal编程技术网

C# 下拉列表为';行不通

C# 下拉列表为';行不通,c#,asp.net-mvc-3,drop-down-menu,C#,Asp.net Mvc 3,Drop Down Menu,控制器: OnePersonAllInfoViewModel vModel = new OnePersonAllInfoViewModel(); vModel.PreferredContactType = new PreferredContactType(); ViewBag.PrefContactTypes = new SelectList(dbEntities.PreferredContactTypes .Or

控制器:

   OnePersonAllInfoViewModel vModel = new OnePersonAllInfoViewModel();
  vModel.PreferredContactType = new PreferredContactType();


ViewBag.PrefContactTypes = new SelectList(dbEntities.PreferredContactTypes
                                  .OrderBy(pct => pct.PreferredContactTypeID),
                                   "PreferredContactTypeID", "PreferredContactType1",
                                   vModel.PreferredContactType.PreferredContactTypeID);
视图:


@LabelFor(model=>model.PreferredContactType.PreferredContactTypex)
@Html.DropDownListFor(model=>model.PreferredContactType.PreferredContactTypeID,
ViewBag.PrefContactTypes作为选择列表)
我在回邮时收到了这个错误。。。没有具有键“PreferredContactType.PreferredContactTypeID”的“IEnumerable”类型的ViewData项


有什么想法吗?谢谢

在HttpPost控制器操作中,如果重新显示相同的视图,则必须以与GET操作相同的方式重新填充
ViewBag.PrefContactTypes
属性:

[HttpPost]
public ActionResult Process(OnePersonAllInfoViewModel model)
{
    ViewBag.PrefContactTypes = ...
    return View(model);
}
您似乎还定义了一些以ViewModel为后缀的类。这让读者相信,您在应用程序中使用的是视图模型,在下一行中使用的是
ViewBag
。为什么?为什么不充分利用视图模型及其强大的类型呢

就这样,

public class OnePersonAllInfoViewModel
{
     public int PreferredContactTypeID { get; set; }
     public IEnumerable<PreferredContactType> PrefContactTypes { get; set; }
}
然后是一种观点:

@Html.DropDownListFor(
    model => model.PreferredContactTypeID, 
    Model.PrefContactTypes
)
行动后:

[HttpPost]
public ActionResult Index(OnePersonAllInfoViewModel model)
{
    if (!ModelState.IsValid)
    {
        // the model is invalid => we must redisplay the same view =>
        // ensure that the PrefContactTypes property is populated
        model.PrefContactTypes = dbEntities
            .PreferredContactTypes
            .OrderBy(pct => pct.PreferredContactTypeID)
            .ToList();
        return View(model); 
    }

    // the model is valid => use the model.PreferredContactTypeID to do some
    // processing and redirect
    ...

    // Obviously if you need to stay on the same view then you must ensure that 
    // you have populated the PrefContactTypes property of your view model because
    // the view requires it in order to successfully render the dropdown list.
    // In this case you could simply move the code that populates this property
    // outside of the if statement that tests the validity of the model

    return RedirectToAction("Success"); 
}

回发时,是否已使用数据重新绑定viewModel?此错误意味着vModel.PreferredContactType为空(如果在保存更改或验证后执行回邮并返回相同页面,则此错误为正常)。
@Html.DropDownListFor(
    model => model.PreferredContactTypeID, 
    Model.PrefContactTypes
)
[HttpPost]
public ActionResult Index(OnePersonAllInfoViewModel model)
{
    if (!ModelState.IsValid)
    {
        // the model is invalid => we must redisplay the same view =>
        // ensure that the PrefContactTypes property is populated
        model.PrefContactTypes = dbEntities
            .PreferredContactTypes
            .OrderBy(pct => pct.PreferredContactTypeID)
            .ToList();
        return View(model); 
    }

    // the model is valid => use the model.PreferredContactTypeID to do some
    // processing and redirect
    ...

    // Obviously if you need to stay on the same view then you must ensure that 
    // you have populated the PrefContactTypes property of your view model because
    // the view requires it in order to successfully render the dropdown list.
    // In this case you could simply move the code that populates this property
    // outside of the if statement that tests the validity of the model

    return RedirectToAction("Success"); 
}