Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class MAGAZINE
- {
- public MAGAZINE()
- {
- this.AUTHORs = new HashSet<AUTHOR>();
- }
- public long REF_ID { get; set; }
- public string NOTES { get; set; }
- public string TITLE { get; set; }
- public virtual REFERENCE REFERENCE { get; set; }
- public virtual ICollection<AUTHOR> AUTHORs { get; set; }
- }
- public partial class AUTHOR
- {
- public AUTHOR()
- {
- this.MAGAZINEs = new HashSet<MAGAZINE>();
- }
- public long AUTHOR_ID { get; set; }
- public string FULL_NAME { get; set; }
- public virtual ICollection<MAGAZINE> MAGAZINEs { get; set; }
- }
- }
- public ActionResult Edit(long id)
- {
- MAGAZINE magazine = db.MAGAZINEs.Find(id);
- return View(magazine);
- }
- [HttpPost]
- public ActionResult Edit(MAGAZINE magazine)
- {
- if (ModelState.IsValid)
- {
- db.Entry(magazine).State = EntityState.Modified;
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(magazine);
- }
- public class AuthorModelBinder : IModelBinder
- {
- public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
- {
- var values = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
- if (values != null)
- {
- // We have specified asterisk (*) as a token delimiter. So
- // the ids will be separated by *. For example "2*3*5"
- var ids = values.AttemptedValue.Split('*');
- List<int> validIds = new List<int>();
- foreach (string id in ids)
- {
- int successInt;
- if (int.TryParse(id, out successInt))
- {
- validIds.Add(successInt);
- }
- else
- {
- //Make a new author
- AUTHOR author = new AUTHOR();
- author.FULL_NAME = id.Replace("'", "").Trim();
- using (RefmanEntities db = new RefmanEntities())
- {
- db.AUTHORs.Add(author);
- db.SaveChanges();
- validIds.Add((int)author.AUTHOR_ID);
- }
- }
- }
- //Now that we have the selected ids we could fetch the corresponding
- //authors from our datasource
- var authors = AuthorController.GetAllAuthors().Where(x => validIds.Contains((int)x.Key)).Select(x => new AUTHOR
- {
- AUTHOR_ID = x.Key,
- FULL_NAME = x.Value
- }).ToList();
- return authors;
- }
- return Enumerable.Empty<AUTHOR>();
- }
- }
- public ActionResult Edit(Post post)
- {
- if (ModelState.IsValid)
- {
- repo.EditPost(post);
- ...
- }
- ...
- }
- namespace PrideParrot.Web.Controllers.ModelBinders
- {
- [ValidateInput(false)]
- public class PostBinder : IModelBinder
- {
- private IRepository repo;
- public PostBinder(IRepository repo)
- {
- this.repo = repo;
- }
- #region IModelBinder Members
- public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
- {
- HttpRequestBase request = controllerContext.HttpContext.Request;
- // retrieving the posted values.
- string oper = request.Form.Get("oper"),
- idStr = request.Form.Get("Id"),
- heading = request.Form.Get("Heading"),
- description = request.Form.Get("Description"),
- tagsStr = request.Form.Get("Tags"),
- postTypeIdStr = request.Form.Get("PostType"),
- postedDateStr = request.Form.Get("PostedDate"),
- isPublishedStr = request.Form.Get("Published"),
- fileName = request.Form.Get("FileName"),
- serialNoStr = request.Form.Get("SerialNo"),
- metaTags = request.Form.Get("MetaTags"),
- metaDescription = request.Form.Get("MetaDescription"),
- themeIdStr = request.Form.Get("Theme");
- // initializing to default values.
- int id = 0, serialNo = 0;
- DateTime postedDate = DateTime.UtcNow;
- DateTime? modifiedDate = DateTime.UtcNow;
- postedDate.AddMilliseconds(-postedDate.Millisecond);
- modifiedDate.Value.AddMilliseconds(-modifiedDate.Value.Millisecond);
- /*if operation is not specified throw exception.
- operation should be either'add' or 'edit'*/
- if (string.IsNullOrEmpty(oper))
- throw new Exception("Operation not specified");
- // if there is no 'id' in edit operation add error to model.
- if (string.IsNullOrEmpty(idStr) || idStr.Equals("_empty"))
- {
- if (oper.Equals("edit"))
- bindingContext.ModelState.AddModelError("Id", "Id is empty");
- }
- else
- id = int.Parse(idStr);
- // check if heading is not empty.
- if (string.IsNullOrEmpty(heading))
- bindingContext.ModelState.AddModelError("Heading", "Heading: Field is required");
- else if (heading.Length > 500)
- bindingContext.ModelState.AddModelError("HeadingLength", "Heading: Length should not be greater than 500 characters");
- // check if description is not empty.
- if (string.IsNullOrEmpty(description))
- bindingContext.ModelState.AddModelError("Description", "Description: Field is required");
- // check if tags is not empty.
- if (string.IsNullOrEmpty(metaTags))
- bindingContext.ModelState.AddModelError("Tags", "Tags: Field is required");
- else if (metaTags.Length > 500)
- bindingContext.ModelState.AddModelError("TagsLength", "Tags: Length should not be greater than 500 characters");
- // check if metadescription is not empty.
- if (string.IsNullOrEmpty(metaTags))
- bindingContext.ModelState.AddModelError("MetaDescription", "Meta Description: Field is required");
- else if (metaTags.Length > 500)
- bindingContext.ModelState.AddModelError("MetaDescription", "Meta Description: Length should not be greater than 500 characters");
- // check if file name is not empty.
- if (string.IsNullOrEmpty(fileName))
- bindingContext.ModelState.AddModelError("FileName", "File Name: Field is required");
- else if (fileName.Length > 50)
- bindingContext.ModelState.AddModelError("FileNameLength", "FileName: Length should not be greater than 50 characters");
- bool isPublished = !string.IsNullOrEmpty(isPublishedStr) ? Convert.ToBoolean(isPublishedStr.ToString()) : false;
- //** TAGS
- var tags = new List<PostTag>();
- var tagIds = tagsStr.Split(',');
- foreach (var tagId in tagIds)
- {
- tags.Add(repo.PostTag(int.Parse(tagId)));
- }
- if(tags.Count == 0)
- bindingContext.ModelState.AddModelError("Tags", "Tags: The Post should have atleast one tag");
- // retrieving the post type from repository.
- int postTypeId = !string.IsNullOrEmpty(postTypeIdStr) ? int.Parse(postTypeIdStr) : 0;
- var postType = repo.PostType(postTypeId);
- if (postType == null)
- bindingContext.ModelState.AddModelError("PostType", "Post Type is null");
- Theme theme = null;
- if (!string.IsNullOrEmpty(themeIdStr))
- theme = repo.Theme(int.Parse(themeIdStr));
- // serial no
- if (oper.Equals("edit"))
- {
- if (string.IsNullOrEmpty(serialNoStr))
- bindingContext.ModelState.AddModelError("SerialNo", "Serial No is empty");
- else
- serialNo = int.Parse(serialNoStr);
- }
- else
- {
- serialNo = repo.TotalPosts(false) + 1;
- }
- // check if commented date is not empty in edit.
- if (string.IsNullOrEmpty(postedDateStr))
- {
- if (oper.Equals("edit"))
- bindingContext.ModelState.AddModelError("PostedDate", "Posted Date is empty");
- }
- else
- postedDate = Convert.ToDateTime(postedDateStr.ToString());
- // CREATE NEW POST INSTANCE
- return new Post
- {
- Id = id,
- Heading = heading,
- Description = description,
- MetaTags = metaTags,
- MetaDescription = metaDescription,
- Tags = tags,
- PostType = postType,
- PostedDate = postedDate,
- ModifiedDate = oper.Equals("edit") ? modifiedDate : null,
- Published = isPublished,
- FileName = fileName,
- SerialNo = serialNo,
- Theme = theme
- };
- }
- #endregion
- }
- }
Add Comment
Please, Sign In to add comment