I tried to find the bits of information available on the net, But couldn't hook it up together. I have an application which makes use of a login page. Now I'm not binding the view to any model. However I have two inputs (Username and password). I want to fire the jquery unobtrusive validation feature that comes with Asp.net Mvc. Here are the samples:
Backbone Template with Twitter bootstrap which is the Login template (underscore templating engine).
@using (@Html.BeginForm("Login", "Home", FormMethod.Post, new { @class = "form-horizontal" }))
{
<div class="modal-body">
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
@*<input type="text" id="inputEmail" placeholder="Email">*@
@Html.TextBox("Email", "", new { @placeholder = "Email" })
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
<input type="password" id="inputPassword" placeholder="Password">
</div>
</div>
<div class="control-group">
@*<div class="controls">
<label class="checkbox">
<input type="checkbox"> Remember me
</label>
<button type="submit" class="btn">Sign in</button>
</div>*@
</div>
</div>
<div class="modal-footer">
@*<a class="btn" href="#" id="closeBtn">Close</a>*@ <button type="submit" class="btn btn-primary" >Login</button>
</div>
}
The Coffeescript for Login view
require ['backbone','bootstrap','jqValUnobtrusive'],(Backbone) ->
class LoginView extends Backbone.View
el:$("#content")
initialize: ->
@render()
@validationOptions = @options.validationOptions
validationOptions:
rules:
'Email':
required:true
'user[password]':
required:true
close:->
console.log "hello"
render : ->
$(@el).find("#login").html _.template $("#loginTemplate").html()
App = new LoginView
And My controller to add Server side logic to validation rule in absence of javascript from the browser
[HttpPost]
[ValidateInput(true)]
public ActionResult Login(FormCollection Form)
{
ViewBag.Message = "Method posted";
if (Form["Email"] == string.Empty)
{
ModelState.AddModelError("Email", "Please Provide a Username");
ModelState.SetModelValue("Email", ValueProvider.GetValue("Email"));
}
return View();
}
Would someone be able to shine some light on this problem? How to put the twitter bootstrap validation messages with this jquery unobtrusive code to show the errors? Or extend the jquery unobtrusive validation plugin to work with twitter bootstrap / backbone to perform the validation?
My criteria is the username and password shouldn't be NULL or blank . A simple case for study for validating a form without model binding (I dont want to bind the Login page to any model). Rather hook up the jquery unobtrusive plugin with extending backbone and twitter bootstrap styling!