ASP.NET MVC2 DataAnnotaion Validation within an updatepanel - asp.net-mvc-2

I am currently working on a website using MVC2 and using ASP.NET Ajax to handle moving between pages. Everything is working fine except a page that has a form for the user to fill out that uses DataAnnotations for validation. This form falls within my UpdatePanel and won't conduct server or client side validation, both which I have working if I remove the UpdatePanel.
Is this even possible, or am I missing a step?
Here is a general outline of my code:
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptMgr" runat="server" ScriptMode="Release">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<div class="page">
<div id="main">
<% Html.EnableClientValidation(); %>
<h2>
ContactUs</h2>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Name) %>
<%: Html.ValidationMessageFor(model => model.Name) %>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>

Take a look at Ajax.BeginForm instead of using the update panel.

Related

`WWW::Mechanize Won't Fill Form Out

I am trying to fill out a form on a website using WWW::Mechanize but the form is embedded in a script, not the usual <form> tag
<script>
component_lang["ib4"] = {};
</script>
<script type="text/x-handlebars-template" id="ib4-tpl">
<form class="component component-form" id="input" enctype="multipart/form-data" method="post" action="/" >
<div class="input-group type-firstlast">
<div class="input-label">
<label>Your Name</label>
</div>
<div class="inputs columns">
<div class="column-2 column-first"><input type="text" name="first" placeholder="First Name" value=""></div>
<div class="column-2"><input type="text" name="last" placeholder="Last Name" value=""></div>
</div>
</div>
</form> </script>
<li id="ib4" class="form page-component-has-overflow"></li>
How would I use WWW::Mechanize to fill this form out? The mech-dump utility did not find any forms on the page.
WWW::Mechanize only works on the HTML, it has no Javascript support. For a Mechanize-like module with Javascript support, you have several options.
WWW::Mechanize::Firefox which lets you drive Firefox.
WWW::Scripter with WWW::Scripter::Plugin::JavaScript.
WWW::Selenium to interface to Selenium RC or Selenium::Remote::Driver to interface to Selenium Webdriver.

Angular JS bindings seem to stop working inside of ng-include

I'm using angular js. When I have a snippet that I put in ng-include instead of outright writing the html to the same file, the former doesn't seem to pick up my bindings for my input fields.
In other words, if the following snippet:
<form name="pickupForm" ng-submit="completeCheckoutPickup()" novalidate>
<div class="row checkout-input">
<div class="col-sm-12">
<div class="title">
PICK UP DATE & TIME
</div>
<span class="glyphicon glyphicon-calendar date-icon-color"></span>
<input type="date" ng-model="userDate" required>
<span class="glyphicon glyphicon-time date-icon-color"></span>
<input type="time" ng-model="userTime" required>
</div>
</div>
</form>
is included as
<div ng-include src="'/templates/account/checkout/checkout.pickup.desktop.html'"></div>
The angularjs bindings for the ng-model stop working.
Any ideas?
enter code here

Fire ASP.NET MVC2 client side validation from an anchor tag

I'm changing an existing ASP.NET MVC2 application which uses the MicrosoftMvcValidation.js etc. for client side validation.
<h2>Details</h2>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("Details", "Add", FormMethod.Post, new { id="addForm" }))
{ %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Title) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Title) %>
<%: Html.ValidationMessageFor(model => model.Title) %>
</div>
...
<input type="submit" value="Create" />
The client side validation is working fine and dandy.
However, the designer wants to change the submit button to be a pretty anchor tag. I'm trying to submit the form using the anchor's onclick event i.e:
<a class="add-campaign-button-done" onclick="document.getElementById('addForm').submit();"><span></span></a>
The form submits, but client-side validation no longer works.
With a little bit of jQuery you can do the trick, see this post : Call MVC 3 Client Side Validation Manually for ajax posts for more informations and let me know if you need more help!

How to create a post from iPhone to Rails

Newbie to Rails and iOS here. I have a Rails blog application with Post model that has just a title and and image. I can create a new Post from a browser using the following html.erb code:
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<p>
<%= f.file_field :image %>
</p>
<div class="actions">
<%= f.submit %>
</div>
But how would I do this from an iPhone app? Is there an equivalent form function in iOS?
I can recommend you using ASIHttpRequest, especially the class ASIFormDataRequest. With this you can send form data easily as a POST request.

Even with ASP.NET 3.5, I am getting "Validation of viewstate MAC failed" error

I found a lost of posts to solve this error: "Validation of viewstate MAC failed". I read that it is a ASP.NET 2.0 error which is also a bug. But I am using VS 2008 with ASP.NET 3.5 SP1. Why this error is coming in this version also?
I am using ASP TextBox controls. Some posts have mentioned that ASP TextBoxes generate this error, so I have set AutoPostBack of each TextBox to False.
How to get rid of this mess?
Code Added Below **
* Default.aspx *
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FlexStock._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<!--<%# Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="cc1" %>-->
<link rel="Stylesheet" type="text/css" href="~/Styles/main.css" />
<title>FlexStock#.NET - Gamma Edition</title>
</head>
<body id="LoginPageBody" class="loginPageBodyClass">
<img id="imgProductLogo" src="Styles/stockist_gamma.png" style="text-align:center;text-shadow:black;margin-left:500px" alt="Product Logo"/>
<form id="loginForm" runat="server" class="loginForm" action="./Forms/selectCompany.aspx" >
<div id="divMainLoginPage" runat="server">
<div class="label">
<asp:Label ID="lblUserName" runat="server" CssClass="LabelControls" AssociatedControlID="txtUserName">User Name: </asp:Label>
</div>
<div class="value">
<asp:TextBox ID="txtUserName" runat="server" CssClass="TxtStyle" Width="199px" AutoPostBack="false"></asp:TextBox>
</div>
<div class="label">
<asp:Label ID="lblPassword" runat="server" CssClass="LabelControls" AssociatedControlID="txtPassword">Password: </asp:Label>
</div>
<div class="value">
<asp:TextBox ID="txtPassword" runat="server" CssClass="TxtStyle" TextMode="Password" Width="199px" AutoPostBack="false"></asp:TextBox>
</div>
<div class="label">
<asp:Label ID="lblCaptcha" runat="server" Text="Security Check"></asp:Label>
</div>
<div class="value">
<!-- <cc1:CaptchaControl ID="ccJoin" runat="server" CaptchaBackgroundNoise="none" CaptchaLength="5" CaptchaHeight="60" CaptchaWidth="200" CaptchaLineNoise="None" CaptchaMinTimeout="5" CaptchaMaxTimeout="240" /> -->
</div>
<div class="label">
<asp:Label ID="lblEnterCaptcha" runat="server" Text="Enter Value"></asp:Label>
</div>
<div class="value">
<asp:TextBox ID="txtCaptcha" runat="server" CssClass="TxtStyle" Width="199px" AutoPostBack="false"></asp:TextBox><br /><br />
<asp:Button ID="btnLogin" runat="server" Width="60" Text="Login" CssClass="BtnStyle"/>
</div>
</div>
</form>
<img id="img1" src="Styles/impact_logo.png" style="text-align:right;text-shadow:black;margin-left:800px" />
</body>
</html>
selectCompany.aspx **
<%# Page Title="" Language="C#" MasterPageFile="~/master1.Master" AutoEventWireup="true" CodeBehind="~/Forms/selectCompany.aspx.cs" Inherits="FlexStock.Forms.selectCompany" EnableViewStateMac="false" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<link rel="Stylesheet" type="text/css" href="../Styles/selectCompany.css" />
<link rel="Stylesheet" type="text/css" href="../Styles/main.css" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<form id="frmSelectCompany" enableviewstate="false">
<div id="label" class="label" style="width:300px">
<asp:Label ID="lblSelectCompany" runat="server" Text="Select Company or Create New"></asp:Label>
</div>
<div id="btnCreate" style="text-align:right">
<asp:Button ID="btnCreateNew" runat="server" Text="Create New" CssClass="BtnStyle" />
</div><br />
<div id="divGrid">
</div>
</form>
</asp:Content>
This is a pretty common error to see. It's a security feature in ASP.NET that prevents someone from injecting additional controls onto the page after it has rendered, and posting those controls back to the server. It makes your website harder to hack.
Are you creating any input controls through javascript? If so, don't. Create the controls normally inside the web form and wrap them in a <div style='display:none;'> and display them when you need them.
Are you creating any controls dynamically within C#? - If you are, you must create them inside Page_Init, not Page_Load, otherwise ASP.NET won't recognise them as valid.
You can disable this security measure by setting EnableViewStateMac=False at the top of the aspx page, but I strongly advise against it.