Edytor działa, pojawia się wynik końcowy ale tylko w kontekście bieżącej strony gdy chcę zapisać do bazy i wyświetlić gdzieś indziej to nie działa. Nie wiem czemu wydaje mi się, że robię wszystko prawidłowo.
Proszę o zerknięcie i wytknięcie co jest źle nie muszą być gotowe odp ;)
Kontroler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Repository;
using Repository.Models;
using Project_Shop.Models.ViewModels;
using Ganss.XSS;
namespace Project_Shop.Controllers
{
public class ContactController : Controller
{
// Properties
private readonly Provider.Contact.IContact _contactProvider;
// Ctor
public ContactController(Provider.Contact.IContact contactProvider)
{
this._contactProvider = contactProvider;
}
// Methods
public ActionResult Index()
{
ContactViewModel contact = new ContactViewModel();
var model = this._contactProvider.Get();
if(model != null)
{
contact.Id = model.Id;
contact.Content = model.Content;
}
return View(contact);
}
// GET:
public ActionResult Edit()
{
var model = this._contactProvider.Get();
ContactViewModel contact = null;
if (model != null)
{
contact = new ContactViewModel()
{
Id = model.Id,
Content = model.Content
};
}
return View(contact);
}
// POST:
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult Edit([Bind(Include="Id,Content")]ContactViewModel contact)
{
if(ModelState.IsValid)
{
var sanitizer = new HtmlSanitizer();
var contentChecked = sanitizer.Sanitize(contact.Content);
contact.Content = contentChecked;
ContactModel model = new ContactModel()
{
Id = contact.Id,
Content = contact.Content
};
if (this._contactProvider.Get().Id == contact.Id)
this._contactProvider.Edit(model);
else
this._contactProvider.Add(model);
this._contactProvider.SaveChanges();
return RedirectToAction("Index");
}
return View(contact);
}
}
}
Widoki:
//Index:
@model Project_Shop.Models.ViewModels.ContactViewModel
<div class="row">
@if(Model != null)
{
@Html.Raw(Model.Content);
}
</div>
// Edit:
@model Project_Shop.Models.ViewModels.ContactViewModel
@{
ViewBag.Tytul = "Edytuj treść HTML";
}
<h2>Edytuj treść HTML</h2>
<div class="row" style="max-width:800px">
<div class="col-xs-12">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<input type="hidden" name="Id" value="Model.Id" />
<hr />
<h2>Kod HTML:</h2>
@Html.TextAreaFor(model => model.Content)
<div class="form-actions align-right clearfix">
<button type="submit" class="btn btn-primary pull-left">
<i class="ace-icon fa fa-check bigger-110"></i>
Zapisz
</button>
</div>
}
@if (Model != null)
{
<hr />
<h2>Podgląd w HTMLu:</h2>
<hr />
@Html.Raw(Model.Content);
}
</div>
</div>
Modele i kontekst
// Model:
public class ContactModel
{
public int Id { get; set; }
public string Content { get; set; }
}
// ViewModel
public class ContactViewModel
{
[Key]
public int Id { get; set; }
[AllowHtml]
public string Content { get; set; }
}
// Kontekst
// Properties - ułamek
public DbSet<ContactModel> ContactModel { get; set; }
public int SaveChanges()
{
return base.SaveChanges();
}
// Provider
private readonly Repository.IContext _context;
public Contact(Repository.IContext context)
{
this._context = context;
}
public Repository.Models.ContactModel Get()
{
return this._context.ContactModel.FirstOrDefault();
}
public void Add(Repository.Models.ContactModel contact)
{
this._context.ContactModel.Add(contact);
}
public void Edit(Repository.Models.ContactModel contact)
{
this._context.Entry(contact).State = System.Data.Entity.EntityState.Modified;
}
public int SaveChanges()
{
return this._context.SaveChanges();
}
Użyty jest IoC więc nie zamieszczam interfejsów ani bindowania. Wydaje mi się, że może być coś z kontekstem.