ASP.NET MVC DropDownListFor模型绑定

时间:2013-05-22 16:13:06

标签: 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; }
}


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; } 
}

最后是我的cshtml:

@{

    using (Html.BeginForm())
    {     

        <h4>Incident Number</h4><input id="txtIncidentNumber" name="IncidentNumber" type="text" />           
        @Html.DropDownListFor(x => x.product.CategoryID, new SelectList(ViewBag.Categories, "ID", "Name"))      


        @* WILL CAUSE IncidentNumber in BasicIdentificationIndex(ViewModelData product) TO BE NULL
        <h4>Incident Number</h4><input id="afasf" name="product.CategoryID" type="text" />
            *@

            <input type="submit" value="Submit" />   
    }
}

修改

在我的Post函数中,我可以使用Request []访问product.CategoryID,如下所示:

  [HttpPost]
    public ActionResult BasicIdentificationIndex(ViewModelData product)
    {
        String tmp = Request["product.CategoryID"];
        ViewBag.Categories = GetItems();    

        return View(product);           
    }

它返回正确的值。但是这个值不会在我的模型中绑定,如果我尝试使用DropDownListFor,那么ViewModelData产品中的所有VALUES都将为null。

2 个答案:

答案 0 :(得分:1)

FIXED:

这是问题所在。

public ActionResult BasicIdentificationIndex(ViewModelData product)

public class ViewModelData
{       
   public Employee product { get; set; } 
}

我的ViewModelData中对象的NAME与BasicIdentificationIndex()中的PARAMETER相同。这引起了重大问题。我所要做的就是更改名称。

修改 其他非常重要的是当我使用EditorForModel()时,我的下拉列表没有绑定,但是当我用@ Html.EnumDropDownList手动创建它们时,绑定工作。很高兴知道。

答案 1 :(得分:0)

以下是在同一视图中填充三个DropDownListFor的示例方法:

<强>视图模型:

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" })

希望这会有所帮助......