ASP.net MVC ValidationSummary always being rendered - validation

I've added an ASP.net MVC validation summary and even when the page is first loaded and when ModelState is valid it renders this out...
<div class="validation-summary-valid" data-valmsg-summary="true"><span>Errors</span>
<ul><li style="display:none"></li>
</ul></div>
The text 'Errors' is not hidden! (Its not even styled but that's not the point!)
How do I make it only show the validation summary heading when there's an error?
Cheers, Ian.

The validation-summary-valid CSS class is defined in the default MVC /Content/Site.css file as:
.validation-summary-valid
{
display: none;
}
...do you definitely have a reference to this file in your View?

Related

Can I add a razer view file in my wwwroot folder and use my controller to point to that .cshtml file and pass my payload?

I have a working ASP.NET Core 5.0 MVC application working. I want to know how I can add a razor view file in my wwwroot folder and use my controller to point to that .cshtml file and pass my payload?
I'm wondering if I can adjust my controller class to point to a razor view page that resides in wwwroot? Right now I have my controller class set up like this:
[Route("~/wwwroot/helloWorld/HelloWorld.cshtml")]
public IActionResult Index(string pageId)
{
switch (pageId)
{
case "Foo":
DataModel foo = new DataModel(pageId);
return View(foo);
case "Bar":
DataModel bar = new DataModel(pageId);
return View(bar);
}
return View();
}
I have a hyperlink set up in my Views/Home/Index.cshtml like this:
<ul class="navbar-nav flex-grow-1">
<li class="nav-item" style="display: inline-flex; margin-right: .5em; vertical-align:top">
#Html.ActionLink("Home", "Index", "Home")
</li>
<li class="nav-item" style="display: inline-flex; margin-right: .5em; vertical-align:top">
#Html.ActionLink("To Hello World", "Index", "HelloWorld", new { id = "Foo" })
</li>
</ul>
But I get this error every time:
No webpage was found for the web address:
https://localhost:5001/wwwroot/helloWorld/HelloWorld.cshtml?id=Foo
Am I doing something wrong or is this just not possible? If it's not possible, I'd like to understand why. Many thanks in advance.
But I get this error every time:
No webpage was found for the web address:
https://localhost:5001/wwwroot/helloWorld/HelloWorld.cshtml?id=Foo
Am I doing something wrong or is this just not possible? If it's not
possible, I'd like to understand why. Many thanks in advance.
It is possible to return/redirect the html page in the wwwroot folder from the MVC controller, but it is impossible to transfer the data/model from the controller to the static html page.
Static files, such as HTML, CSS, images, and JavaScript, are assets an ASP.NET Core app serves directly to clients by default. When access them, it will go through the UseStaticFiles() middleware, instead of via the asp.net core routing.

How to render html on a given view brefore RenderBody() from the layout is triggered

I'm working on an ASP.NET MVC3 project. I'm using Twitter Bootstrap for my styling (not sure if that's important). The problem I have is that my Index.cshtml view of the Home controller has slightly different layout from the other pages (additional image navigation at the top which I don't show once the user select where he wants to go) but this is causing problems so I remove this part from the Index view to another partial view _ImageNavigation.cshtml and what I want to do is render the content of this partial view when Home/Index.cshtml is opened and I want to render it before the #RenderBody() also independently from it so I get the page the way I want it.
Right now in my _Layout.cshtml I have:
<div id="main">
<div class="container-fluid">
<div class="row">
<div class="col-md-10">#RenderBody() </div>
<div class="col-md-2">
//some static content
</div>
</div>
</div>
So I have two ideas first - adding #RenderPage("~/Views/Shared/_ImageNavigation.cshtml") right before #RenderBody() like :
<div class="row">
#RenderPage("~/Views/Shared/_ImageNavigation.cshtml")
<div class="col-md-10">#RenderBody() </div>
which produces the effect I want, but as you may guess _ImageNavigation is rendered on every page which is not what I want. I want it only on my Home/Index.cshtml so I guess the maybe some kind of check could be made to see what view is loading and render _ImageNavigation only if it's the correct view. Something like :
if (LoadingView == Home/Index.cshtml)
{
#RenderPage("~/Views/Shared/_ImageNavigation.cshtml")
}
Of course the above is just pseudo code, I don't know if it's possible and how to make such a check. And also I wonder if there is a way to do it in the page itself. I tried to put #RenderPage("~/Views/Shared/_ImageNavigation.cshtml") directly in my Home/Index.cshtml but obviously this way the page is rendered as if the code is written directly in the View and not loaded explicitly.
Maybe there's other way. This seems like pretty standard problem but I don't seem to find a proper solution.
When you have a smaller number of exceptions I like to use Sections. Here is a very simlified example:
Layout:
#if (IsSectionDefined("Nav"))
{
RenderSection("Nav")
}
else
{
<nav>default nav</nav>
}
#RenderBody()
Page with alternative nav:
#section Nav
{
<nav>My alternate nav</nav>
}
<div>This is the body for RenderBody</div>
You can read more on Defining Default Content For A Razor Layout Section - Phil Haacked.

Jquery validation plugin dynamically add rules if control exists

I am adding a whole bunch of Jquery validation rules dynamically. I am getting error because some of the textboxes are hidden based on user selection on the page before and therefore do not exist on the page. How do I check if the textbox exists before adding the validation rules?
Here are the validation rules being added:
$('[id$="txtLastName"]').rules('add', {
isSpecialChar: true,
maxlength: 13,
oneOrBothEntered: "txtFacility",
messages: {
oneOrBothEntered: "Either Provider Name or Facility Name must be entered"
}
});
$('[id$="txtFirstName"]').rules('add', {
isSpecialChar: true,
maxlength: 11,
conditionallyRequired: "txtLastName",
messages: {
conditionallyRequired: "This field is required."
}
});
...and a whole bunch more. So say txtLastName was hidden, this code breaks. How do I check first if txtlastname exists before adding the rules?
EDIT:
Per #sparky's comment, adding HTML for fields:
<div class="container1">
<span class="span150Padded">Last</span>
<asp:TextBox ID="txtLastName" runat="server" CssClass="textBoxMedium"></asp:TextBox>
</div>
<div class="container1">
<span class="span150Padded">First</span>
<asp:TextBox ID="txtFirstName" runat="server" CssClass="textBoxMedium"></asp:TextBox>
</div>
The reason I am adding the rules dynamically is because I am using master pages so my fields are renamed. I couldn't use the <%=textbox.ClientID%> method because my validation is in a separate js file which is referenced and called from my aspx page, and a js file won't recongnize something like that. After alot of back and forth, I have found that the cleanest solution was to add the validation rules dynamically.
If you have another suggestion for me, please let me know.
When using the jQuery Validate plugin or any of its methods, the plugin will not target all elements if your selector targets more than one element.
Use the jQuery .each() method to target all elements.
$('[id$="txtLastName"]').each(function() {
$(this).rules('add', {
...
});
});
EDIT:
If you want to avoid using .each(), then target the one element.
$('#justOneField').rules('add', {
...
});
Per the documentation
$("#myform").validate({
ignore: ":hidden"
});

Can I use Kendo MVC helpers inside templates?

I need to use Kendo MVC helper Razor code in template as listed below:
<script id="some-reusable-control" type="text/x-kendo-template">
#(Html.Kendo().Window()
.Name("details-window"))
</script>
But the problem is rendered HTML+JS contains a # (sharp symbol) that is rendered as part of #= # syntax inside template. So I getting 'parse error'.
<div id="details-window" style="display:none"></div><script>
jQuery(function(){jQuery("#details-window
").kendoWindow({animation:false,modal:true,draggable:true /*, etc */ });});
</script>
Could anyone please provide me a solution of how to use Kendo helpers in templates.
To use Kendo UI Widgets as content for a template you could use the ToClientTemplate method.
e.g.
<script id="some-reusable-control" type="text/x-kendo-template">
#(Html.Kendo().Window()
.Name("details-window")
.ToClientTemplate())
</script>

How can i call partial view in my page?

I am working on asp.net mvc3 application.
In my application, User can post their comment and at that time i want to display their post in same page using ajax and jquery. This application is like facebook - we can share post, user can comment to post and all.
I have created two actions
- setcomment
-getcomment
how can i do,
- at page load i want to display textarea and post button to post commnent and
- below that all post of the user will be displayed on that
- and user can post subcomments also.
I have my code working but in that i am using actionlink to go to another page and use refresh button to load page again.
i have
setcomment -
#model <NAMESPACE>.<MODELNAME>
<some code>
getcomment -
#model IEnumerable<<NAMESPACE>.<MODELNAME>>
<some code>
i am not able to call
<div id="showusercomments" style="float: left; width: 75%">
#Html.Partial("_GetUserComment")
</div>
what can i do to solve this problem?
Try using #Html.Action() instead of Html.Partial .
Also see:
my answer here
this SO answer
You've got two options:
<div id="showusercomments" style="float: left; width: 75%">
#Html.Action("_GetUserComment")
</div>
or, get the IEnumerable<...> in the parent page's controller and pass it to the partial:
<div id="showusercomments" style="float: left; width: 75%">
#Html.Partial("_GetUserComment", Model.Comments)
</div>

Resources