How do you get dynamically loaded tabs to work in ASP.Net Core MVC?
1. I have a simple Index.cshtml that uses bootstrap tabs to create two tabs from the a tags on the page. (To test out options, I first copied from https://qawithexperts.com/article/asp.net/bootstrap-tabs-with-dynamic-content-loading-in-aspnet-mvc/176)
2. There is a click event on each tab that uses $.ajax() to call the controller and then set the html of the appropriate div.
3. I have a model with one field, a string that is required.
4. I have the create view that Visual Studio created.
5. When I run it and click the first tab, the controller returns PartialView("FirstTabCreate") and loads into the div and everything looks great.
6. The problem is when clicking the "Create" button.
I am not sure what to return so that this works. I have seen lots of examples online that use dynamically loaded tabs but none of them have models or validation.
Thanks, this does work but it does not solve my issue. Doing this essentially causes the form validation to happen all client side and so my Controller method does not get called. One of the business requirements is to log validation errors in the database, which I was doing in the Controller method when ModelState was not valid.
I'll look to see if there is a way to get all validation errors client side so that I can still log them in the db. It just seems like there should be an easier way to do tabs.
Client-side validation prevents a costly round-trip to the server, and reduces the number of times the server code has to run just to reject the request as invalid.
If you log every single validation error, then unless you're very lucky with your users, I suspect you're going to end up with a "validation errors" database that grows by hundreds of gigabytes every day.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Speed is an important part of this data entry app and so they want to know who keeps making the same mistakes over and over. For example, who keeps forgetting to fill in a phone number. This way they can train users to be more efficient.
So, I get your point, but no, it won't be nearly that bad.
We actually are replacing a Java based web app and the current table for validation errors is big, but not too big.
To send emails from an (old) asp.net website I use the following code :
Protected Sub SendMail(sender As Object, e As System.EventArgs)
Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
Dim smtp As New SmtpClient
smtp.Host = smtpSection.Network.Host
smtp.EnableSsl = smtpSection.Network.EnableSsl
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
smtp.Port = smtpSection.Network.Port
'solution 1 does not work !
'Dim networkCred As NetworkCredential = New Net.NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)
'smtp.Credentials = networkCred
'solution 2 does not work !
'Dim networkCred As New Net.NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)
'smtp.Credentials = networkCred
'solution 3 does work !!!
smtp.Credentials = New Net.NetworkCredential("firstname.lastname@example.org", "zzzzzz")
Using mm As New MailMessage(smtpSection.From, txtTo.Text.Trim())
mm.Subject = txtSubject.Text.Trim()
mm.Body = txtBody.Text.Trim()
mm.IsBodyHtml = False
TextBox2.Text = smtpSection.Network.UserName
TextBox3.Text = smtpSection.Network.Password
TextBox4.Text = smtpSection.Network.Host
TextBox5.Text = smtpSection.Network.Port
TextBox6.Text = smtpSection.Network.EnableSsl
TextBox7.Text = smtpSection.Network.DefaultCredentials
TextBox8.Text = smtpSection.From
TextBox9.Text = txtTo.Text.Trim()
Catch error_t As Exception
TextBox1.Text = error_t.ToString
The web.config section contains this :
<smtp deliveryMethod="Network" from="email@example.com" >
The namespaces loaded are :
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Net.Configuration" %>
Solution 3 works fine but I don't like to show passwords in a page.
When using solution 1 or 2 the mailserver replies it does not relay, so apparently the credentials are not retrieved work properly.
All the other parameters do show properly in the textboxes I designed to test this sub.
Does anyone have a suggestion ?
Two things. ASP.NET code always executes on the server, never the client. That code will never be shown as part of "a page".
The first two attempts you show as "does not work" are because you're making assumptions about what those smtpSection.Network.UserName and smtpSection.Network.Password properties are returning. I would be willing to bet they don't return anything, so you're creating a NetworkCredential with blank username and password.
How do you tell? One word: debugger. Set a breakpoint on the line that creates the NetworkCredential and examine the values of UserName and Password. Chances are good those values are not what you think they are.