CKEditor MVC 3 implementation - asp.net

Learning mvc and I am trying to implement a page with 3 fields Name-Surname-Description
So in my learning example I am loading employees and I should be able to create and edit them.
The description should use CKEditor .
I can load employees
I can save them
However I cannot seem to be able to save the description,such as whatever the user types in the description field. I have seen few examples on the net but none with a solution to download,as I cannot seem to put together. I have found this guy with a cool html helper but cannot seem to be able to put an example together
http://www.andrewbarber.com/post/CKEditor-Html-Helpers-ASPNET-MVC-Razor-Views.aspx
The problems are :
How do you get the value that is typed inside the ckEditor.
In my viewModel the description is null all the time
the ckEditor slow down the creation of the page quite a lot.How can I make it faster? I dont need all the options.
Is there an example using mvc3 out there that I can use as a template.
I have done all the plumbing as follows:
Create.chtml
#model MvcApplicationCKEditorIntegration.Models.EmployeeViewModel
#{
ViewBag.Title = "Create";
}
<h2>
Create</h2>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset>
<legend>EmployeeViewModel</legend>
<div class="editor-label">
#Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FirstName)
#Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LastName)
#Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Email)
#Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PhotoPath)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.PhotoPath)
#Html.ValidationMessageFor(model => model.PhotoPath)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
<textarea class="ckeditor" id="ckeditor" rows="10"></textarea>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
<script type="text/javascript" src="../../ckeditor/ckeditor.js"></script>
EmployeeController
public class EmployeeController : Controller
{
public ActionResult Index()
{
var employeeRepository=new EmployeeRepository();
var employees = employeeRepository.GetAll();
var employeeList = employees.Select(employee => new EmployeeViewModel
{
EmployeeId = employee.EmployeeId,
FirstName = employee.FirstName,
LastName = employee.LastName,
PhotoPath = employee.PhotoPath,
Email = employee.Email,
Description = employee.Description
}).ToList();
return View(employeeList);
}
public ActionResult Create()
{
return View(new EmployeeViewModel());
}
[HttpPost]
public ActionResult Create(EmployeeViewModel vm)
{
if(ModelState.IsValid)
{
var employeeRepository=new EmployeeRepository();
var emp=new Employee
{
FirstName = vm.FirstName,
LastName = vm.LastName,
Description = vm.Description,
Email = vm.Email,
PhotoPath = vm.PhotoPath
};
employeeRepository.Insert(emp);
return RedirectToAction("Index");
}
return View(vm);
}
}
}
Thanks for any suggestions!!!
EDITED EXAMPLE USING CKEditor helper
#using MvcApplicationCKEditorIntegration.Helpers
#model MvcApplicationCKEditorIntegration.Models.EmployeeViewModel
#{
ViewBag.Title = "Create";
}
<h2>
Create</h2>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#Html.CKEditorHeaderScripts()
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset>
<legend>EmployeeViewModel</legend>
<div class="editor-label">
#Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FirstName)
#Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LastName)
#Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Email)
#Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PhotoPath)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.PhotoPath)
#Html.ValidationMessageFor(model => model.PhotoPath)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Description)
</div>
#Html.CKEditorFor(model=>model.Description)
<p>
<input type="submit" value="Create" onclick="#Html.CKEditorSubmitButtonUpdateFunction()" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
<script type="text/javascript" src="../../ckeditor/ckeditor.js"></script>

You aren't actually using the CKEditor helper at all like is described on that blog page (which is my own blog)
The purpose of that helper is that once you have included the code correctly into your project, you can simply do this:
#Html.CKEditorFor(model=>model.Description)
However, you seem to simply be creating a plain-old text area and working with it 'manually' after that. There isn't anything to bind it to your property, as would exist if you had used the helper described in that post.
Also note that you aren't using the code that Updates the text area behind the scenes; so if your model has Required set on the Description field, you will get a client-side validation error the first time you submit an otherwise properly-configured CKEditorFor() This isn't unique to my helper; any bound property that is 'required' needs the bit of Javascript that is mentioned in that blog post, too. I do it as an onclick off the submit button, but you can run that same code anywhere. You just need to include it in the page, which you haven't done.

You might want to try setting the name attribute of the textarea to "Description"
so:
<div class="editor-field">
<textarea class="ckeditor" id="ckeditor" rows="10" name="Description"></textarea>
</div>
if that doesn't work then you might have to use javascript to get the value of what's in the editor and set it in a hidden field before the post.

Related

ASP.NET MVC How to use Ajax.BeginForm?

i try to use Asp.net mvc4 Ajax helper and when i submit form it makes full post back.
Notice that i include all important scripts like Jquery and jquery.unobtrusive-ajax inside head element.
<script src="#Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery-1.9.1.min.js")" type="text/javascript"> </script>
Students Controller
[HttpPost]
public string Create(Students students)
{
if (Request.IsAjaxRequest())
{
if (ModelState.IsValid)
{
db.Students.Add(students);
db.SaveChanges();
// return RedirectToAction("Index");
}
}
return "<h2>Customer updated successfully!</h2>";
}
Create View
#using (Ajax.BeginForm("Create","Students", new AjaxOptions() { HttpMethod= "POST", UpdateTargetId = "fs1" }))
{
<fieldset id="fs1">
<legend>Students</legend>
<div class="editor-label">
#Html.LabelFor(model => model.ST_NAME)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ST_NAME)
#Html.ValidationMessageFor(model => model.ST_NAME)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ST_BIRTH_DATE)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ST_BIRTH_DATE)
#Html.ValidationMessageFor(model => model.ST_BIRTH_DATE)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ST_PHONE)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ST_PHONE)
#Html.ValidationMessageFor(model => model.ST_PHONE)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ST_ADDR)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ST_ADDR)
#Html.ValidationMessageFor(model => model.ST_ADDR)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ST_CLASS)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ST_CLASS)
#Html.ValidationMessageFor(model => model.ST_CLASS)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ST_STAT)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ST_STAT)
#Html.ValidationMessageFor(model => model.ST_STAT)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.LAST_UPDATE_DATE)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LAST_UPDATE_DATE)
#Html.ValidationMessageFor(model => model.LAST_UPDATE_DATE)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.CURRENT_CLASS_GRADE)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.CURRENT_CLASS_GRADE)
#Html.ValidationMessageFor(model => model.CURRENT_CLASS_GRADE)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.CURRENT_CLASS)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.CURRENT_CLASS)
#Html.ValidationMessageFor(model => model.CURRENT_CLASS)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ST_CODE)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ST_CODE)
#Html.ValidationMessageFor(model => model.ST_CODE)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.REG_CLASS)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.REG_CLASS)
#Html.ValidationMessageFor(model => model.REG_CLASS)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.IDNO)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.IDNO)
#Html.ValidationMessageFor(model => model.IDNO)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Notice that when i check if the request is ajax it gave me false.

I get button at the top on asp.net mvc4 Page

I have a following code in View.
I suppose the button should come at the end of field set, but it is coming at the top as shown in the figure.
<fieldset>
<legend>tblCategory</legend>
#Html.HiddenFor(model => model.Id)
<div class="editor-label">
#Html.LabelFor(model => model.CategoryName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.CategoryName)
#Html.ValidationMessageFor(model => model.CategoryName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Description)
#Html.ValidationMessageFor(model => model.Description)
</div>
<input type="submit" value="Save" />
</fieldset>
put it in a <p>:
<p>
<input type="submit" value="Save" />
</p>

how to use submit button with ajax in asp.net

In my Asp mvc project I want to add company to database without refreshing the page.
How can I do it with ajax. I want to submit data and return to the same page without refresh.
this is a part of my code
#using (Html.BeginForm("Create", "Company", FormMethod.Post, new { enctype =
"multipart/form-data" }))
{
#Html.ValidationSummary(true)
<fieldset>
<div class="editor-label">
#Html.LabelFor(model => model.nom_company)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.nom_company)
#Html.ValidationMessageFor(model => model.nom_company)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.desc_company)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.desc_company)
#Html.ValidationMessageFor(model => model.desc_company)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.datedebut_company)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.datedebut_company)
#Html.ValidationMessageFor(model => model.datedebut_company)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.datefin_company)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.datefin_company)
#Html.ValidationMessageFor(model => model.datefin_company)
</div>
<p>
<input type="file" name="file" />
<input type="submit" value="Create" />
</p>
</fieldset>
}
Any help please

Html page doesn't post to a controller

I have class in a model Customer
When I am clicking a submit button on Cshtml page,
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Registration</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Fname)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Fname)
#Html.ValidationMessageFor(model => model.Fname)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Lname)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Lname)
#Html.ValidationMessageFor(model => model.Lname)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Address)
#Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.phoneNo)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.phoneNo)
#Html.ValidationMessageFor(model => model.phoneNo)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Username)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Username)
#Html.ValidationMessageFor(model => model.Username)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Password)
#Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ConfirmPassword)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ConfirmPassword)
#Html.ValidationMessageFor(model => model.ConfirmPassword)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
The page doesn't post to the controller
[HttpPost]
public ViewResult DisplayCustomer(FormCollection Collection)
{
objinfo.Fname = Request.Form["Fname"].ToString();
objinfo.Lname = Request.Form["Lname"].ToString();
objinfo.Address = Request.Form["Address"].ToString();
objinfo.phoneNo = Request.Form["PhoneNo"].ToString();
objinfo.Username = Request.Form["UserName"].ToString();
objinfo.Password = Request.Form["Password"].ToString();
objutility.InsertEmployee(objinfo);
return View("DisplayCustomer");
}
I am not able to get Values into request.form. What particular code I am missing?
1 - you don't need to use Request.Form, or FormCollection .
Your controller(s) should look like this:
// this method should just display the page, not handle the post back
public ViewResult DisplayCustomer(){
return View();
}
// this method will handle the post back
[HttpPost]
public ViewResult DisplayCustomer(Customer model){
// Handle the post back.
if(ModelState.IsValid){
/* Hanlde the submission, at this point "model" should have all the properties, such as model.Fname, model.Lname, etc...
DB.InserOnSubmit(model);
DB.Customers.AddObject(model);
DB.Customers.Add(model);
you get the point
*/
}
}

How to use multiple edit form using Html Helper

I want to make multiple editing page.
But I don't know how to use TextBoxFor(), TextAreaFor(), ValidationMessageFor() in a foreach loop.
#foreach (var note in Model.noteList)
{
using(Html.BeginForm()){
#Html.Hidden("id", note.id);
<div class="userIdArea"><b>#note.userId</b></div>
<div class="noteArea">#note.content</div>
<br />
<div class="editor-label">
#Html.LabelFor(model => model.note.userId)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.note.userId, new { #Value = note.userId })
#Html.ValidationMessageFor(model => model.note.userId)
</div>
<div class="editor-field">
#Html.TextAreaFor(note => note.noteList)
#Html.ValidationMessageFor(model => model.note.content)
</div>
<input type="submit" value="Edit" />
}
}
The code above cannot set the textarea value, and I don't think it's the right way to do that.
EDIT )
I changed code like this,
#foreach (var note in Model.noteList)
{
using(Html.BeginForm()){
#Html.Hidden("note.id", note.id);
<div class="userIdArea"><b>#note.userId</b></div>
<div class="noteArea">#note.content</div>
<br />
<div class="editor-label">
#Html.LabelFor(model => model.note.userId)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => note.userId)
#Html.ValidationMessageFor(model => note.userId)
</div>
<div class="editor-field">
#Html.TextAreaFor(model => note.content)
#Html.ValidationMessageFor(_ => note.content)
</div>
<input type="submit" value="Edit" />
}
}
and I still have problem with using ValidationMessageFor().
I make only one content empty and submit form then it happens like this,
How should I do put ValidationMessage on right place?
[EDIT #2]
Yes, I have create form too in same view,
the View code is like this,
#model MemoBoard.Models.NoteViewModel
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Note</h2>
<br /><br />
#using(Html.BeginForm()){
<div class="editor-label">
#Html.LabelFor(model => model.note.userId)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.note.userId)
#Html.ValidationMessageFor(model => model.note.userId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.note.content)
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.note.content, new { rows = 4})
#Html.ValidationMessageFor(model => model.note.content)
</div>
<input type="submit" value ="Save" />
} #* //End create form *#
<!-- List Area -->
#foreach (var note in Model.noteList)
{
using(Html.BeginForm()){
#Html.Hidden("note.id", note.id);
#Html.EditorForModel()
<div class="userIdArea"><b>#note.userId</b></div>
<div class="noteArea">#note.content</div>
<br />
<div class="editor-label">
#Html.LabelFor(model => model.note.userId)
</div>
<div class="editor-field">
#Html.EditorFor(model => note.userId, new { id = "A"})
#Html.ValidationMessageFor(model => note.userId)
</div>
<div class="editor-field">
#Html.TextAreaFor(model => note.content)
#Html.ValidationMessageFor(model => note.content)
</div>
<input type="submit" value="Edit" />
}
}
And,
Model,
public class Note
{
[Key]
public int id { get; set; }
[Required(ErrorMessage="Content is required")]
[DisplayName("Note")]
public string content { get; set; }
public DateTime date { get; set; }
[Required(ErrorMessage = "User ID is required")]
[DisplayName("User ID")]
public string userId {get; set;}
public Boolean isPrivate { get; set; }
public virtual ICollection<AttachedFile> AttachedFiles { get; set; }
}
View Model,
public class NoteViewModel
{
public IEnumerable<Note> noteList { get; set; }
public Note note { get; set; }
}
Controller,
public ActionResult Index()
{
var notes = unitOfWork.NoteRepository.GetNotes();
return View(new NoteViewModel(){noteList=notes.ToList(), note = new Note()});
}
[HttpPost]
public ActionResult Index(Note note)
{
try
{
if (ModelState.IsValid)
{
unitOfWork.NoteRepository.InsertNote(note);
unitOfWork.Save();
return RedirectToAction("Index");
}
}catch(DataException){
ModelState.AddModelError("", "Unable to save changes. Try again please");
}
var notes = unitOfWork.NoteRepository.GetNotes();
return View(new NoteViewModel() { noteList = notes.ToList(), note = new Note() });
}
You can actually do this if you want (don't need to use the model):
#Html.LabelFor(model => note.userId)
#Html.ValidationMessageFor(model => note.userId)
#Html.ValidationMessageFor(model => note.content)
Sometimes I change the lambda variable name to _, to show that the model is not important (but it's optional if you want):
#Html.LabelFor(_ => note.userId)
#Html.ValidationMessageFor(_ => note.userId)
#Html.ValidationMessageFor(_ => note.content)
Since you are iterating over the "note" var in the for each statement your html helpers would reference that "note" var and not the model. The solution is below:
#foreach (var note in Model.noteList)
{
using(Html.BeginForm()){
#Html.Hidden("id", note.id);
<div class="userIdArea"><b>#note.userId</b></div>
<div class="noteArea">#note.content</div>
<br />
<div class="editor-label">
#Html.LabelFor(note => note.userId)
</div>
<div class="editor-field">
#Html.TextBoxFor(note => note.userId, new { #Value = note.userId })
#Html.ValidationMessageFor(note => note.userId)
</div>
<div class="editor-field">
#Html.TextAreaFor(note => note.noteList)
#Html.ValidationMessageFor(note => note.content)
</div>
<input type="submit" value="Edit" />
}
}

Resources