Wednesday 30 November 2016

MVC - TextBoxFor - Set default value

Watch this example on YouTube


Replace
 @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })

With
    @Html.TextBoxFor(model => model.FirstName, new {  @class = "form-control" , @Value="NO NAME" })

MVC - Set width of TextBox

Watch this exampel on YouTube



in .css add the following
.width200{
    width: 200px;
}


in view replace

   @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
with
   @Html.TextBoxFor(model => model.FirstName, new { htmlAttributes = new { @class = "width200" } })

MVC - Table - display No Data found message if empty table



Watch this example on YouTube:

Replace 

@model IEnumerable<MVCTest.Models.Customer>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Address)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
            @Html.ActionLink("Details", "Details", new { id = item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })
        </td>
    </tr>
}

</table>


with 

@model IEnumerable<MVCTest.Models.Customer>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
@if (Model.Count() > 0)
{


<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Address)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
            @Html.ActionLink("Details", "Details", new { id = item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })
        </td>
    </tr>
}

</table>
}
else{
    <h2>No Data Found</h2>
}

Wednesday 2 November 2016

Fix - MVC Error: 'object' does not contain a definition for ''

Watch this example on YouTube

To fix it replace
   @Html.TextBoxFor(model => model.CustomerID, new  object { @class = "WidthLong" })
With
   @Html.TextBoxFor(model => model.CustomerID, new  { @class = "WidthLong" })

Watch this on YouTube:


0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'checked'

To fix it - replace
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="Scripts/jquery-1.10.2.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <script type="text/javascript">
            $(document).ready(function () {
                $('.rb').click(function () {
                    if ($('#RadioButton1').checked()) {
                        alert('radio button 1 checked');                   
                    } else {
                        alert('radio button 2 checked');
                    }
                   
                });
            });
        </script>
    <div>
        <asp:RadioButton ID="RadioButton1" runat="server" GroupName="GroupOne" />
        <asp:RadioButton ID="RadioButton2" runat="server" GroupName="GroupOne"/>
        <asp:Button ID="Button1" class="rb" runat="server" Text="Button" />
    </div>
    </form>
</body>

With
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="Scripts/jquery-1.10.2.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <script type="text/javascript">
            $(document).ready(function () {
                $('.rb').click(function () {
                    if ($('#RadioButton1').attr('checked')) {
                        alert('radio button 1 checked');                   
                    } else {
                        alert('radio button 2 checked');
                    }
                   
                });
            });
        </script>
    <div>
        <asp:RadioButton ID="RadioButton1" runat="server" GroupName="GroupOne" />
        <asp:RadioButton ID="RadioButton2" runat="server" GroupName="GroupOne"/>
        <asp:Button ID="Button1" class="rb" runat="server" Text="Button" />
    </div>
    </form>
</body>

jQuery - fix error - 0x800a1391 - JavaScript runtime error: '$' is undefined

Watch this example on YouTube

To fix it Replace

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <script type="text/javascript">
            $(document).ready(function () {
                $('.rb').click(function () {
                    alert('clicked');
                });
            });
        </script>
    <div>
       
    </div>
    </form>
</body>
</html>



With

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="Scripts/jquery-1.10.2.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <script type="text/javascript">
            $(document).ready(function () {
                $('.rb').click(function () {
                    alert('clicked');
                });
            });
        </script>
    <div>
       
    </div>
    </form>
</body>
</html>

ASP.Net - fix error - Cannot have multiple items selected in a DropDownList.


Cannot have multiple items selected in a DropDownList.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Watch this example on YouTube:





Here is my HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>ONE</asp:ListItem>
            <asp:ListItem>TWO</asp:ListItem>
            <asp:ListItem>THREE</asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html> 


And here is my code behind

        protected void Page_Load(object sender, EventArgs e)
        {
            ListItem lst = DropDownList1.Items.FindByText("ONE");
            if (lst != null)
            {
                lst.Selected = true;
            }           
        } 



To fix it replace
  lst.Selected = true;
With
                DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("ONE")); 
  

VB.NET - Enum - get number of elements (lenght of enum)

Watch this example on YouTube


    Public Enum SomeEnum
        Bob = 1
        Frank = 2
        Gary = 3
        Taras = 4
        Oleh = 5
    End Enum

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim num As Integer = [Enum].GetNames(GetType(SomeEnum)).Length
        MessageBox.Show(num)
    End Sub