Ian
Ian

Reputation: 63

Client Side Validation with ASP.NET MVC 2 using SPARK view engine

Is there a way to use Client Side Validation with the SPARK view engine?

I have the following SPARK view:

<script type="text/javascript" src="~/content/script/shared/MicrosoftAjax.js?${ApplicationStartTime}"></script>      
<script type="text/javascript" src="~/content/script/shared/jquery-ui-1.7.2.custom.min.js?${ApplicationStartTime}"></script>
<script type="text/javascript" src="~/content/script/shared/jquery.validate.min.js?${ApplicationStartTime}"></script>
<script type="text/javascript" src="~/content/script/shared/jquery.validate.unobtrusive.js?${ApplicationStartTime}"></script>
<script type="text/javascript" src="~/content/script/shared/MicrosoftMvcJQueryValidation.js?${ApplicationStartTime}"></script>


<viewdata model="Business.Models.Development.Dtos.DonationFormDto" />

#Html.EnableClientValidation();

<form id="form" action="~/development/donate.mvc" method="post">

<label>
  *First Name
</label><br/>
<input type="text" name="model.FirstName" Id="FirstName" value="${Model.FirstName}"/> ${Html.ValidationMessage("model.FirstName")}
<br/>

</form>

Server-side validation works fine, but client-side validation doesn't.

I can make it work by using ASP.NET Helpers and form syntax like so:

using (Html.BeginForm("Index", "Donate", FormMethod.Post, new {id = "form", action="donate.mvc" }))
{  
    <%=Html.LabelFor( model => model.FirstName) %><br/>
    <%=Html.TextBoxFor(model => model.FirstName)%>
    <%=Html.ValidationMessageFor( model => model.FirstName) %><br/>
}

But then my model doesn't bind correctly and sever-side validation fails on submit.

Any suggestions? Is abandoning SPARK and creating a classic strongly-typed view the only option?

Upvotes: 0

Views: 438

Answers (1)

Ichiro Satoshi
Ichiro Satoshi

Reputation: 301

@KarlBear, why did you use "<%=" instead of "${...}" or "!{...}" in spark ?

${Html.LabelFor( model => model.FirstName)}<br/>
${Html.TextBoxFor(model => model.FirstName)}
${Html.ValidationMessageFor( model => model.FirstName)]<br/>

Upvotes: 0

Related Questions