为什么当我插入新数据时我的代码删除firebase中的所有数据

问题描述

我正在做一个mvc项目。我已经完成了插入数据编码。但是,当我想在现有零件上添加评级模块时。它将删除firebase中的所有数据。

这是我的控制器上的示例代码

 [HttpGet]
        public ActionResult GiveRate(string id)
        {
            client = new FireSharp.FirebaseClient(config);
            FirebaseResponse response = client.Get("Products/" + id);
            Product data = JsonConvert.DeserializeObject<Product>(response.Body);
            return View(data);
        }

        [HttpPost]
        public ActionResult GiveRate(Product product)
        {
            client = new FireSharp.FirebaseClient(config);
            SetResponse response = client.Set("Products/" + Product.ProdID,product);
            return RedirectToAction("List");
        }

我的班级代码

public class Product
    {
        public string ProdID { get; set; }
        public string ProdName { get; set; }
        public string price { get; set; }
        public string rating { get; set; }
     }

视图中的示例代码

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()



    <div class="form-group">
        @Html.LabelFor(model => model.rating,htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.rating,new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.rating,"",new { @class = "text-danger" })
        </div>
    </div>



    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
}

此过程可以在产品上添加费率,但是单击视图上的按钮后,将删除Firebase中的所有文件。有什么办法解决这个问题?

解决方法

调用Set时,它将使用您传入的内容替换该位置上的所有当前数据。

如果要在现有位置下添加数据,则必须在较低的位置调用Set

client.Set("Products/" + Product.ProdID + "/Total",42);

或者,您必须调用Update而不是Set,如updating data上的Firesharp文档所示。