【问题标题】:Problem with databinding (?) in [HttpPost] Edit ActionMethod[HttpPost] 编辑操作方法中的数据绑定 (?) 问题
【发布时间】:2011-08-17 16:50:57
【问题描述】:

我有一个名为 ArticleAdmin 的视图模型,其中包含一个复选框列表:

public class ArticleAdmin
{
    public ArticleAdmin()
    {
        TopicCheckboxes = new List<TopicCheckbox>();
    }

    ... 

    public IList<TopicCheckbox> TopicCheckboxes { get; set; }

    ...
}

TopicCheckbox 有自己的视图模型类,在单独的文件中定义:

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]“编辑”ActionMethod。

即使在表单中填充了 TopicCheckboxes 列表,但 ActionMethod 中的列表是空的。

[HttpPost]
public ActionResult Edit(ArticleAdmin articleData)

...articleData.TopicCheckboxes 的计数为 0。

那么如何让模型绑定正常工作,以便在 ActionMethod 中正确填充此复选框列表在回发时

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2 model-binding


    【解决方案1】:

    你已经初始化了TopicCheckBoxes,但是你没有给它添加元素。

    查看由Haacked's articlethis answer 回答的this question,它有一个自定义模型绑定器来附加列表。

    【讨论】:

    • 感谢 LordCover。我正在深入研究那个例子,看看我是否能理解它——看起来我需要深入研究 ModelBindingContext 和 ControllerContext。目前,我只想澄清一下(我已经更新了我的问题以反映这一点):填充模型以发送到视图没有问题,问题在于 HttpPost 操作方法上的模型绑定。跨度>
    【解决方案2】:

    好的,我主要根据这个问题弄清楚了:Custom Model Binder for Complex composite objects HELP

    因为我现在觉得这可能是一个重复的问题,所以我会删除它,除非有人在第二天左右进来并且认为它在其他方面有用。

    关键是在复选框的输入名称属性中设置一个数组结构。就我而言,这意味着每个复选框都需要一系列隐藏值:

    <div>
    
        <input type = "checkbox" name="TopicCheckboxes[1].IsAssociated" value = "true"id="topic_1" checked />
    
        <input type = "hidden" name = "TopicCheckboxes.Index" value = "1" />
        <input type = "hidden" name="TopicCheckboxes[1].IsAssociated" value = "false" />
        <input type = "hidden" name = "TopicCheckboxes[1].TopicName" value = "test" />
        <input type = "hidden" name = "TopicCheckboxes[1].TopicId" value = "1" />
        <label for='topic_1'> test </label>
    </div>
    

    真正非常重要的字段是第一个隐藏字段:TopicCheckboxes.Index“默认绑定器查看它以供自己使用”,并且需要为每个复选框使用不同的值重复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多