I am creating a Sitecore 8.2 MVC test website and I am getting a couple of errors and client form validation Issues. Get methods are working fine but the post method is not working. I have exact same test site working fine with ASP.NET MVC but now I am trying to convert into a Sitecore application.
Here is my code
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));
// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css"));
}
}
Here is my controller
public class MoviesController : SitecoreController
{
public ActionResult GetMovieItems()
{
var movieItemsDatasource = Sitecore.Context.Database.GetItem("/sitecore/content/CustomData/Movies");
List<MovieItem> movieItems = new List<MovieItem>();
foreach (var item in movieItemsDatasource.Children)
{
var movieItem = new MovieItem(item as Sitecore.Data.Items.Item);
movieItems.Add(movieItem);
}
//return View("~/Views/MVCDemo/Renderings/Movies/MoviesList.cshtml", movieItems);
return View("~/Views/MVCDemo/Renderings/Movies/MoviesList.cshtml", movieItems);
}
public ActionResult GetByName(string Name)
{
var movieItemsDatasource = Sitecore.Context.Database.SelectItems("/sitecore/content/CustomData/Movies//*[@Title = \"First Movie\"]").FirstOrDefault();
return View("~/Views/MVCDemo/Renderings/Movies/MovieDetail.cshtml", new MovieItem(movieItemsDatasource as Sitecore.Data.Items.Item));
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddNew(MovieItem movieItem)
{
if (ModelState.IsValid)
{
var movieItemsDatasource = Sitecore.Context.Database.SelectItems("/sitecore/content/CustomData/Movies//*[@Title = \"First Movie\"]").FirstOrDefault();
return RedirectToAction("GetMovieItems");
}
return GetDefaultAction();
}
public ActionResult AddNewMovie()
{
return View("~/Views/MVCDemo/Renderings/Movies/AddMovie.cshtml");
}
}
GetMovieItems and GetByName methods are working fine. I have created an AddMovie form
@using Sitecore8MVC.Web.Code
@model MovieItem
<h1>Add NewMovie</h1>
@using(Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.
SitecoreRoute
Name, FormMethod.Post))
{
@Html.Sitecore().FormHandler("Movies", "AddNew")
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Movie</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "asdsa", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ReleaseDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ReleaseDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReleaseDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Genre, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Genre, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Genre, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Rating, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Rating, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Rating, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
When I post my form, validation is not working, here is my Model
public class MovieItem1
{
[Required(ErrorMessage = "Please supply an address")]
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public string Price { get; set; }
public string Rating { get; set; }
}
and
public class MovieItem : CustomItem
{
public MovieItem(Item innerItem) : base(innerItem)
{
}
public string Title { get { return InnerItem["Title"]; } }
public DateTime ReleaseDate { get { return ((Sitecore.Data.Fields.DateField)InnerItem.Fields["Release Date"]).DateTime; } }
public string Genre { get { return InnerItem["Genre"]; } }
public string Price { get { return InnerItem["Price"]; } }
public string Rating { get { return InnerItem["Rating"]; } }
}
Here is the snapshot of all CSS and JS classes loaded through the bundle
and my main.cshtml page
@using Sitecore.Mvc
@using System.Web.Optimization
@* @using Sitecore.Mvc.Analytics.Extensions *@
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
@Html.Sitecore().ViewRendering("~/Views/MVCDemo/Layouts/Metadata.cshtml")
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Html.Sitecore().ControllerRendering("TopNavigation", "GetMenuItems")
<div class="container body-content">
@Html.Sitecore().Placeholder("body")
<hr />
<footer>
<p>© @DateTime.Now.Year - MVC Movie</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
Issues
- Client side validation is not working. (FIXED)
- If I use MoviewItem model, I get
"No parameterless constructor defined for this object."
error, I have implemented dependency injection explained in this article https://medium.com/@structuredcode/replacing-the-default-di-container-in-sitecore-8-2-4e65d4956c99
Update Validation issue has been fixed, client validation issue fixed by adding "ClientValidationEnabled" and "UnobtrusiveJavaScriptEnabled" in web.config file or you can add directly on Layout.cshtml page. The modal issue also got fixed by changing the router as told by Peter Prochazka.
I am still getting "No parameterless constructor defined for this object." after submitting the form which is due to the Modal being inherited from Sitecore CustomItem, If I use other Modal which is not being inherited from CustomItem, It works fine.