Dotnet 3.5 interview questions and answers Headline Animator
Monday, October 26, 2009
Thursday, October 22, 2009
ASP.NET MVC Example Application over Northwind with the Entity Framework
ASP.NET MVC Example Application over Northwind with the Entity Framework
http://blogs.msdn.com/brada/archive/2008/01/29/asp-net-mvc-example-application-over-northwind-with-the-entity-framework.aspx
Wednesday, October 7, 2009
Model View Controller (MVC) Architecture in ASP.NET
http://www.codeproject.com/aspnet/ModelViewController.asp
Thursday, September 24, 2009
MVC AJAX Form with Ajax.BeginForm() and jQuery Thickbox ...

After clicking this link, it pops up the following dialog where the user can type in their message:
This scenario can be implemented with MVC with very few lines of code. First off, we’ll be using the AJAX HTML Message pattern so that the AJAX messages that are going across the wire are simple HTML snippets. Also, we’ll use jQuery Thickbox for our dialog. The first thing we need to do it to implement our “Contact this person” link:
1: <%=Html.ActionLink("Contact this person", "Index", "Contact", new { height = 200, width = 300 }, new { @class = "thickbox" }) %>
This is a pretty typical implementation of the jQuery thickbox. We’re specifying that we want an AJAX call to be made to our ContactController.Index() method. The HTML that this ActionLink renders will simply look like this:
1: <a class="thickbox" href="/Contact?height=200&width=300">Contact this person</a>
The complete code for the ContractController looks like this:
1: public class ContactController : Controller
2: {
3: public ActionResult Index()
4: {
5: return View(new ContactMessage());
6: }
7:
8: [ActionName("Index")]
9: [AcceptVerbs(HttpVerbs.Post)]
10: public ActionResult SubmitMessage(ContactMessage message)
11: {
12: return this.View("Confirmation", message);
13: }
14: }
It is important to note that both views returned by the ContactController’s action methods are both partial views (i.e., Index.ascx and Confirmation.ascx) with our HTML snippets in there. We know that in the Index.ascx we want to do an AJAX form submission/post rather than a full page form submission. There are a couple of ways to do this including using the jQuery Form plugin. However, in this case, the MVC framework already comes built-in with this functionality so we don’t have to rely on the jQuery Form plugin (unless we want to). Most of the time we find ourselves using the Html property of the View which is of type HtmlHelper. However, there is also an Ajax property of the view of type AjaxHelper. The AjaxHelper has a BeginForm method that will allow you to submit the form via AJAX. You’ll need to make sure you include the MicrosoftAjax.js and MicrosoftMvcAjax.js scripts in your page to use the Ajax helpers. The complete implementation for the Index.ascx can just look like this:
1: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcAjaxForm.Models.ContactMessage>" %>
2:
3: <h2>Contact</h2>
4:
5: <% using (Ajax.BeginForm("Index", "Contact", new AjaxOptions { UpdateTargetId = "contactArea" } )) { %>
6: <div id="contactArea">
7: Email Address: <%=Html.TextBox("EmailAddress") %> <br /><br />
8: Message: <%=Html.TextArea("MessageText")%>
9: <input type="submit" />
10: </div>
11: <% } %>
Notice the UpdateTargetId is specifying the “contactArea” div. This causes all HTML that is sent back on the form post (i.e., the confirmation message) to be displayed inside this div only.
Tuesday, September 22, 2009
ASP.NET MVC jQuery Autocomplete plug-in using JSON.
ASP.NET MVC jQuery Autocomplete plug-in using JSON.
This post demonstrate how to use JQuery Autocomplete feature in ASP.NET MVC. In this demo we call the controller Action method to get the data in JSON format and bind it to the text box to accomplish the Autocomplete feature.
Before starting download the js and css file from jQuery Autocomplete plugin and add in the project and follow the steps mention below
Step 1. Add the following code for action method in controller
public
ActionResult StateLookup(string q, int limit)
{
var jsonData = new[]{
new {Code = "AL", Name = "Alabama"},
new {Code = "CA", Name= "Califonia"},
new {Code = "IN", Name = "Indiana"},
new {Code = "MD", Name = "Maryland"},
new {Code = "NJ", Name = "New Jersey"}
};
return Json(jsonData);
}
I have used hardcode JSON list for the state. You can construct the JSON data from database using LINQ.
Step 2. Add the following java script in the view
<script
type="text/javascript">
$(document).ready( function() {
$('#state').autocomplete('<%=Url.Action("StateLookup", "Home") %>', {
dataType: 'json',
parse: function(data) {
var rows = new Array();
for(var i=0; i<data.length; i++){
rows[i] = { data:data[i], value:data[i].Code, result:data[i].Code };
}
return rows;
},
formatItem: function(row, i, n) {
return row.Code + ' - ' + row.Name;
},
width: 300,
mustMatch: true,
});
});
</script>
Here, I specify that the rows in the drop down will have the state "Code - Name" format, and that the value that will be put in the textbox is the Code property of the selected item. This is done with the value property of the rows array.
Step 2. Add the HTML Text box
<%= Html.TextBox("state")%>
Complie the application and run it. To get this running, don't forget to include the required javascript files and the css to style the autocomplete drop down.
Wednesday, September 9, 2009
How to use textbox
- <!--HTML helper method for TextBox with ID only -->
- <%= Html.TextBox("txtFirstName") %>
- <input id="txtFirstName" name="txtFirstName" type="text" value="" />
- <!--HTML helper method for TextBox with ID and Value -->
- <%= Html.TextBox("txtFirstName","Alice") %>
- <input id="txtFirstName" name="txtFirstName" type="text" value="Alice" />
- <!--HTML helper method for TextBox with ID and Value and custom attributes. use '@' if the attribute name is keyword in C# or VB -->
- <%= Html.TextBox("txtFirstName", "Alice", new { @class="error", client_selector="alphanumeric" })%>
- <input class="error" client_selector="alphanumeric" id="txtFirstName" name="txtFirstName" type="text" value="Alice" />
How to use drop down list
- <!--HTML helper method for DropDownList field with ID and OptionLabel -->
- <%= Html.DropDownList("ddlStates") %>
- <%= Html.DropDownList("ddlStates","Please select state") %>
Exception : There is no ViewData item with the key 'ddlStates' of type 'IEnumerable<SelectListItem>'.
Code
- <!--HTML helper method for DropDownList field with ID and SelectListItem ( Made "Maryland" to be the default selected value) -->
- <%= Html.DropDownList("ddlStates",new List <SelectListItem>(){
- new SelectListItem(){ Text="Maryland", Value="MD", Selected=true },
- new SelectListItem(){Text="Kansas", Value="KS"},
- new SelectListItem(){Text="Texas", Value="TX"}}) %>
- <select id="ddlStates" name="ddlStates"><option selected="selected" value="MD">Maryland</option>
- <option value="KS">Kansas</option>
- <option value="TX">Texas</option>
- </select>
- <!--HTML helper method for DropDownList field with ID , SelectListItem and OptionLabel(Please select state). OptionLabel will be selected by default if not selected is given in the SelectListItem-->
- <%= Html.DropDownList("ddlStates", new List <SelectListItem>(){
- new SelectListItem(){ Text="Maryland", Value="MD"},
- new SelectListItem(){Text="Kansas", Value="KS"},
- new SelectListItem(){Text="Texas", Value="TX"}},"Please select state") %>
- <select id="ddlStates" name="ddlStates"><option value="">Please select state</option>
- <option value="MD">Maryland</option>
- <option value="KS">Kansas</option>
- <option value="TX">Texas</option>
- </select>
- <!--HTML helper method for DropDownList field with ID , SelectListItem , OptionLabel(Please select state) and custom attributes. use '@' if the attribute name is keyword in C# or VB. The default selection will be "Maryland".-->
- <%= Html.DropDownList("ddlStates", new List<SelectListItem>(){
- new SelectListItem(){ Text="Maryland", Value="MD", Selected=true },
- new SelectListItem(){Text="Kansas", Value="KS"},
- new SelectListItem(){Text="Texas", Value="TX"}}, "Please select state", new { @class="error", client_selector="alphanumeric"})%>
- <select class="error" client_selector="alphanumeric" id="ddlStates" name="ddlStates"><option value="">Please select state</option>
- <option selected="selected" value="MD">Maryland</option>
- <option value="KS">Kansas</option>
- <option value="TX">Texas</option>
- </select>
Tuesday, September 8, 2009
How to use checkbox
Notice that the check box helper (Html.CheckBox()) renders two input controls. First, it renders a check box control as you’d expect, and then it renders a hidden input control of the same name. This is to work around the fact that when check boxes are deselected, browsers don’t submit any value for them. Having the hidden input control means the MVC Framework will receive the hidden field’s value (i.e., false) when the check box is unchecked.
Checkbox doesn't have a label. So add the html label tag with for attribute with value as “name of checkbox”
Code:
<%= Html.CheckBox("chkAccept2", true )%>
<label for="chkAccept2">Accept Terms & Conditions</label>
<input name="chkAccept2" type="hidden" value="false" />
<label for="chkAccept2">Accept Terms & Conditions</label>
<%= Html.CheckBox("chkAccept3", false, new { @class="error", @client_selector="alphanumeric" }) %>
<label for="chkAccept3">Accept Terms & Conditions</label>
<input class="error" client_selector="alphanumeric" id="chkAccept3" name="chkAccept3" type="checkbox" value="true" />
<input name="chkAccept3" type="hidden" value="false" /><label for="chkAccept3">Accept Terms & Conditions</label>
Source : Apress.Pro.ASP.NET.MVC.Framework.


Original Source: