i have form has need work when javascript disabled, , have validate form, have tag helper so:
<span asp-validation-for="contactname" class="text-danger" />
and in controller:
[httppost] [validateantiforgerytoken] public async task<iactionresult> create(contactviewmodel contact) { if (!modelstate.isvalid) { return view(contact); } // }
here view model:
public class contactviewmodel { public int contactid { get; set; } public datetime contactdate { get; set; } [required(errormessage ="پر کردن این فیلد الزامی میباشد.")] [maxlength(100, errormessage ="حداکثر تعداد کراکتر برای این فیلد نمیتواند بیشتر از صد باشد.")] public string contactname { get; set; } [required(errormessage = "پر کردن این فیلد الزامی میباشد.")] [emailaddress(errormessage ="لطفا یک ایمیل معتبر وارد کنید.")] [maxlength(100, errormessage = "حداکثر تعداد کراکتر برای این فیلد نمیتواند بیشتر از صد باشد.")] public string contactemail { get; set; } [required(errormessage = "پر کردن این فیلد الزامی میباشد.")] [maxlength(4000, errormessage = "حداکثر تعداد کراکتر برای این فیلد نمیتواند بیشتر از چهار هزار باشد.")] public string contactbody { get; set; } [range(1000000000, double.maxvalue, errormessage = "لطفا یک شماره تلفن معتبر وارد نمایید.")] public string contactphone { get; set; } }
when use tag helper validation, validation message doesn't show below textbox if validation fail, when use old html helpers this:
@html.validationmessagefor(model => model.contactname)
it works, wanted know if it's design or bug? worth note validation summary tag helper works if set asp-validation-summary="validationsummary.all"
, want validation message individual text box on form, though?
i realized that
<span asp-validation-for="model.name" class="text-danger"/>
will not render unless add jquery-validation js page. in mvc core, have add calling
@section scripts { @{await html.renderpartialasync("_validationscriptspartial");} }
the partial view _validationscriptspartial has markup
<environment names="development"> <script src="~/lib/jquery-validation/dist/jquery.validate.js"></script> <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
Comments
Post a Comment