i have this code which generate checkboxes from my table HairTags, This code works already with my CEATION in my controller, but i woul love to use it in my EDIT, adn i do not knwo how to change my EDIT code in order to use checkboxes in my edit form. Model
namespace HairCollection3.Models
{
public class Creation
{
public string UserId { get; set; }
[Key]
public int CreationId { get; set; }
public string CreationLanguage { get; set; }
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ViewRes.ValidationStrings))]
[Display(Name = "Sex", ResourceType = typeof(ViewRes.Names))]
public string CreationSex { get; set; }
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ViewRes.ValidationStrings))]
[Display(Name = "CreationTitle", ResourceType = typeof(ViewRes.NamesCreation))]
[StringLength(2000)]
[AllowHtml]
public string CreationTitle { get; set; }
public bool CreationVisible { get; set; }
public bool CreationDelete { get; set; }
public DateTime CreationDate { get; set; }
public string CreationIpAdress { get; set; }
public string CreationPhotoBis { get; set; }
public string CreationPhoto750 { get; set; }
public string CreationPhotoReal { get; set; }
public string Creationtag { get; set; }
public virtual ICollection<CreationLike> CreationLikes { get; set; }
}
public class CreationLike
{
public int CreationId { get; set; }
public string UserId { get; set; }
public virtual ApplicationUser User { get; set; }
[Key]
public int CreationLikeId { get; set; }
public DateTime CreationLikeDate { get; set; }
public string CreationLikeIpAdress { get; set; }
public virtual Creation ParentCreation { get; set; }
}
public class HairTag
{
[Key]
public int HairTagId { get; set; }
[Required]
public string HairTagTitle { get; set; }
[Required]
public string HairTagTitleEN { get; set; }
[Required]
public string HairTagTitleIT { get; set; }
[Required]
public string HairTagTitleSP { get; set; }
[Required]
public string HairTagType { get; set; }
[Required]
public int HairTagOrder { get; set; }
}
//CHECKBOXES
public class HairTagModel
{
//[Key]
public int Value { get; set; }
public string Text { get; set; }
public bool IsChecked { get; set; }
}
public class CreationHairTagsModel
{
public CreationHairTagsModel()
{
Creation = new Creation();
}
public Creation Creation { get; set; }
public List<HairTagModel> CreationHairTags { get; set; }
}
}
Controller : CREATE CODE
GET: /Creation/CreationUpload [Authorize] public ActionResult CreationUpload() { var model = new CreationHairTagsModel();
model.CreationHairTags = db.HairTags.Select(x => new HairTagModel { Text = x.HairTagTitle, Value = x.HairTagId }).ToList();
return View(model);
}
// POST: /Creation/CreationUpload[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult CreationUpload(CreationHairTagsModel creationbind, IEnumerable<HttpPostedFileBase> files)
{
if (ModelState.IsValid)
{
EDIT CODE
// GET: /Creation/CreationEdit/5
[Authorize]
public ActionResult CreationEdit(int? id)
{
Creation creation = db.Creations.Find(id);
return View(creation);
}
// POST: /Creation/CreationEdit/5
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public ActionResult CreationEdit([Bind(Include = "CreationId,CreationSex,CreationTitle")] Creation creation)
{
if (ModelState.IsValid)
{
And also i would love to improve my search form with this code in order to have the checkboxes in my form and keep them checked when a user search for something and chechk some of them... Search code :
// Index/
[AllowAnonymous]
public ActionResult Index(string sortOrder, string currentFilter, string searchString, string currentFilterSex, string searchStringSex, int? page)
{
//SORTING
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
//I think you can actually drop the Likes include as it will be included with the Likes.Select include, so maybe try this (although if it doesn't work, use the first example which should):
var creations = db.Creations.Include(m => m.CreationLikes.Select(l => l.User)) as IQueryable<Creation>;
//SEARCH
//Pagination
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
if (!String.IsNullOrEmpty(searchString))
{
creations = creations.Where(r => r.CreationTitle.ToUpper().Contains(searchString.ToUpper()));
}
//Sex
if (searchStringSex == null)
{
searchStringSex = currentFilterSex;
}
ViewBag.currentFilterSex = searchStringSex;
if (!String.IsNullOrEmpty(searchStringSex))
{
creations = creations.Where(r => r.CreationSex.ToUpper().Contains(searchStringSex.ToUpper()));
}
//SEX
//Language
creations = creations.Where(r => r.CreationLanguage.ToUpper().Equals(ViewRes.Shared.Langue.ToUpper()));
//Not hidden by admin and Not deleted by user
creations = creations.Where(r => r.CreationVisible.Equals(true));
creations = creations.Where(r => r.CreationDelete.Equals(false));
//SORTING
creations = creations.OrderByDescending(r => r.CreationDate);
//FIRST 100 SALON
creations = creations.Take(100);
//Pagination
int pageSize = 80;
int pageNumber = (page ?? 1);
return View(creations.ToPagedList(pageNumber, pageSize));
}
please help!!
Aucun commentaire:
Enregistrer un commentaire