0

在我的 MVC3 应用程序中,我有一堆“SubscriptionTariffs”,每个都与许多“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; }
4

1 回答 1

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;
        }
    });
于 2013-02-11T00:16:05.210 回答