Custom validation not working in mvc

Posted by Prabu_Spark under ASP.NET MVC on 6/19/2014 | Points: 10 | Views : 1247 | Status : [Member] | Replies : 7
Hi sir,
I write custom validation in mvc, but it is not working. I shared the code for your reference. Kindly check and give me the solution for this problem.

[Note: My problem is, i cant able to see the error message, if i give fullname without space]


FullNameAttribute.cs
using System;
using System.ComponentModel.DataAnnotations;
namespace MvcApplication1.Models
{
public class FullNameAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
ErrorHandler.WriteError(value.ToString());
var nameComponents = value.ToString().Split(' ');
return nameComponents.Length == 2;


}
}
}

EmpDetails.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using MvcApplication1.Models;
namespace MvcApplication1.Models
{
public class EmpDetails
{
[Required(ErrorMessage = "Please type your name")]
[StringLength(150, ErrorMessage = "You can only add up to 150 characters")]
[FullName(ErrorMessage = "Please type your full name")]
public string From { get; set; }
}
}


With regards,
J.Prabu.
[Email:prbspark@gmail.com]



Responses

Posted by: Goud.Kv on: 6/19/2014 [Member] [MVP] Gold | Points: 25

Up
0
Down
Hi Prabu.,
Please post code of your view engine.
Please use code block while posting code to understand easily

Thanks & Regards,
Krishna

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabu_Spark on: 6/19/2014 [Member] Starter | Points: 25

Up
0
Down
Hi,
I shared the view engine code for your reference.
@model MvcApplication1.Models.EmpDetails


@{
ViewBag.Title = "Index";
}

<h2>Index</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>EmpDetails</legend>

<div class="editor-label">
@Html.LabelFor(model => model.From)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.From)
@Html.ValidationMessageFor(model => model.From)
</div>

<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>


With regards,
J.Prabu.
[Email:prbspark@gmail.com]

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Goud.Kv on: 6/19/2014 [Member] [MVP] Gold | Points: 25

Up
0
Down
Please try this line in your Html.BeginForm .
@Html.ValidationSummary()


And also try replacing scripts with
@Scripts.Render("~/Scripts/jquery.validate.min.js")

@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js")



Thanks & Regards,
Krishna

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabu_Spark on: 6/19/2014 [Member] Starter | Points: 25

Up
0
Down
Hi,
I already added that code, still it is not working. Please give me your mail id, i will send the full source code.

With regards,
J.Prabu.
[Email:prbspark@gmail.com]

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Goud.Kv on: 6/19/2014 [Member] [MVP] Gold | Points: 25

Up
0
Down
If that doesn't works change the editor-field like below,
<div class="editor-field">
@Html.EditorFor(model => model.From, "", "fieldname")
@Html.ValidationMessage("fieldname")
</div>


Thanks & Regards,
Krishna

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 6/20/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
check ur class using ur controller and view correctly

Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 6/20/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

add your code zip format

Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response