使用多个类别复选框处理插入/更新对象的最佳方法是什么?

时间:2010-11-03 04:47:56

标签: sql asp.net-mvc

我需要一些关于最佳实践的指导。

我有两个SQL表,一个名为Person,其中包含PersonId,PersonName,另一个名为Category,其中包含CategoryId,CategoryName。类别可能非常多,偶尔会添加。

我想要一个创建视图,它显示PersonName的文本框,然后是Category表中每个类别的单个复选框。

用户可以输入名称并根据需要勾选多个类别复选框,然后保存。

是否在Person表中插入PersonName然后在名为PersonCategory的新表中插入每个人/类别组合的条目是正确的方法,该表包含字段PersonId和CategoryId?

如果我这样做,如果在编辑视图中更改类别,我该如何处理更新?这是一个类别可能是针对一个人不受控制的。是否需要删除PersonCategory中的记录?

3 个答案:

答案 0 :(得分:3)

通常,对于这样的事情,我会避免更新。我会从PersonCategory中删除给定人员的所有条目,然后重新插入PersonCategory中保存的条目。

答案 1 :(得分:1)

您的问题的答案是肯定的,在添加/删除的地方添加/删除它们,是的。

这是一个多对多关系,使用链接表以关系方式表示。

答案 2 :(得分:1)

我就是这样做的。我将通过以下内容。

  1. 查看模型
  2. ASP.NET MVC视图(用户界面)
  3. 控制后行动方法
  4. 保存算法
  5. 查看模型

    public class PersonViewModel
    {
        public int Id { get; set; }
    
        public string PersonName { get; set; }
    
        // For rendering Checkboxes in the MVC View
        public IList<SelectListItem> CategoryCheckboxes { get; set; }
    
        // For data binding on Postback (HTTP POST).
        // <input type="checkbox" /> will have the same `name` attribute as this.
        public IList<int> AssociatedCategories { get; set; }
    }
    

    ASP.NET MVC视图

    <div>Name: <%= Html.EditorFor( m => m.PersonName ) %></div>
    
    <fieldset>
        <legend>Associatied Categories</legend>
    
        <% foreach (SelectListItem c in Model.CategoryCheckboxes) { %>
            <!-- 'name' attribute is same as the C# View Model property name -->
            <%= Html.CheckBox( "AssociatedCategories", c.Selected, new Dictionary<string, object> { { "value", c.Value } } ); %>
            <%= Html.Encode( c.Text ) %><br />
        <% } %>
    
    </fieldset>
    

    控制器发布操作方法

    [HttpPost]
    public ActionResult Edit(PersonViewModel viewModel)
    {
        Person p = personService.Update( viewModel ); // persist changes to DB
        if (p == null) // person record doesn't exist in DB
            return RedirectToAction("NotFound");
    
        return RedirectToAction("Edit", new { id = viewModel.Id });
    }
    

    保存算法

    // this is in the `PersonService` class.
    public void Update(PersonViewModel viewModel)
    {
        Person p = personRepository.GetByPrimaryKey( viewModel.Id );
        if (p == null)
            return null;
    
        p.PersonName = viewModel.PersonName;
    
    
        IList<int> existingAssociatedCategories = GetAssociatedCategories( p.PersonId );
        foreach( int i in viewModel.AssociatedCategories )
        {
            if (!existingAssociatedCategories.Contains( i ))
                Associate( p.PersonId, i ); // add NEW association in DB
        }
    
    
        foreach( int k in  existingAssociatedCategories)
        {
            if (!viewModel.AssociatedCategories.Contains( k ))
                DeleteAssociation( p.PersonId, k ); // delete existing association
        }
    
        repository.UpdatePerson( p ); // save changes to DB for Person object
    
        return p;
    }
    



    如果您有任何疑问,请与我联系。

相关问题