Asp.net MVC3 validation issue with jQuery hint plugin

Recently I came across an issue in which asp.net MVC3 validations stopped working after I added jQuery hint plug in.

Solution:

1. Add reference to jquery.hint.js in header tag
2. Add jquery.validate.min.js and jquery.validate.unobtrusive.min.js just before end of the body tag
Tuesday, October 18, 2011
Posted by Unknown
Tag :

Asp.net MVC3 validation independent of Model

I was facing an tricky situation where a page will have multiple inputs derived from one model property. E.g. There is a model which has property called "Question" which will be used in a view where there will be multiple instance of this Question property as a input textbox but only first question should be mandatory and rest questions should be optional.
Now when I add RequiredField attribute in model itself and add @HtmlValidationMessageFor in view it shows validation message for all input fields.


SurveyQuestionsModel.cs
@Html.TextBoxFor(model => model.SurveyQuestions[i].Question)
@Html.ValidationMessageFor(model => model.SurveyQuestions[i].Question)


SurveyQuestion.cshtml
[Required(ErrorMessage = "Required field")] 
[Display(Name = "Question")] 
public string Question {get;set;}

Finally I got the solution, though I think it's not the correct way. But it solved my issue. Steps I did to fix the issue -
  1. I observed Html for input fields for which there is an validation error. The input field will have additional attributes such as "data-val-required" and "data-val"
  2. Then I added these fields to textbox which needs to be validated.
  3. Then I added Html.Validation() for textbox with validation message.
My final code in SurveyQuestion.cshtml  looks like this
@if (i == 0)
{
 @Html.TextBoxFor(model => model.SurveyQuestions[i].Question, new Dictionary { { "data-val-required", "required" }, { "data-val", "true" },  { "title", "Question " + (i + 1) }, {"class","QuestionTextBox"} })           
 
@Html.ValidationMessage("SurveyQuestions[0].Question", "At least one question is required.") } else { @Html.TextBoxFor(model => model.SurveyQuestions[i].Question, new { @class = "QuestionTextBox", @title = "Question " + (i + 1) }) }

Saturday, October 15, 2011
Posted by Unknown

How to set default browser in VS2010 MVC3 project


I want to change the default browser used by Visual Studio for debugging. Normally the route I'd take to do this is right clicking on an .aspx file and setting the default from the Browse With... dialog.
Unfortunately, ASP.NET MVC Views don't have the Browse With... option.
Solution:
  1. Create "Default.htm" page inside views folder and right click on it, you will be surprised to see option  "Browse with"


Saturday, October 1, 2011
Posted by Unknown

Jquery - Tips & Tricks


  • Use min version of jquery (jquery.min.js) always, it is compressed and improves performance.
  • For jquery IntelliSense use vsdoc.js e.g . currently available version is jquery-1.5-vsdoc.js. Download it from http://damianedwards.com/files/jquery/jquery-1.5-vsdoc.js . This is very useful and helps you while writing jquery functions.
  • CDN hosted jquery - This is the recommended version of jQuery to use for your application. The code in here should be stable and usable in all modern browsers.
  • jquery shorthand - instead of using full syntax for initializing jquery function you can use shorthand which is very quick and easy to understand
Full syntax - 

$(document).ready(function ()
{
    // ...
});
Shorthand syntax - 
$(function ()
{
    // ...
});
  • jsfiddle - it is very useful online page where you can check any jquery functions.
  • Link http://jsfiddle.net/
Friday, September 30, 2011
Posted by Unknown

It's the begining!

Hello Geeks of Sumeru!

From today onwards we are starting new trend of daily .net! What is it?
The answer is simple, everyone from .net team will present his/her own leanings on that day. Topics can be anything on .net and duration will be 15-20 mins..

So let's get rolling :)

Happy .neting....


Thursday, September 29, 2011
Posted by Unknown

Popular Post

Blogger templates

Labels

Followers

Powered by Blogger.

- Copyright © daily .net - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -