public Task BindModelAsync(ModelBindingContext bindingContext) { if (bindingContext == null) { throw new ArgumentNullException(nameof(bindingContext)); } NoteOrContent noteOrContent = new NoteOrContent() { NotebookId = bindingContext.ValueProvider.GetValue("NotebookId").FirstOrDefault().ToLongByHex(), NoteId = bindingContext.ValueProvider.GetValue("NoteId").FirstOrDefault().ToLongByHex(), UserId = bindingContext.ValueProvider.GetValue("UserId").FirstOrDefault().ToLongByHex(), Title = bindingContext.ValueProvider.GetValue("Title").FirstOrDefault(), Desc = bindingContext.ValueProvider.GetValue("Desc").FirstOrDefault(), Src = bindingContext.ValueProvider.GetValue("Src").FirstOrDefault(), ImgSrc = bindingContext.ValueProvider.GetValue("ImgSrc").FirstOrDefault(), Tags = bindingContext.ValueProvider.GetValue("Tags").FirstOrDefault(), Content = bindingContext.ValueProvider.GetValue("Content").FirstOrDefault(), Abstract = bindingContext.ValueProvider.GetValue("Abstract").FirstOrDefault(), IsNew = bindingContext.ValueProvider.GetValue("IsNew").FirstOrDefault().ToBool(), IsMarkdown = bindingContext.ValueProvider.GetValue("IsMarkdown").FirstOrDefault().ToBool(), FromUserId = bindingContext.ValueProvider.GetValue("FromUserId").FirstOrDefault().ToLongByHex(), IsBlog = bindingContext.ValueProvider.GetValue("IsBlog").FirstOrDefault().ToBool() }; var IsNew = bindingContext.ValueProvider.GetValue("IsNew").FirstOrDefault(); bindingContext.Result = ModelBindingResult.Success(noteOrContent); return(Task.CompletedTask); }
public async Task <JsonResult> UpdateNoteOrContent([ModelBinder(BinderType = typeof(NoteOrContentModelBinder))] NoteOrContent noteOrContent) { var userid = GetUserIdBySession(); var oldNote = noteService.GetNoteById(noteOrContent.NoteId); // 新添加note if (noteOrContent.IsNew.IsValidTrue() && oldNote == null) { var userId = GetUserIdBySession(); // 为共享新建? if (noteOrContent.FromUserId != null) { userId = noteOrContent.FromUserId; } //todo:IsBlog.Value 缺陷 空指针异常 var note = new Note() { UserId = userId, NoteId = noteOrContent.NoteId, NotebookId = noteOrContent.NotebookId, Title = noteOrContent.Title, Src = noteOrContent.Src, Tags = noteOrContent.Tags.ToTagsArray(), Desc = noteOrContent.Desc, ImgSrc = noteOrContent.ImgSrc, IsBlog = noteOrContent.IsBlog.GetValueOrDefault(), IsMarkdown = noteOrContent.IsMarkdown.GetValueOrDefault() }; var noteContent = new NoteContent() { UserId = userId, IsBlog = note.IsBlog, Content = noteOrContent.Content, Abstract = noteOrContent.Abstract }; note = noteService.AddNoteAndContentForController(note, noteContent, userid); return(Json(note, MyJsonConvert.GetLeanoteOptions())); } var noteUpdate = new Note(); var needUpdateNote = false; if (noteOrContent.Desc.IsValid()) { needUpdateNote = true; noteUpdate.Desc = noteOrContent.Desc; } if (noteOrContent.Title.IsValid()) { needUpdateNote = true; noteUpdate.Title = noteOrContent.Title; } if (noteOrContent.ImgSrc.IsValid()) { needUpdateNote = true; noteUpdate.ImgSrc = noteOrContent.ImgSrc; } if (noteOrContent.Tags.IsValid()) { needUpdateNote = true; noteUpdate.Tags = noteOrContent.Tags.ToTagsArray(); } // web端不控制 if (needUpdateNote) { noteService.UpdateNote(userid, noteOrContent.NoteId, noteUpdate, -1); } if (noteOrContent.Content.IsValid()) { noteContentService.UpdateNoteContent(userid, noteOrContent.NoteId, noteOrContent.Content, noteOrContent.Abstract, needUpdateNote, -1, DateTime.Now); } return(Json(true)); }