Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
SQL
below code is my view am trying to add required field validator in red color on text-boxes( txtValue, txtLocation)..

Note: i have set if condition in this view that you can see below. textboxes are called in ID

HTML
<div class="searchform cf">
@using (Html.BeginForm())
{
<input type="search" name="txtValue" id="txtValue" value="@Session["searched"]" placeholder="Need Service ? ">

<input type="search" name="txtLocation" id="txtLocation" value="@Session["location"]" placeholder=" City or State">

<button type="submit" value="Search" style="margin-top: 5px;">Search</button>
}
</div>
<div style="margin: 35px 0px 0px 90px">

@if (ViewBag.Message == true)
{
<label id="lblMessage" title="Please enter keyword" style="color:red;">Please enter keyword...!</label>
}
else
{
if (Model != null)
{
if (Model.Count() != 0)
{
<div>
<h2 style="font-size: 15px;">Searched for "@Session["searched"]"</h2>
</div>
 <div>
@foreach (var item in Model)
 {
<div class="tiptext">
Map
</div><hr />
<div class="description_body">
^__b>ZipCode <h4 class="des">@item.ZipCode</h4>
@Html.ActionLink("Book An Appointment", "CalendarView", "Appt", new { id = @item.UserID }, null)
@*@Html.ActionLink("Book An Appointment", "Popup", "Search", new { @class = "openDialog", data_dialog_id = "aboutlDialog", data_dialog_title = "Additinal Customer" })*@
</div>
</div>
</div>
</div>
</div>
</div>
}
</div>
}
else
{
<label id="lblErrorMsg" title="Record not fount...!" style="color:red;">Record not found...!</label>
}
}
}
</div>
</div>
Posted

1 solution

Required field validator goes on top of your property in the controller like this:

[Required(ErrorMessage = "Product Name is required")]
[StringLength(100, ErrorMessage = "Must be under 100 characters")]
public string productName { get; set; }

Then where the error needs to be displayed, you need to add style with color red.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900