通过模型更新集合而不创建新行

时间:2013-02-08 15:51:19

标签: c# asp.net sql asp.net-mvc-3 linq-to-sql

在我的MVC3应用程序中,我有一堆“SubscriptionTariff”,每个“SubscriptionTariff”都与许多“SubscriptionServices”相关。我创建了一个视图,可以编辑这些关税,还可以编辑通过服务模型传递的集合。

问题是,无论何时提交表单,而不是编辑数据库中的现有服务,它都会为每个连接的服务创建一个新行。

这是我的控制器方法:

    /// <summary>
    /// Function Edit
    /// </summary>
    /// <param name="subscriptionViewModel"></param>
    /// <returns></returns>
    [HttpPost]
    public ActionResult Edit(SubscriptionViewModel subscriptionViewModel)
    {
        if (subscriptionViewModel == null) throw new ArgumentException("The value of subscriptionViewModel cannot be null.", "subscriptionViewModel");

        Gateway.Instance.Logger.LogDebug("SubscriptionController Edit: Method entered.");

        using (_ownerManager)
        {
            var mappedSubscription = Mapper.Map<SubscriptionTariff>(subscriptionViewModel);
            _ownerManager.SaveSubscription(mappedSubscription);
        }

        Gateway.Instance.Logger.LogDebug("SubscriptionController Edit: Method exited.");

        return RedirectToAction("Index");
    }

管理器的一部分应该在数据库上运行查询:

    var userTariff = (from s in this.Database.SubscriptionTariffs 
                                  where s.SubscriptionTariffId.Equals(subscriptionTariff.SubscriptionTariffId) 
                                  select s).FirstOrDefault();

    userTariff.SubscriptionTariffId = subscriptionTariff.SubscriptionTariffId;
    userTariff.Name = subscriptionTariff.Name;
    userTariff.Description = subscriptionTariff.Description;
    userTariff.IsActive = subscriptionTariff.IsActive;
    userTariff.PricePerMonth = subscriptionTariff.PricePerMonth;
    userTariff.CurrencyCode = subscriptionTariff.CurrencyCode;
    userTariff.PaymentServiceProvider = subscriptionTariff.PaymentServiceProvider;
    userTariff.IncomingMaxTransactionPerMonth = subscriptionTariff.IncomingMaxTransactionPerMonth;
    userTariff.OutgoingMaxTransactionPerMonth = subscriptionTariff.OutgoingMaxTransactionPerMonth;

    userTariff.SubscriptionServices = subscriptionTariff.SubscriptionServices;

    this.Database.SaveChanges();
    Gateway.Instance.Logger.LogDebug("SaveSubscription method exited");
    return subscriptionTariff.SubscriptionTariffId;

关税正在更新,但我无法获得更新服务。如果有人能帮我一把,我们将不胜感激!

编辑:

可能也很有用,包括ViewModel!

    /// <summary>
    /// 
    /// </summary>
    public int SubscriptionTariffId { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public string Description { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public bool IsActive { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public decimal PricePerMonth { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public string CurrencyCode { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public string PaymentServiceProvider { get; set; }

    /// <summary>
    /// Gets or sets the outgoing max transaction per month.
    /// </summary>
    /// <value>The outgoing max transaction per month.</value>
    public int OutgoingMaxTransactionPerMonth { get; set; }

    /// <summary>
    /// Gets or sets the incoming max transaction per month.
    /// </summary>
    /// <value>The incoming max transaction per month.</value>
    public int IncomingMaxTransactionPerMonth { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public virtual ICollection<SubscriptionService> SubscriptionServices { get; set; }

1 个答案:

答案 0 :(得分:0)

而不是

userTariff.SubscriptionServices = subscriptionTariff.SubscriptionServices;

请尝试

subscriptionTariff.SubscriptionServices.ToList().ForEach(ss =>
    {
        // get the instance of the correct subscriptionTariff using the
        // id (IdOfSubscriptionServices)
        var u = userTariff.SubscriptionServices.FirstOrDefault(e => e.IdOfSubscriptionServices == ss.IdOfSubscriptionServices);

        if (u != null)
        {
            // update each property
            u.Prop1 = ss.Prop1;
            u.Prop2 = ss.Prop2;
        }
    });