Guest User

Untitled

a guest
Jan 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. public partial class MAGAZINE
  2. {
  3. public MAGAZINE()
  4. {
  5. this.AUTHORs = new HashSet<AUTHOR>();
  6. }
  7.  
  8. public long REF_ID { get; set; }
  9. public string NOTES { get; set; }
  10. public string TITLE { get; set; }
  11.  
  12. public virtual REFERENCE REFERENCE { get; set; }
  13. public virtual ICollection<AUTHOR> AUTHORs { get; set; }
  14. }
  15.  
  16. public partial class AUTHOR
  17. {
  18. public AUTHOR()
  19. {
  20. this.MAGAZINEs = new HashSet<MAGAZINE>();
  21. }
  22.  
  23. public long AUTHOR_ID { get; set; }
  24. public string FULL_NAME { get; set; }
  25.  
  26. public virtual ICollection<MAGAZINE> MAGAZINEs { get; set; }
  27. }
  28. }
  29.  
  30. public ActionResult Edit(long id)
  31. {
  32. MAGAZINE magazine = db.MAGAZINEs.Find(id);
  33. return View(magazine);
  34. }
  35.  
  36. [HttpPost]
  37. public ActionResult Edit(MAGAZINE magazine)
  38. {
  39. if (ModelState.IsValid)
  40. {
  41. db.Entry(magazine).State = EntityState.Modified;
  42. db.SaveChanges();
  43. return RedirectToAction("Index");
  44. }
  45.  
  46. return View(magazine);
  47. }
  48.  
  49. public class AuthorModelBinder : IModelBinder
  50. {
  51. public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  52. {
  53. var values = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
  54. if (values != null)
  55. {
  56. // We have specified asterisk (*) as a token delimiter. So
  57. // the ids will be separated by *. For example "2*3*5"
  58. var ids = values.AttemptedValue.Split('*');
  59.  
  60. List<int> validIds = new List<int>();
  61. foreach (string id in ids)
  62. {
  63. int successInt;
  64. if (int.TryParse(id, out successInt))
  65. {
  66. validIds.Add(successInt);
  67. }
  68. else
  69. {
  70. //Make a new author
  71. AUTHOR author = new AUTHOR();
  72. author.FULL_NAME = id.Replace("'", "").Trim();
  73. using (RefmanEntities db = new RefmanEntities())
  74. {
  75. db.AUTHORs.Add(author);
  76. db.SaveChanges();
  77. validIds.Add((int)author.AUTHOR_ID);
  78. }
  79. }
  80. }
  81.  
  82. //Now that we have the selected ids we could fetch the corresponding
  83. //authors from our datasource
  84. var authors = AuthorController.GetAllAuthors().Where(x => validIds.Contains((int)x.Key)).Select(x => new AUTHOR
  85. {
  86. AUTHOR_ID = x.Key,
  87. FULL_NAME = x.Value
  88. }).ToList();
  89. return authors;
  90. }
  91. return Enumerable.Empty<AUTHOR>();
  92. }
  93. }
  94.  
  95. public ActionResult Edit(Post post)
  96. {
  97. if (ModelState.IsValid)
  98. {
  99. repo.EditPost(post);
  100. ...
  101. }
  102. ...
  103. }
  104.  
  105. namespace PrideParrot.Web.Controllers.ModelBinders
  106. {
  107. [ValidateInput(false)]
  108. public class PostBinder : IModelBinder
  109. {
  110. private IRepository repo;
  111.  
  112. public PostBinder(IRepository repo)
  113. {
  114. this.repo = repo;
  115. }
  116.  
  117. #region IModelBinder Members
  118.  
  119. public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  120. {
  121. HttpRequestBase request = controllerContext.HttpContext.Request;
  122.  
  123. // retrieving the posted values.
  124. string oper = request.Form.Get("oper"),
  125. idStr = request.Form.Get("Id"),
  126. heading = request.Form.Get("Heading"),
  127. description = request.Form.Get("Description"),
  128. tagsStr = request.Form.Get("Tags"),
  129. postTypeIdStr = request.Form.Get("PostType"),
  130. postedDateStr = request.Form.Get("PostedDate"),
  131. isPublishedStr = request.Form.Get("Published"),
  132. fileName = request.Form.Get("FileName"),
  133. serialNoStr = request.Form.Get("SerialNo"),
  134. metaTags = request.Form.Get("MetaTags"),
  135. metaDescription = request.Form.Get("MetaDescription"),
  136. themeIdStr = request.Form.Get("Theme");
  137.  
  138. // initializing to default values.
  139. int id = 0, serialNo = 0;
  140. DateTime postedDate = DateTime.UtcNow;
  141. DateTime? modifiedDate = DateTime.UtcNow;
  142. postedDate.AddMilliseconds(-postedDate.Millisecond);
  143. modifiedDate.Value.AddMilliseconds(-modifiedDate.Value.Millisecond);
  144.  
  145. /*if operation is not specified throw exception.
  146. operation should be either'add' or 'edit'*/
  147. if (string.IsNullOrEmpty(oper))
  148. throw new Exception("Operation not specified");
  149.  
  150. // if there is no 'id' in edit operation add error to model.
  151. if (string.IsNullOrEmpty(idStr) || idStr.Equals("_empty"))
  152. {
  153. if (oper.Equals("edit"))
  154. bindingContext.ModelState.AddModelError("Id", "Id is empty");
  155. }
  156. else
  157. id = int.Parse(idStr);
  158.  
  159. // check if heading is not empty.
  160. if (string.IsNullOrEmpty(heading))
  161. bindingContext.ModelState.AddModelError("Heading", "Heading: Field is required");
  162. else if (heading.Length > 500)
  163. bindingContext.ModelState.AddModelError("HeadingLength", "Heading: Length should not be greater than 500 characters");
  164.  
  165. // check if description is not empty.
  166. if (string.IsNullOrEmpty(description))
  167. bindingContext.ModelState.AddModelError("Description", "Description: Field is required");
  168.  
  169. // check if tags is not empty.
  170. if (string.IsNullOrEmpty(metaTags))
  171. bindingContext.ModelState.AddModelError("Tags", "Tags: Field is required");
  172. else if (metaTags.Length > 500)
  173. bindingContext.ModelState.AddModelError("TagsLength", "Tags: Length should not be greater than 500 characters");
  174.  
  175. // check if metadescription is not empty.
  176. if (string.IsNullOrEmpty(metaTags))
  177. bindingContext.ModelState.AddModelError("MetaDescription", "Meta Description: Field is required");
  178. else if (metaTags.Length > 500)
  179. bindingContext.ModelState.AddModelError("MetaDescription", "Meta Description: Length should not be greater than 500 characters");
  180.  
  181. // check if file name is not empty.
  182. if (string.IsNullOrEmpty(fileName))
  183. bindingContext.ModelState.AddModelError("FileName", "File Name: Field is required");
  184. else if (fileName.Length > 50)
  185. bindingContext.ModelState.AddModelError("FileNameLength", "FileName: Length should not be greater than 50 characters");
  186.  
  187. bool isPublished = !string.IsNullOrEmpty(isPublishedStr) ? Convert.ToBoolean(isPublishedStr.ToString()) : false;
  188.  
  189. //** TAGS
  190. var tags = new List<PostTag>();
  191. var tagIds = tagsStr.Split(',');
  192. foreach (var tagId in tagIds)
  193. {
  194. tags.Add(repo.PostTag(int.Parse(tagId)));
  195. }
  196. if(tags.Count == 0)
  197. bindingContext.ModelState.AddModelError("Tags", "Tags: The Post should have atleast one tag");
  198.  
  199. // retrieving the post type from repository.
  200. int postTypeId = !string.IsNullOrEmpty(postTypeIdStr) ? int.Parse(postTypeIdStr) : 0;
  201. var postType = repo.PostType(postTypeId);
  202. if (postType == null)
  203. bindingContext.ModelState.AddModelError("PostType", "Post Type is null");
  204.  
  205. Theme theme = null;
  206. if (!string.IsNullOrEmpty(themeIdStr))
  207. theme = repo.Theme(int.Parse(themeIdStr));
  208.  
  209. // serial no
  210. if (oper.Equals("edit"))
  211. {
  212. if (string.IsNullOrEmpty(serialNoStr))
  213. bindingContext.ModelState.AddModelError("SerialNo", "Serial No is empty");
  214. else
  215. serialNo = int.Parse(serialNoStr);
  216. }
  217. else
  218. {
  219. serialNo = repo.TotalPosts(false) + 1;
  220. }
  221.  
  222. // check if commented date is not empty in edit.
  223. if (string.IsNullOrEmpty(postedDateStr))
  224. {
  225. if (oper.Equals("edit"))
  226. bindingContext.ModelState.AddModelError("PostedDate", "Posted Date is empty");
  227. }
  228. else
  229. postedDate = Convert.ToDateTime(postedDateStr.ToString());
  230.  
  231. // CREATE NEW POST INSTANCE
  232. return new Post
  233. {
  234. Id = id,
  235. Heading = heading,
  236. Description = description,
  237. MetaTags = metaTags,
  238. MetaDescription = metaDescription,
  239. Tags = tags,
  240. PostType = postType,
  241. PostedDate = postedDate,
  242. ModifiedDate = oper.Equals("edit") ? modifiedDate : null,
  243. Published = isPublished,
  244. FileName = fileName,
  245. SerialNo = serialNo,
  246. Theme = theme
  247. };
  248. }
  249.  
  250. #endregion
  251. }
  252. }
Add Comment
Please, Sign In to add comment