public void AddBoard(CreateBoardFormDto dto) { var board = new Board { Name = dto.Name, Description = dto.Description }; using (var db = new PGSBoardContext()) { db.Boards.Add(board); db.SaveChanges(); } }
public ActionResult CreateBoard([Bind(Prefix = "CreateBoardFormDto")] CreateBoardFormDto dto) //We need bind attribute because: //CreateBoardFormDto is member of ShowBoardsViewModel which is model of Create view, as you can see in this view //in helpers texboxfor we tells ASP that this field is for m.CreateBoardFormDto.Name so ASP will name this input field //CreateBoardFormDto_Name - and there is a problem because CreateBoardFormDto has no field CreateBoardFormDto_Name, it has //only Name field so we need tell ASP that in this form names there is a prefix CreateBoardFormDto { if (this.ModelState.IsValid) //We need to check if our dto is valid (see validation attributes in dto) { this._boardsService.CreateBoard(dto); //If is valid we are calling create board service return(RedirectToAction("Index")); //After it redirect to Index view } var viewModel = this._boardsService.GetCreateBoardViewModel(); //If dto is invalid create viewmodel again viewModel.CreateBoardFormDto = dto; //Pass there invalid dto to not lose information from all fields user filled return(View(viewModel)); //Show Create board view again }
//Method for creating new board public void CreateBoard(CreateBoardFormDto dto) { this.boardsRepository.AddBoard(dto); }