ASP.NET MVC DropDownListFor模型绑定 [英] ASP.NET MVC DropDownListFor Model binding
问题描述
我正在尝试将下拉列表绑定到我的模型,但是出现问题.传递给HttpPost函数的模型是NULL,但仅当我尝试绑定到产品"对象的CategoryID字段时,该模型才为NULL.如果我不尝试绑定到product.CatgeoryID,则IncidentNumber字符串不为null.感谢您的输入.
I am trying to bind a dropdown to my model, but I am having issues. My model that is passed into my HttpPost function is NULL, but only if I try to bind to the "product" object's CategoryID field. If I exclude trying to bind to product.CatgeoryID, then the IncidentNumber string is not null. Thanks for any input.
这是我的模特.
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:
And finally my 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:
In my Post function, I can access product.CategoryID with Request[] like so:
[HttpPost]
public ActionResult BasicIdentificationIndex(ViewModelData product)
{
String tmp = Request["product.CategoryID"];
ViewBag.Categories = GetItems();
return View(product);
}
它返回正确的值.但是此值不会绑定到我的模型中,如果我尝试对产品完全使用DropDownListFor,则ViewModelData产品中的ALL VALUES将为null.
And it returns the right value. But this value will not bind in my model, and if I try to use DropDownListFor at all with product, ALL VALUES in ViewModelData product will be null.
推荐答案
已修复:
这是问题所在.
public ActionResult BasicIdentificationIndex(ViewModelData product)
public class ViewModelData
{
public Employee product { get; set; }
}
ViewModelData中对象的名称与BasicIdentificationIndex()中的PARAMETER相同.这引起了重大问题.我要做的就是更改名称.
The NAME of my object in my ViewModelData was the same as the PARAMETER in BasicIdentificationIndex(). This caused major issues. All I had to do was change the name.
其他非常重要的事情是,当我使用EditorForModel()时,我的下拉列表没有绑定,但是当我使用@ Html.EnumDropDownListFor手动创建它们时,绑定才起作用.很高兴知道.
Something else that is very IMPORTANT is that when I was using EditorForModel(), my dropdowns weren't binding, but when I would manually create them with @Html.EnumDropDownListFor the binding worked. Good to know.
这篇关于ASP.NET MVC DropDownListFor模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!