Using xval to client side validate forms - c#

I am using ASP.NET MVC2 and to validate the forms i use xVal. It seems like the server side validation works fine, but the client side validation doesnt work or atleast doesn't show up.
The code i use looks like this:
<% using (Html.BeginForm()) {%>
div class="label"><label for="EmailAddress">Email Address</label></div>
<div class="field">
<%= Html.TextBox(Prefix + ".EmailAddress")%>
<%= Html.ValidationMessage(Prefix + ".EmailAddress")%>
</div>
<%}%>
<%= Html.ClientSideValidation<Customer>(Prefix)%>
When i remove the prefix it works fine. But when i remove it only the server side validation works.
Searching on xVal on this side i found this post that looks a bit like the same problem:
Using xval with fields containing periods
But no answers here (yet).
Thanks in advance for the help.

Solved it with the following code:
<% using (Html.BeginForm("ActionName", "Controller")) {%>
div class="label"><label for="EmailAddress">Email Address</label></div>
<div class="field">
<%= Html.TextBox("EmailAddress")%>
<%= Html.ValidationMessage("EmailAddress")%>
</div>
<%}%>
<%= Html.ClientSideValidation<Customer>()%>

Related

How to show list items in columns in ASP web forms

Hi I am trying to show list items in bootstrap columns but this code show list in vertical straight line
<div class="row">
<% var registragionNumberList = AppCalculations.GetInstance().GetRegistrationNumber();%>
<%
foreach (var item in registragionNumberList)
{%>
<div class="col-md-4">
<textarea> item </textarea>
</div>
<%} %>
</div>
please review and correct.
The code that you have provided works fine for me - it shows the results in three different columns so the issue isn't with the code that you have provided. I would guess that the issue is with bootstrap not being loaded correctly, do you have a script tag for bootstrap on the page?
If bootstrap is loaded and you are still seeing the issue then it might be worth seeing if there is some other styling that is causing an issue, try putting your code on an empty page with just boostrap and the code that you have put above.

MailChimp template variables

So far my site has been using html files on my server. Now I want to utilize Mailchimp and it's template.
This is my html file where I dynamically replace the variables using String's replace in C#.
<h1>
<img src="http://john.doe/test.png" border="0" /></h1>
<hr />
<div style="padding-left: 30px;">
<p>
You have joined John Doe, where you can sell art direct to the public.</p>
<h2>Username and password</h2>
<p>
Here's a reminder of your username and password.</p>
<p>
Username:
<% Email %>
<br />Password:
<% Password %>
</p>
<p>
To log in simply go to
John Doe</p>
<h2>Thanks for joining!</h2>
</div>
So <% Email %> and <% Password %> are dynamically populated. How could I achieve the same in MailChimp template? I visited this page http://templates.mailchimp.com/getting-started/template-language/ but it is not clear how can I achieve this.
Sounds like you want merge tags
For example: instead of <% email %> use *|EMAIL|*
http://kb.mailchimp.com/merge-tags/all-the-merge-tags-cheat-sheet for a list of available merge tags. If you want to send out the password, you probably don't want to use mailchimp at all; you probably want to use Mandrill (from the same company). Also consider SendGrid; or just google 'email api'

MvcForm calls Action for a partial view but is displaying as plain text

I'm following the video on Microsoft's Virtual Academy named "Developing ASP.NET MVC 4 Web Applications Jump Start". In part 5: Integrating Javascript and MVC 4 they start showing off how to use PartialViewResults. I've copied the code exactly as they have in the video, however, I'm getting a different result. They don't show some of the code (SessionController) and it was required for me to figure that part out myself, so maybe that's where I'm messing up.
The problem is that my partial view being called by Html.Action is showing up as plain text instead of the HTML. Like below
I've tried Html.Action, Html.RenderAction, Html.Partial and Html.RenderPartial and all seem to give the same results. Any clue what I might be doing wrong here? I don't want to flood this post with a massive list of code, so I'll post code as requested, if necessary.
EDIT: Requested Razor Code*
Step1:
#model MVC_Tutorial2.Models.Session
<h3>
#Model.Title
</h3>
<div>
#Model.Abstract
</div>
#Html.Action("_GetForSession", "Comment", new { sessionID = Model.SessionID })
Step2:
#model IEnumerable<MVC_Tutorial2.Models.Comment>
<div id="comments>
<ul>
#foreach (var comment in Model) {
<li>#comment.Content</li>
}
</ul>
#using (Ajax.BeginForm("_Submit", "Comment", new AjaxOptions() { UpdateTargetId = "comments" })) {
#Html.AntiForgeryToken()
#Html.Action("_CommentForm", new { SessionID = ViewBag.SessionID })
}
</div>
Step3:
#model MVC_Tutorial2.Models.Comment
#Html.HiddenFor(m => m.SessionID)
<div>
#Html.LabelFor(m => m.Content)
#Html.EditorFor(m => m.Content)
</div>
<button type="submit">Submit Comment</button>
You'll get this exact behaviour if you haven't correctly linked in the required scripts. Try adding the following at the top of your main view (not the partial):
<script src="~/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
Obviously make sure to include the correct version of jQuery you have available in your project.
Update
As you already have the scripts there, something else is obviously wrong. First thing I notice is this:
<div id="comments>
Note the missing quotation-mark. As you have UpdateTargetId = "comments", this may be the cause of your problem.

Html.ValidationMessageFor not showing

I have a MVC2 Project using ADO.NET Entity Data Model that I used to generate my view automatically since it has tons of fields. It gave me code like this.
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>
<div class="editor-label">
<%= Html.LabelFor(model => model.Position) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Position) %>
<%= Html.ValidationMessageFor(model => model.Position) %>
</div>
But with a ton more fields.
Here is my Controller code:
MyProjectEntities_Career_Applications dbca = new MyProjectEntities_Career_Applications();
[HttpPost]
public ActionResult Apply(Career_Applications application)
{
if (ModelState.IsValid)
{
//save form
dbca.AddToCareer_Applications(application);
dbca.SaveChanges();
}
else
{
ModelState.AddModelError("", "Application could not be submitted. Please correct the errors below.");
}
//redirect to thank you page
return View("Apply", application);
}
When the ModelState.IsValid fails I see the <%= Html.ValidationSummary(true) %> but none of the Html.ValidationMessageFor's ever show up.
What am I missing?
Turns out the problem was with the CSS. Stupid mistake, ugh!
Thanks for the help regardless.
I think you're missing a Jquery reference or error handling library that you're using.
This also happens often when you include validation script multiple times. (In shared view, and in your view)

Dynamically change style attributes in ASP.NET MVC

I'm reading percentages from a database, then use that number to create a bar showing visual progress. I need to do something like this on my aspx page:
<%
if (ViewData["width"] != null){
<div style="width: <%: ViewData["width"] %>px;"
</div>
}%>
Of course the above method doesn't work, but hopefully you can see what im trying to achieve. How do i change style attributes dynamically in ASP.NET MVC?
You just wrote HTML in the middle of a code block. You need to put your HTML outside the code block.
<% if (ViewData["width"] != null) { %>
<div style="width: <%: ViewData["width"] %>px;"></div>
<% } %>
Alternatively, you could switch to the Razor language which does away with all the <% %>s and allows you to intersperse C# and HTML much easier. It looks like this:
#if (ViewBag.width != null) {
<div style="width: #(ViewBag.width)px;"></div>
}
Your method should work, you just need to put the div code outside of the server block.
<% if (ViewData["width"] != null){ %>
<div style="width: <%: ViewData["width"] %>px;"
</div>
<% } %>

Categories