validation (datanotation ) for textboxes is not working when I populate dropdownlist in the same page ,when I click on submit button it was showing error instead of showing requirefields.when I comment on dropdownlist in html and actionresult in controller
and debug and run then validation are showing but when I enable dropdown it was redirecting to error.
Model:
publicclassClientProfScreen
{
[Display(Name =
"Client FirstName")]
[Required(ErrorMessage="Client FirstName
is Mandatory")]
publicstring CFirstName {
get;
set; }
publicstring CMiddleName {
get;
set; }
[Required(ErrorMessage =
"Client LastName is Mandatory")]
Description:An
application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.
Details:To enable the details of this specific error message to be viewable on the local server machine, please create a <customErrors> tag within a "web.config"
configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".
What is the error? What is the expected results? Can you share all the relevant code including the model that has the actual data annotations? The dropdown does not have a validation message. Are you expecting a validation message for the dropdown?
I see that you updated your original post. I recommend reading the error message and following the instructions. This will give you the actual .NET error. Commonly when you see the actual error, you can fix the bug in your code. In the future I recommend
using standard .NET exception handling; try...catch blocks.
To enable the details of this specific error message to be viewable on the local server machine, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag
should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off"
And if you set the <customErrors mode="Off"/> you can get the detailed errors, then you can modify your project based on this error message.
Best regards,
Sam
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
134 Points
507 Posts
validations and dropdown
Apr 28, 2020 06:56 PM|emaak|LINK
Hi ,
validation (datanotation ) for textboxes is not working when I populate dropdownlist in the same page ,when I click on submit button it was showing error instead of showing requirefields.when I comment on dropdownlist in html and actionresult in controller and debug and run then validation are showing but when I enable dropdown it was redirecting to error.
Model:
public class ClientProfScreen
{
[Display(Name = "Client FirstName")]
[Required(ErrorMessage="Client FirstName is Mandatory")]
public string CFirstName { get; set; }
public string CMiddleName { get; set; }
[Required(ErrorMessage = "Client LastName is Mandatory")]
[Display(Name = "Client LastName")]
public string CLastName { get; set; }
[DataType(DataType.Date)]
public DateTime DateOfBirth { get; set; }
[Required(ErrorMessage = "Please select Account")]
public string AccountStatus { get; set; }
public static IEnumerable GetAccountStatus()
{
List<SelectListItem> AccountStatusOptions = new List<SelectListItem>()
{
new System.Web.Mvc.SelectListItem()
{
Text="Open",
Value = "1"
},
new System.Web.Mvc.SelectListItem()
{
Text="Closed",
Value = "2"
},
};
return AccountStatusOptions;
}
Controller:UserAccess
//[HttpGet]
public ActionResult ClientProfScr()
{
ViewBag.AccountOptions = ClientProfScreen.GetAccountStatus();
return View();
}
[HttpPost]
public ActionResult ClientProfScr(ClientProfScreen clientprofilescreen)
{
return View();
}
@model ClientProf.Models.ClientProfcreen
@{
Layout = null;
}
<h2>ClientProfScr</h2>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Login</title>
<style>
#login-div {
position: absolute;
left: 40%;
top: 40%;
border: 1px solid #ccc;
padding: 10px 10px;
}
</style>
</head>
<body>
<div id="login-div">
@using (Html.BeginForm("ClientProfScr", "UserAccess", FormMethod.Post))
{
<table>
<tr>
<td>@Html.LabelFor(Model => Model.CFirstName)</td>
<td>@Html.EditorFor(Model => Model.CFirstName)</td>
<td>@Html.ValidationMessageFor(Model => Model.CFirstName,"",new {@style = "color:red"})</td>
</tr>
<tr>
<td>@Html.LabelFor(Model => Model.CMiddleName)</td>
<td>@Html.EditorFor(Model => Model.CMiddleName)</td>
</tr>
<tr>
<td>@Html.LabelFor(Model => Model.CLastName)</td>
<td>@Html.EditorFor(Model => Model.CLastName)</td>
<td>@Html.ValidationMessageFor(Model => Model.CLastName, "", new { @style = "color:red" })</td>
</tr><tr>
<td>@Html.LabelFor(Model => Model.DateOfBirth)</td>
<td>@Html.EditorFor(Model => Model.DateOfBirth)</td>
<td>@Html.ValidationMessageFor(Model => Model.DateOfBirth, "", new { @style = "color:red" })</td>
</tr>
<tr>
<td>@Html.LabelFor(Model => Model.AccountStatus)</td>
<td>
<div>
@Html.DropDownListFor(model => model.AccountStatus, ViewBag.AccountStatusOptions as List<SelectListItem>, "--Select AccountStatus--")
</div>
</td></tr>
<tr>
<td>
<input type="submit" value="Login" />
<td></td>
</tr>
</table>
}
</div>
</body>
</html>
Runtime Error
Server Error in '/' Application.
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.
Details: To enable the details of this specific error message to be viewable on the local server machine, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".
All-Star
53041 Points
23627 Posts
Re: validations and dropdown
Apr 28, 2020 07:14 PM|mgebhard|LINK
What is the error? What is the expected results? Can you share all the relevant code including the model that has the actual data annotations? The dropdown does not have a validation message. Are you expecting a validation message for the dropdown?
Member
134 Points
507 Posts
Re: validations and dropdown
Apr 28, 2020 07:27 PM|emaak|LINK
Hi mgebhard,
thank you for replying .updated in the above code.Please check.
All-Star
53041 Points
23627 Posts
Re: validations and dropdown
Apr 28, 2020 07:30 PM|mgebhard|LINK
I see that you updated your original post. I recommend reading the error message and following the instructions. This will give you the actual .NET error. Commonly when you see the actual error, you can fix the bug in your code. In the future I recommend using standard .NET exception handling; try...catch blocks.
Contributor
3370 Points
1409 Posts
Re: validations and dropdown
Apr 29, 2020 03:26 AM|samwu|LINK
Hiemaak,
Try this code in your config file.
And if you set the <customErrors mode="Off"/> you can get the detailed errors, then you can modify your project based on this error message.
Best regards,
Sam