I'm checking out Asp.Net Core on .Net core. Scaffolding has created some templates. The default validation for the model is not sufficient, so I've added some extra validation when the object is send to the controller
ViewData.ModelState.AddModelError(nameof(Invoice.InvoiceNr), "Invoice number should be unique.");
Now in the browser I only see that message when #Html.ValidationMessage("InvoiceNr") is added to the cshtml file. Only asp-validation-for="InvoiceNr" does not seem to present any property errors.
It took some time before I figured this out. Can someone shed some light on this why this is, it seems counter-intuitive to me, to have to add 2 lines to show all validation errors.
Thanks!
#Shyju
<div class="form-group">
<label asp-for="InvoiceNr" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="InvoiceNr" class="form-control"/>
<span asp-validation-for="InvoiceNr" class="text-danger" />
</div>
</div>
#GSerg
The reason it is not working is because you are not using the validation-for tag helper correctly. Instead of using the self closing notation, you should use an explicit closing for the span.
Replace
<span asp-validation-for="InvoiceNr" class="text-danger" />
With
<span asp-validation-for="InvoiceNr" class="text-danger"></span>
In short, you do not need to use both the tag helper and the html helper together to the the validation error. With the correct usage of tag helper, you will be able to see the validation error message.
Related
Is there any way to disbale jquery validation on certain input fields inside a form? So that it won't bother me with the 'red text under my input' or whenever I submit my form. I'm searching for a while now but without any succes. I tried using formnovalidate="formnovalidate" from this questions but also without any succes.
Here's my cshtml input code:
<div class="form-group">
<label asp-for="preExposure" class="control-label"></label>
<input asp-for="preExposure" id="preExposureInput" class="form-control" type="text"/>
<span asp-validation-for="preExposure" class="text-danger"></span>
</div>
Although I posted as a comment that it was pretty similar to the linked question, the most important part of that answer is below:
Once you have the jquery validation turned on for the form with the .validate() you can still configure the rules for the individual element that you want, and remove all the rules.
$('#myfield').rules('remove'); // removes all rules for this field
I have made a Asp.net core view and getting data from database is working, but I want to add adding comments option. Comments send almost properly, but while sending a comment I need to pass "samochodId" from my asp.net core model which was passed first.
<div class="form-group">
<label asp-for="Komentarz.Wiadomosc" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Komentarz.Wiadomosc" class="form-control" />
<span asp-validation-for="Komentarz.Wiadomosc" class="text-danger"></span>
<input asp-for="Komentarz.samochodId" class="form-control" />
</div>
</div>
This works, but I need to remove this line:
<input asp-for="Komentarz.samochodId" class="form-control" /> and do the pass automaticly by getting this Id from My model (Model.Samochod.Id) or maybe from the route?.
How Can I do this?
use hidden field <input type="hidden" asp-for="Komentarz.samochodId" /> and make sure it's within the opening and closing of form <form></form> element
Using ASP .Net Core (Version 2.2, NON-MVC)
I have a database field (Car) which contains text, in HTML format.
I am using an ASP .Net "Input" Helper Tag to display the data (read only) on a webpage.
<input asp-for="Car.Description" asp-format="" disabled class="form-control" />
I would like to display the data as Html.Raw, but am unsure how to do so.
I originally thought I could do something like this (use Html.Raw), but this throws an exception:
<input asp-for=#Html.Raw(Car.Description) asp-format="" disabled class="form-control" />
I then thought the "asp-format" field might have some usable options, but I don't see any.
<div class="form-group row">
<div class="col-2">
<label asp-for="Car.Description"></label>
</div>
<div class="col-10">
<input asp-for="Car.Description" asp-format="" disabled class="form-control" />
</div>
</div>
End result is that I would like to have the display "raw" HTML on the screen.
Thanks
If you want to display the field as read only, you may not even need the "input" helper tag. Just displaying the data in the div should work.
Remove the input helper and just display the text in the div as follows:
<div class="form-group row">
<div class="col-2">
<label asp-for="Car.Description"></label>
</div>
<div class="col-10">
#Html.Raw(Car.Description)
</div>
</div>
I've implemented a working datepicker in a form, which is part of my asp.net mvc project. However, I am having trouble obtaining the input value. For the other attributes in the form, I've been using Html.EditorFor(), but in this case, that does not work as it results in two text boxes appearing. I've been unable to find a solution on the Internet and have looked for alternatives to EditorFor, but so far, have not found anything that works.
<div class="form-group">
#Html.Label("date:", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-3">
<input type="text" class="form-control" id="date">
</div>
</div>
Any help would be greatly appreciated.
I'm assuming when you say "having trouble obtaining the input value," you mean that the the "date" param of the posted form's controller action isn't getting populated.
First try TextBoxFor(). If that doesn't work, you may need to use the name attribute in your input element.
<input type="text" class="form-control" id="date" name="date">
If you are not averse to using jQuery, you could just use this line:
var date = $('#date').val();
and then find a way to pass the javascript variable to your controller
An easy solution to this was to add a name attribute to the tag, and pass this as a parameter to the controller method.
i am using mvc 4 application. actually my application it should running correctly by suddenly it showing compilation error and cleared all the temp files also it not working please help me
My error is:
View Code
<div id="page-wrapper">
<div class="row">
<div class="#ViewBag.HideClass">
#ViewBag.Message
</div>
<div class="col-lg-12">
<h2 class="page-header"> User Registration </h2>
<input type="hidden" id="MenuId" value="#(int)MenuDetails.MenuCompanyMaster" />
</div>
<!-- /.col-lg-12 -->
</div>
thank in advane
In your view page try changing this:
value="#(int)MenuDetails.MenuCompanyMaster"
To this:
value="#((int)MenuDetails.MenuCompanyMaster)"
In Razor syntax, an # section followed by a ( will end the C# code at the nearest matching ). In this case, it's just the #(int). The MenuDetails.MenuCompanyMaster section ends up being random other markup. By wrapping everything in an extra () it indicates to the Razor parser that it's all one expression.
However, I'm not certain if that will fully solve the problem that you have.