// This is the Insert method to insert the entered DevInfo item // USAGE: <asp:FormView InsertMethod="InsertItem"> public void InsertItem() { _devInfo = new MongoHelper <DevInfo>(); var item = new IMserver.Models.DevInfo(); item.Type = (MyFormView.FindControl("MyDevType") as DropDownList).SelectedValue; item.CompName = (MyFormView.FindControl("MyCompName") as DropDownList).SelectedValue; TryUpdateModel(item); Expression <Func <DevInfo, bool> > ex = p => p.DevID == item.DevID; if (_devInfo.FindBy(ex).Count() > 0) { Response.Write("<script>alert('已存在拥有该设备号的设备!无法添加!');</script>"); return; } else { //取得系统时间作为设备添加时间。但是mongo默认为UTC时间会减去8小时,后取出显示时出错,所以在这加8小时 item.AddTime = DateTime.Now.AddHours(8); if (ModelState.IsValid) { try { _devInfo.Insert(item); } catch (NullReferenceException dbEx) { Response.Write("<script>alert(" + dbEx.Message + ");</script>"); } //_db.DevInfo_view.Add(item); //_db.SaveChanges(); Response.Redirect("Default"); } } }
// This is the Update methd to update the selected DevInfo_view item // USAGE: <asp:FormView UpdateMethod="UpdateItem"> public void UpdateItem(string DevID, string CompName) { ex = p => p.DevID == DevID; var item = _devInfo.FindOneBy(ex); if (item == null) { // The item wasn't found //ModelState.AddModelError("", String.Format("Item with id {0} was not found", DevID)); ModelState.AddModelError("", "找不到该设备"); return; } item.Type = (MyFormView.FindControl("MyDevType") as DropDownList).SelectedValue; item.CompName = (MyFormView.FindControl("MyCompName") as DropDownList).SelectedValue; TryUpdateModel(item); if (ModelState.IsValid) { _devInfo.Update(item); } Response.Redirect("../Default"); }
protected void Page_Load(object sender, EventArgs e) { (MyFormView.FindControl("MyDevType") as DropDownList).SelectedValue = ViewState["Type"].ToString(); (MyFormView.FindControl("MyCompName") as DropDownList).SelectedValue = ViewState["Comp"].ToString(); }