ASP MVC Razor - FormValidation - #Html.ValidationMessageFor - Editor [duplicate] - asp.net

This question already has an answer here:
attaching jquery validation to replacement element
(1 answer)
Closed 6 years ago.
Validation working on all other elements i used sofar but validation-message for editor does not show up. ModelState has correct error-state,
but no message can be seen on the form.
here is what i have:
MODEL:
[Required(AllowEmptyStrings = false, ErrorMessage = "Diese Feld muss angegeben werden.")]
[StringLength(8000,ErrorMessage = "Feld muss zwischen {2} und {1} Zeichen enthalten.", MinimumLength = 8)]
[DataType(DataType.MultilineText)]
[AllowHtml]
[Display(Name = "Beschreibung")]
public string Description { get; set; }
VIEW:
<div class="form-group">
#Html.LabelFor(model => model.Description, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Description, "", new { #class = "text-danger" })
</div>
</div>
this also does not work:
#Html.ValidationMessageFor(model => model.Description)
when i do this in view:
#Html.ValidationMessageFor(model => model.Description, "test message")
it works - meaning, it shows "test message" - but i want the messages i defined in model...
---- here is some field that works as expected ----
MODEL:
[Required(ErrorMessage = "Dieses Feld muss angegeben werden.")]
[StringLength(25, MinimumLength = 2)]
[Display(Name = "KeyWord")]
public string Keyword { get; set; }
VIEW:
<div class="form-group">
#Html.LabelFor(model => model.Keyword, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Keyword, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Keyword, "", new { #class = "text-danger" })
</div>
</div>
when field is empty and submit attempted, the required message is shown.
when field contains too-less or too many characters, and field is not in focus or submit is attempted, string-length message is shown ..
that's what i would like for the 2nd editor ...
----- end of working field -----
what am i doing wrong ?
thank you
SOLVED:
it was that "jQuery TE" was used to beautify the editor.
thank you #Stephen Muecke and #Varun Vasishtha for pushing me there ;)

There must be a javascript error that is restricting jquery validation to work, try to debug using Firebug in Firefox

Exclude the last "test message":
#Html.ValidationMessageFor(model => model.Description)
See the documentation: https://msdn.microsoft.com/en-us/library/system.web.mvc.html.validationextensions.validationmessagefor%28v=vs.118%29.aspx

Related

ASP.NET MVC Dropdown Default value

Please look at my code. I am trying to load the default value for the drop-down. value is not accepting from the design. I'm loading data to the combobox from, the account controller, I have added list of items to the Tempdata and then in the design I'm assigning the list to the combo box. So here when loading the form I need to set the default value for the combo box drop down list. Please help
This is how I assigning values from the account controller.
List<Request_Types> RequestTyleList = db.Request_Types.Where(r => r.Status == true).ToList();
List<SelectListItem> ReqTypeDropDown = RequestTyleList.Select(r => new SelectListItem { Text = r.Request_Type, Value = r.Id.ToString() }).ToList();
TempData["RequestTyleList"] = ReqTypeDropDown;
This how I called that TempData in the view:
#{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
List<SelectListItem> ReqTypes = (List<SelectListItem>)TempData.Peek("RequestTyleList");
}
And this is where I assigning that values to the Combobox in the view
<div class="col-md-6 col-sm-6">
<div class="form-group row">
#Html.LabelFor(model => model.ReqType, htmlAttributes: new { #class = "control-label col-md-3" })
<div class="col-sm-8">
#Html.DropDownListFor(model => model.ReqType, ReqTypes, new { #class = "js-dropdown" })
#Html.ValidationMessageFor(model => model.ReqType, "", new { #class = "text-danger" })
</div>
</div>
</div>
So I when opening the view, I need to assign the default value to the ReqTypes
Just add the following parameter to assign default value
#Html.DropDownListFor(model => model.ReqType, new SelectList(ReqTypes,"Value","Text","Your_Default_Value"), "Select",new { #class = "js-dropdown" })

Bootstrap "required" class not working for current MVC web app

I'm currently working with an MVC web app. I went in to restyle the Create template and after doing so, I lost the ability to mark required fields with a red asterisk. I don't understand why my code is no longer working. Please help. Code below:
HTML/Razor Syntax
<div class="form-group required col-md-4">
#Html.LabelFor(model => model.Buy1FirstName)
#Html.EditorFor(model => model.Buy1FirstName, new { htmlAttributes = new { #class = "form-control", required = "required" } })
#Html.ValidationMessageFor(model => model.Buy1FirstName, "Please enter a valid first name", new { #class = "text-danger" })
</div>
My CSS/StyleSheet
.form-group .required .control-label:after {
content:" *" !important;
color:red !important;
}
This is what it currently looks like. It should have a red * after the label "First Name".
I believe that there are two small errors here.
The control-label class reference is in your css, but not in the actual html tag.
The css selectors are a little off just as the comments mention. I just tried your code and was able to see the red asterisk with
these changes:
<div class="form-group required col-md-4">
#Html.LabelFor(model => model.Buy1FirstName, new { #class = "control-label" })
#Html.EditorFor(model => model.Buy1FirstName, new { htmlAttributes = new { #class = "form-control", required = "required" } })
#Html.ValidationMessageFor(model => model.Buy1FirstName, "Please enter a valid first name", new { #class = "text-danger" })
.form-group.required .control-label:after {
content: " *" !important;
color: red !important;
}
Notice the only thing I changed was adding the class reference in the html label to "control-label", and removing the space between .form-group and .required in the css section.

Required Validation Not working For DropdownList in MVC

Below View Code. when i click on Create button without Selecting Activity. it is not showing validation message. i am using Required attribute in Model Class
<div class="form-group">
#Html.LabelFor(model => model.TransactionTypeID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.TransactionTypeID, new SelectList(ViewBag.GetT, "TransactionTypeID", "TransactionType"), "--Select--", new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.TransactionTypeID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ActivityID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.ActivityID, new SelectList(ViewBag.GetA, "ActivityID", "ActivityName"), "--Select--", new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ActivityID, "", new { #class = "text-danger" })
</div>
</div>
//Controller code
public ActionResult Create()
{
List<TransactionTypeMaster> T = new DATransactionTypeMaster().GetListAll();
ViewBag.GetT = T;
List<ActivityMaster> A = new DAActivityMaster().GetListAll();
ViewBag.GetA = A;
return View();
}
Can Any One Help on this. Need To Know Where i m going Wrong
when i click on Create button without Selecting Activity. it is not showing validation message. i am using Required attribute in Model Class
try giving the default '--Select--' options in your drop downs a value of 0.
In your model, make sure your drop down value is set to '[Required]' and also give it the attribute:
[Range(1, Int32.MaxValue, ErrorMessage = "Must Select A Value")]
basically letting it only accept values > 0
I think you TransactionTypeID is int, make that Nullable that would fix this issue. Currently it doesn't work because by default it might be sending zero as a value by using null, it would send null when user doesn't touch this field.

convert text to UpperCase in mvc

Hi I am trying to convert the text entered in textbox to uppercase, but I am not getting the output,please help.
#Html.TextBoxFor(model => model.PanNumber, new { #class = "form-control", #onkeyup = "InputToUpper(this);" })
This should work (remove #onkeyup = "InputToUpper(this);" from the TextBoxFor method)
$('#PanNumber').keyup(function() {
var text = $(this).val();
$(this).val(text.toUpperCase());
});
if you are using "#editorfor" text box then try
#Html.EditorFor(model => model.PanNumber.Alias, new {
htmlAttributes = new { #class = "upper-case" }
})

Asp.net mvc how to change the default label text in #html.labelfor

So using annotations my default label for a field is
[Display(Name = "Spent")]
But of course depending on the context the field is displayed in I would like to change it in the View to say "Spent $".
For the label, MVC5 scaffolding generates...
#Html.LabelFor(model => model.Spend Amount, htmlAttributes: new { #class = "control-label col-md-2" })
and so the label is "Spent".
How can I make it "Spent $" in the view..??
Use this overloaded LabelFor method
#Html.LabelFor(model => model.SpendAmount, "Spent $", htmlAttributes: new { #class = "control-label col-md-2" })

Resources