how to show validation summary with asterisk in mvc?

608 views Asked by At

enter image description here

In this Form used validation summary for errors.

View:-

<div class="col-md-12 table-bordered" style="padding: 20px; margin:10px;">
                            <div class="col-md-12">@Html.ValidationSummary(false,"Please Correct Following details :", new { @class = "text-danger" })</div>
                            <div class="col-md-12" style="padding:10px;">
                                <div class="col-md-6">
                                    <div style="width:95%;">
                                        @Html.LabelFor(Model => Model.ItemName, new { style = "font-weight:bold;", @maxlength = "100" })
                                        @Html.TextBoxFor(Model => Model.ItemName)
                                        <span class="text-danger"> @Html.ValidationMessageFor(Model => Model.ItemName) </span>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div style="width:75%;">
                                        @Html.LabelFor(Model => Model.ActiveProductGroup, new { style = "font-weight:bold;" })
                                        @Html.DropDownListFor(Model => Model.ProductGroupID, new SelectList(ViewBag.ActiveProductGroup, "Value", "Text"), "--Select Value--", new { style = "Width:95%" })
                                        <span class="text-danger"> @Html.ValidationMessageFor(Model => Model.ActiveProductGroup) </span>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-12" style="padding:10px;">
                                <div class="col-md-3">
                                    <div style="width:70%;">
                                        @Html.LabelFor(Model => Model.SequenceNumber, new { style = "font-weight:bold;" })
                                        @Html.TextBoxFor(Model => Model.SequenceNumber, new { style = "text-align:right",  @maxlength = "3" })
                                        <span class="text-danger"> @Html.ValidationMessageFor(Model => Model.SequenceNumber) </span>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div style="width:50%;">
                                        @Html.LabelFor(Model => Model.UnitPrice, new { style = "font-weight:bold;" })
                                        @Html.TextBoxFor(Model => Model.UnitPrice, new { style = "text-align:right", @maxlength = "5" })
                                        <span class="text-danger"> @Html.ValidationMessageFor(Model => Model.UnitPrice) </span>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div style="width:50%;">
                                        @Html.LabelFor(Model => Model.Quantity, new { style = "font-weight:bold;" })
                                        @Html.TextBoxFor(Model => Model.Quantity, new { style = "text-align:right", @maxlength = "5" })
                                        <span class="text-danger"> @Html.ValidationMessageFor(Model => Model.Quantity) </span>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div style="width:80%;">
                                        @Html.LabelFor(Model => Model.EstimatedDeliveryDays, new { style = "font-weight:bold;" })
                                        @Html.TextBoxFor(Model => Model.EstimatedDeliveryDays, new { style = "text-align:right", @maxlength = "2" })
                                        <span class="text-danger"> @Html.ValidationMessageFor(Model => Model.EstimatedDeliveryDays) </span>
                                    </div>
                                </div>
                            </div>
                        </div>

In this used html.validation summary

In this Form it should show asterisk (*) sign below textbox and error in validation summary

how to achieve it?

1

There are 1 answers

0
nainer On

Can't you use the Required attribute in your model?

Example:

public class ClassName{

[Required(ErrorMessage = "* Please enter Item Name")]
public string ItemName {get;set;}

}