MVC ASP 应用程序中的 DropDownListFor

DropDownListFor in an MVC ASP application

我有一个视图模型,您在下面看到

public class AssetCategoryViewModel : IEnumerable<AssetCategory>
{
    DataModelContext db = new DataModelContext();
    public AssetCategory AssetCategory { get; set; }
    public Guid Id { get; set; }
    public IList<AssetCategory> AssetCategoryList { get; set; }
    public IEnumerable<SelectListItem> AssetCategoryNames { get; set; }
    public AssetCategoryViewModel()
    {
        AssetCategoryList = (from x in db.AssetCategory
                             select x).ToList();
        AssetCategoryNames = (from x in db.AssetCategory
                              select new SelectListItem
                              {
                                  Text = x.Name,
                                  Value = x.Code
                              });
    }

    public IEnumerator<AssetCategory> GetEnumerator()
    {
        return AssetCategoryList.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

我想将 SelectlistItem 发送到以查看使用下拉列表。

操作方法:

    public ActionResult AddNewCategory()
    {
        AssetCategoryViewModel acvm = new AssetCategoryViewModel();

        return View(acvm);
    }

和div class(包括辅助方法的下拉列表)

                <div class="form-group">
                    @Html.LabelFor(model => model.AssetCategory.MasterCategory, htmlAttributes: new { @class = "control-label" })
                    @Html.DropDownListFor(model => model.AssetCategoryNames, Model.AssetCategoryNames, "Seçim yapınız", htmlAttributes: new { @class = "form-control" })                        
                </div>

我收到错误

Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

我卡在这上面了。

我建议你这样做来制作下拉列表 您不使用视图模型,而是创建一个模型 class 并添加一个从数据库中检索数据的函数

你的模型 class 应该像 ,`

public list<selectListItem> function()
{
    AssetCategoryList = (from x in db.AssetCategory
                                 select x).ToList();
            AssetCategoryNames = (from x in db.AssetCategory
                                  select new SelectListItem
                                  {
                                      Text = x.Name,
                                      Value = x.Code
                                  });`
}
return list;

它应该 return 列表中的值。

那么controller部分应该是

 public ActionResult AddNewCategory()
    {
        AssetCategoryModel acm = new AssetCategoryModel();
viewmodel vm=new viewmodel();
vm.assetcatagorylist=acm.function();

        return View("viewname",vm);
    }

那么您应该通过将视图模型数据传递给您的视图来将数据呈现给视图,

你的观点像

@using projectname.ViewModel
@model ViewModel
<html>

//your design part

     @Html.DropDownListFor(m => m.assetcatagorylist, Model.assetcatagorylist, new { @id ="hi" })


</html>

它工作得很好 干杯,

非常感谢。 我忘了将新模型添加到 actionresult 视图中。 我已经如下更改了控制器部分并且我工作了

    public ActionResult AddNewCategory()
    {
        AssetCategoryViewModel acvm = new AssetCategoryViewModel();
        return View(acvm);
    }

谢谢。