Reputation: 97
There always a Server Error in '/'Application. when I try to use a validation check in the web application.
This is the Model:
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace MVCDemon.Models
{
public class Movie
{
[DisplayName("MovieName")]
[Required(AllowEmptyStrings =false, ErrorMessage ="CanNotBeEmpty")]
public string Name { get; set; }
}
}
This is the Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MVCDemon.Models;
namespace MVCDemon.Controllers
{
public class MovieController : Controller
{
[HttpPost]
public ActionResult Validation(Movie movie)
{
if (ModelState.IsValid)
{
ViewBag.Name = movie.Name;
}
return View();
}
}
}
This is the Html:
<div>
@model MVCDemon.Models.Movie
@using (Html.BeginForm())
{
<div>
@Html.TextBoxFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
</div>
<input type="submit" value="submit" />
}
</div>
<div>
@ViewBag.Name
</div>
And this is for the layout:
I checked the Scripts and I do import the jquery.validation.min.js and jquery.validation.unobtrusive.min.js
Also, I tried to set the config as someone mentioned:
But the error still there.
Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Upvotes: 2
Views: 96
Reputation: 33306
It looks like your form is not being routed to the controller.
You can do this by adding the below parameters to the Html.BeginForm
:
@using (Html.BeginForm("Validation", "Movie", FormMethod.Post))
{
// ...rest of form code
}
The important parameters are actionName
, controllerName
.
Upvotes: 1