[HttpPost] 编辑 ActionMethod 中的数据绑定 (?) 问题
我有一个名为 ArticleAdmin 的视图模型,其中包含一个复选框列表:
public class ArticleAdmin
{
public ArticleAdmin()
{
TopicCheckboxes = new List<TopicCheckbox>();
}
...
public IList<TopicCheckbox> TopicCheckboxes { get; set; }
...
}
ToopicCheckbox 有自己的视图模型类,在单独的文件中定义:
public class TopicCheckbox
{
public bool IsAssociated { get; set; }
public string TopicName { get; set; }
public int TopicId { get; set; }
}
这对于将模型传递到视图中非常有效:
(更新:为了清晰起见,新包含了此 Action 方法)
public ActionResult Edit(int id)
{
//Get the Article entity by id:
var articleEntity = Repository.Articles.Get<Article>(id);
//Map the entity to the viewmodel:
Mapper.CreateMap<Article, ArticleAdmin>();
// 2nd mapping to populate the article's relations to topics:
Mapper.CreateMap<TopicArticle, TopicArticleAdmin>();
var articleData = Mapper.Map<Article, ArticleAdmin>(articleEntity);
//Generate checkboxes (models) to manage associations with topics:
foreach (var topic in Repository.Topics.List())
{
var topicCheckbox = new TopicCheckbox { TopicId = topic.Id, TopicName = topic.Title };
if (Repository.TopicArticles.FindList(x => x.TopicId == topic.Id && x.ArticleId == id).Count() > 0)
topicCheckbox.IsAssociated = true;
//and add them to the viewmodel:
articleData.TopicCheckboxes.Add(topicCheckbox);
}
return View(articleData);
}
...我期望的所有复选框都出现在表单中:
但显然此列表不是模型绑定回 [HttpPost] 的编辑“操作方法。
尽管 TopicCheckboxes 列表已填充到表单中,但 ActionMethod 中的列表为空。
[HttpPost]
public ActionResult Edit(ArticleAdmin articleData)
...articleData.TopicCheckboxes 的计数为 0。
那么如何使模型绑定正常工作,以便在回发时正确填充 ActionMethod 中的复选框列表?
I have a viewmodel called ArticleAdmin that includes a list of checkboxes:
public class ArticleAdmin
{
public ArticleAdmin()
{
TopicCheckboxes = new List<TopicCheckbox>();
}
...
public IList<TopicCheckbox> TopicCheckboxes { get; set; }
...
}
ToopicCheckbox has its own viewmodel class, defined in a separate file:
public class TopicCheckbox
{
public bool IsAssociated { get; set; }
public string TopicName { get; set; }
public int TopicId { get; set; }
}
This works well for passing the model into the view:
(UPDATE: this Action method is newly included for some clarity)
public ActionResult Edit(int id)
{
//Get the Article entity by id:
var articleEntity = Repository.Articles.Get<Article>(id);
//Map the entity to the viewmodel:
Mapper.CreateMap<Article, ArticleAdmin>();
// 2nd mapping to populate the article's relations to topics:
Mapper.CreateMap<TopicArticle, TopicArticleAdmin>();
var articleData = Mapper.Map<Article, ArticleAdmin>(articleEntity);
//Generate checkboxes (models) to manage associations with topics:
foreach (var topic in Repository.Topics.List())
{
var topicCheckbox = new TopicCheckbox { TopicId = topic.Id, TopicName = topic.Title };
if (Repository.TopicArticles.FindList(x => x.TopicId == topic.Id && x.ArticleId == id).Count() > 0)
topicCheckbox.IsAssociated = true;
//and add them to the viewmodel:
articleData.TopicCheckboxes.Add(topicCheckbox);
}
return View(articleData);
}
...all the checkboxes I expect appear in the form:
But apparently this list isn't model-binding back to the [HttpPost] "Edit" ActionMethod.
Even though the TopicCheckboxes list was populated in the form, the list is empty in the ActionMethod.
[HttpPost]
public ActionResult Edit(ArticleAdmin articleData)
... the count of articleData.TopicCheckboxes is 0.
So how do I get model-binding to work properly so that this list of checkboxes back in the ActionMethod is populated correctly on post-back?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已初始化
TopicCheckBoxes
,但未向其中添加元素。查看这个问题,该问题已由Haacked 的文章 和 这个答案,它有一个自定义用于附加列表的 ModelBinder。
You have initialized
TopicCheckBoxes
, but you didn't add elements to it.Check out this question which was answered by Haacked's article and this answer, which has a custom ModelBinder to attach lists.
好的,我主要根据这个问题弄清楚了:自定义模型复杂复合对象的绑定器帮助
因为我现在觉得这可能是一个重复的问题,所以我将删除它,除非有人在第二天左右进来并评论说它是有用的。
关键在于在复选框的输入名称属性中设置数组结构。就我而言,这意味着每个复选框都需要一系列隐藏值:
真正非常重要的字段是第一个隐藏字段:TopicCheckboxes.Index“默认活页夹会查看它以供其自己使用”,并且需要使用每个复选框的值不同。
OK, I've figured it out based largely on this Question: Custom Model Binder for Complex composite objects HELP
As I now feel this is probably a duplicate question, I'll delete it, unless someone comes in over the next day or so and comments that it is otherwise useful.
The key is in setting up an array structure in the input name attributes of the checkboxes. In my case, that means each checkbox needs a series of hidden values:
The really, really important field is the first hidden field: TopicCheckboxes.Index "which the default binder looks at for it's own use", and needs to be repeated with a different value for each checkbox.