Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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 用于模型绑定的ASP.NET MVC DropDownList_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 用于模型绑定的ASP.NET MVC DropDownList

Asp.net mvc 用于模型绑定的ASP.NET MVC DropDownList,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我正在尝试将下拉列表绑定到我的模型,但我遇到了一些问题。传递到HttpPost函数中的模型为NULL,但仅当我尝试绑定到“product”对象的CategoryID字段时。如果排除尝试绑定到product.CatgeoryID,则IncidentNumber字符串不为null。谢谢你的意见 这是我的模型 public class BIFireTypeModel { public int ID { get; set; } public string Name { get; set;

我正在尝试将下拉列表绑定到我的模型,但我遇到了一些问题。传递到HttpPost函数中的模型为NULL,但仅当我尝试绑定到“product”对象的CategoryID字段时。如果排除尝试绑定到product.CatgeoryID,则IncidentNumber字符串不为null。谢谢你的意见

这是我的模型

public class BIFireTypeModel
{
    public int ID { get; set; }
    public string Name { get; set; }
}


public class Product
{
    public int CategoryID { get; set; }
    public Product()
    {
        CategoryID = 0;
    }
}
这是我的控制器:

//
// GET: /BasicIdentification/
[HttpGet]
public ViewResult BasicIdentificationIndex()
{
    Product newproduct = new Product();
    ViewModelData vmd = new ViewModelData();
    vmd.product = newproduct;
    vmd.product.CategoryID = 2; 
    ViewBag.Categories = GetItems(); 
    return View(vmd); 
}        

[HttpPost]
public ActionResult BasicIdentificationIndex(ViewModelData product)
{      
    ViewBag.Categories = GetItems();         
    return View(product);           
}

public List<BIFireTypeModel> GetItems()
{
    List<BIFireTypeModel> categories = new List<BIFireTypeModel>();
    categories.Add(new BIFireTypeModel() { ID = 1, Name = "IA" });
    categories.Add(new BIFireTypeModel() { ID = 2, Name = "Extended Attack" });
    categories.Add(new BIFireTypeModel() { ID = 3, Name = "Large Fire Support" });
    return categories; 
}        

public class ViewModelData
{
    public String IncidentNumber { get; set; }       
    public Product product { get; set; } 
}
它返回正确的值。但此值不会绑定到我的模型中,如果我尝试将DropDownListFor与product一起使用,ViewModelData product中的所有值都将为null。

修复:

问题就在这里

public ActionResult BasicIdentificationIndex(ViewModelData product)

public class ViewModelData
{       
   public Employee product { get; set; } 
}
ViewModelData中对象的名称与BasicIdentificationIndex()中的参数相同。这引起了重大问题。我所要做的就是换个名字

编辑:
另外一个非常重要的问题是,当我使用EditorForModel()时,我的下拉列表没有绑定,但当我使用@Html.enumdropdownlist手动创建它们时,绑定工作正常。很高兴知道。

以下是一个示例方法,在同一视图中填充三个DropDownList for:

视图模型:

public class GroupViewModel
{  
    public IEnumerable<SelectListItem> Schedules { get; set; }
    public int ScheduleId { get; set; }

    public IEnumerable<SelectListItem> Labs { get; set; }
    public int LabId { get; set; }

    public IEnumerable<SelectListItem> Terms { get; set; } 
    public int TermId { get; set; }
}
public ActionResult Create()
{
    //Populate DropDownList binding values
    var model = new GroupViewModel
    {
        //Preselect the Lab with id 2
        //LabId = 2,

        Labs = repository.Labs.Select(c => new SelectListItem
        {
            Value = c.Id.ToString(),
            Text = c.Name
        }),
        Terms = repository.Terms.Select(c => new SelectListItem
        {
            Value = c.Id.ToString(),
            Text = c.Name
        }),
        Schedules = repository.Schedules.Select(c => new SelectListItem
        {
            Value = c.Id.ToString(),
            Text = c.Name
        })
    };
    return PartialView("_Create", model);
}
@Html.DropDownListFor(m => m.LabId, new SelectList(Model.Labs, "Value", "Text"), 
    "Select", new { @class = "selectpicker" })

@Html.DropDownListFor(m => m.ScheduleId, new SelectList(Model.Schedules, "Value", "Text"), 
    "Select", new { @class = "selectpicker" })

@Html.DropDownListFor(m => m.TermId, new SelectList(Model.Terms, "Value", "Text"), 
    "Select", new { @class = "selectpicker" })

查看:

public class GroupViewModel
{  
    public IEnumerable<SelectListItem> Schedules { get; set; }
    public int ScheduleId { get; set; }

    public IEnumerable<SelectListItem> Labs { get; set; }
    public int LabId { get; set; }

    public IEnumerable<SelectListItem> Terms { get; set; } 
    public int TermId { get; set; }
}
public ActionResult Create()
{
    //Populate DropDownList binding values
    var model = new GroupViewModel
    {
        //Preselect the Lab with id 2
        //LabId = 2,

        Labs = repository.Labs.Select(c => new SelectListItem
        {
            Value = c.Id.ToString(),
            Text = c.Name
        }),
        Terms = repository.Terms.Select(c => new SelectListItem
        {
            Value = c.Id.ToString(),
            Text = c.Name
        }),
        Schedules = repository.Schedules.Select(c => new SelectListItem
        {
            Value = c.Id.ToString(),
            Text = c.Name
        })
    };
    return PartialView("_Create", model);
}
@Html.DropDownListFor(m => m.LabId, new SelectList(Model.Labs, "Value", "Text"), 
    "Select", new { @class = "selectpicker" })

@Html.DropDownListFor(m => m.ScheduleId, new SelectList(Model.Schedules, "Value", "Text"), 
    "Select", new { @class = "selectpicker" })

@Html.DropDownListFor(m => m.TermId, new SelectList(Model.Terms, "Value", "Text"), 
    "Select", new { @class = "selectpicker" })

希望这有帮助……

将id更改为id=“IncidentNumber”!默认模型绑定器在请求中将ID作为相同的属性查找,以便在服务器中重新构建模型?当我不使用product.CategoryIDIs此视图强类型为ViewModelData时,IncidentNumber工作正常?为什么不直接使用Html生成字段?正如Fals所说,为什么不使用@Html.TextBoxFor作为输入?@JohnEdwards它应该查找
Id
,但显示
Name
,以方便用户使用