Tuesday, March 31, 2015

Visual studio show breakpoints

Simply use the keyboard shortcut Ctrl + Alt + B, which should show you the breakpoints window

Ajax partial view mvc update

http://www.blackbeltcoder.com/Articles/script/using-ajax-beginform-with-asp-net-mvc

https://cmatskas.com/update-an-mvc-partial-view-with-ajax/

http://blog.falafel.com/ajaxbeginform-with-mvc-4/

http://stackoverflow.com/questions/5410055/using-ajax-beginform-with-asp-net-mvc-3-razor

http://stackoverflow.com/questions/1440151/asp-net-mvc-ajax-form-loadingelementid





AjaxScript.js :

$(document).ready(function () {
    $("#ajax-loader").css("display", "none");
});

function OnBegin() {
    $("#divMsg").html("Beginning Ajax request.");
}
function OnComplete() {
    $("#divMsg").html("Completing Ajax request.");
}
function OnSuccess(response) {
    $("#searchresults").html(response);
    }

function OnFailure(response) {
    $("#searchresults").html("Whoops! That didn't go so well did it?");
}

view index.cstml
<div  class="panel-body">
    @using (Ajax.BeginForm("PerformAction", new AjaxOptions
     {
        InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
        HttpMethod="POST",
        OnBegin=" OnBegin",
        OnSuccess = "OnSuccess",
        OnFailure = "OnFailure" ,
        LoadingElementDuration = 1000,
        LoadingElementId = "ajax-loader",
        UpdateTargetId = "searchresults"
    }))
    {
        <fieldset>
            @Html.LabelFor(m => m.EmailAddress)
            @Html.TextBoxFor(m => m.EmailAddress)
            @Html.ValidationMessageFor(m => m.EmailAddress)
            <input type="submit" value="Submit" />
        </fieldset>
    }
    <div id="ajax-loader">
        <img src="~/Images/ajax-loader.gif" 
 Style="display:none"/>
    </div>
    <div id="searchresults"></div>
    <div id="divMsg"></div>
</div>


namespace TestAjaxFrom.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new AjaxDemoModel();
            return View();
        }

        [HttpPost]
        public ActionResult PerformAction(AjaxDemoModel model)
        {
            try
            {
                // Perform tasks here
               

                return Content("Action was successfully completed!");
            }
            catch (Exception)
            {
                return Content("Oh no! Looks like we ran off into a ditch on that one!");
            }
        }
    }
}

viewmodel

using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.ComponentModel.DataAnnotations;
namespace TestAjaxFrom.ViewModels
{
    public class AjaxDemoModel
    {
        [Required]
        [Display(Name = "Email address")]
        public string EmailAddress { get; set; }
    }
}

Sample partial view

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace partialView.Models
{
    public class Person
    {
        public int id { get; set; }
        public string nameLast { get; set; }
        public Address address { get; set; }
       
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace partialView.Models
{
    public class Address
    {
        public string streeNo { get; set; }
        public string city { get; set; }
        public string state { get; set; }
    }


}

Home/Index.cshtml

@model partialView.Models.Person
<h2>Index</h2>
@{
    ViewBag.Title = "Index";
}
<div>@Html.DisplayFor(model => model.nameLast)</div>
<div>@Html.DisplayFor(model => model.address.streeNo)</div>
<div>@Html.DisplayFor(model => model.address.city)</div>
<div>@Html.DisplayFor(model => model.address.state)</div>

@Html.Partial("_AddressEdit",Model.address)

@Html.ActionLink("edit address","edit","Person",new { id=Model.id }, null);

@Html.ActionLink("edit address", "edit", "Person", new { id = Model.id }, null);

PersonController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using partialView.Models;

namespace partialView.Controllers
{
    public class PersonController : Controller
    {
        // GET: Person
     
        public ActionResult AddressEdit(int id)
        {
            return View("_Address");
        }


        public ActionResult Index()
        {
            Person person = new Person();
            person.nameLast = "chu";
            Address addr = new Address();
            addr.city = "columbus";
            addr.state = "oh";
            addr.streeNo = "101";
            person.address = addr;

            return View();
        }

        // GET: Person/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Person/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Person/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Person/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Person/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Person/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Person/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

Partialview
_AddressEdit.chtml

@model partialView.Models.Address


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Address</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.streeNo, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.streeNo, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.streeNo, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.city, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.city, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.city, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.state, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.state, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.state, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

A library for managing CSS & JavaScript dependencies and optimization in ASP.Net

https://github.com/Shazwazza/ClientDependency

Partial in edit view

http://stackoverflow.com/questions/20278530/can-i-edit-multiple-models-in-asp-net-mvc-using-partial-views

Below pop up for forms
http://www.codeproject.com/Articles/678222/CRUD-Operations-using-Partial-V

http://www.khalidabuhakmeh.com/utilizing-partial-views-in-asp-net-mvc-to-cut-your-effort-in-half

Monday, March 30, 2015

MVC area

You have to add area to every action link otherwise link will be messed up.

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new {area = "", id = "logoutForm", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
            @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { area = "",title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
    }
}
else
{
    <ul class="nav navbar-nav navbar-right">
        <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new {area = "", id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { area = "", id = "loginLink" })</li>
    </ul>
}


Linking Between Two Areas

ASP.NET MVC Views often need to link to some action method residing in controller classes. Unless otherwise specified, action methods and controllers are assumed to be from the current area. In case you wish to link to an action method belonging to some other area, here is how you would do just that:
  1. <h3>You are inside main area. To navigate to some other area please click on the following links</h3>
  2. <%= Html.ActionLink("Main Area", "Index", "Home", new { area = "" }, null)%>
  3. <br /><br />
  4. <%= Html.ActionLink("Blog Area", "Index", "Home", new { area = "Blog" }, null)%>
  5. <br /><br />
  6. <%= Html.ActionLink("Help Desk Area", "Index", "Home", new { area = "HelpDesk" }, null)%>
  7. <br /><br />
  8. <%= Html.ActionLink("Shopping Area", "Index", "Home", new { area = "Shopping" }, null)%>
The above fragment is from the Index view of the main area. It renders four hyperlinks using ActionLink(). Notice the fourth parameter that specifies area name. This parameter governs the area whose action method is being linked to.

Redirecting to an Action Belonging to Another Area

You can also redirect to some action method of a specific area using RedirectToAction() method. Consider the following action method:
  1. public ActionResult Index()
  2. {
  3. return RedirectToAction("Index", "Home", new { Area = "HelpDesk" });
  4. }
As you can see, the Index() action method of the main area redirects the control to Index() action method of the HelpDesk area.


http://www.codeproject.com/Articles/714356/Areas-in-ASP-NET-MVC

http://www.itorian.com/2013/10/area-in-mvc-5-with-example-step-by-step.html

http://www.codeproject.com/Articles/714356/Areas-in-ASP-NET-MVC

http://www.codeguru.com/csharp/.net/net_asp/mvc/article.php/c20227/Using-Areas-in-ASPNET-MVC-Application.htm


VB.net read text, write text, connect to SQL open connection query SQL and read results add to list box

Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO

Public Class Form1

    Public conn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim i As Integer = 0
        Dim j As Integer = 0
        ListBox1.Items.Clear()
        If ListBox2.Items.Count > 0 Then
            For Each a As String In ListBox2.Items
                i = GetData(a)
                If i > 0 Then
                    j = j + 1
                End If
                ListBox1.Items.Add(String.Format("XYZ:{0} find:{1}", a, i))
                ListBox1.Refresh()
            Next
        End If
        MsgBox("done total found=" & j)
    End Sub

    Private Function GetData(ByVal xyz As Integer) As Integer
        Dim xyzLen As Integer = Len(xyz.ToString)
        Dim sql As String
        Dim xyzBackup As Integer
        xyzBackup = xyz
        If xyzLen = 6 Then
        ElseIf xyzLen < 9 Then
            xyz = Convert.ToInt32(OPERSCommon.Entity.XYZ.ConvertTDFinToSTFin(xyz))
        End If
        sql = "select FROM  where = " & xyz
        If conn.State = ConnectionState.Open Then
        Else
            conn.Open()
        End If
        Dim cnt As Integer = 0
        Dim cmd As New Data.SqlClient.SqlCommand(sql, conn)
        Try

            Dim dr As SqlDataReader = cmd.ExecuteReader()
            While (dr.Read())
                cnt = cnt + 1
            End While
        Catch ex As Exception
            MessageBox.Show("SQL conn error" & ex.Message)
        End Try
        cmd.Dispose()
        conn.Close()
        If cnt > 0 Then
            ListBox3.Items.Add(String.Format("before XYZ={0}=>{1}={2}", xyzBackup, xyz, cnt))
            ListBox3.Refresh()
            Dim lineout As String = xyzBackup & " " & xyz
            Using sw As StreamWriter = New StreamWriter("c:\XYZOnProdandTest.txt", True)
                sw.WriteLine(lineout)
            End Using
        End If
        Return cnt
    End Function

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        For Each line As String In File.ReadLines("c:\XYZFromMSBSDB2P.txt")
            ListBox2.Items.Add(line)
        Next
    End Sub
End Class


Sunday, March 29, 2015

tempdata

http://brockallen.com/2012/06/11/cookie-based-tempdata-provider/

partial view render on button click

http://stackoverflow.com/questions/11961374/partial-view-render-on-button-click

Asp.net ajax

http://www.codeguru.com/csharp/.net/working-with-ajax-helper-in-asp.net-mvc.htm

http://chsakell.com/2013/05/10/ajax-and-jquery-in-asp-net-mvc/

http://www.c-sharpcorner.com/uploadfile/3d39b4/working-with-html-beginform-and-ajax-beginform-in-mvc-3/


http://www.codeguru.com/csharp/.net/working-with-ajax-helper-in-asp.net-mvc.htm
PropertyDescription
UrlThe Url property indicates a URL to which the form is to be submitted. You can also specify a controller and action method in the BeginForm() method instead of setting the URL property.
HttpMethodThe HttpMethod property indicates the HTTP method (GET or POST) to be used while making an Ajax request.
ConfirmThe Confirm property is used to specify a message that will be displayed in a confirm dialog to the end user. If user clicks OK on the confirmation dialog the Ajax call is made.
OnBeginThe OnBegin property specifies a name of JavaScript function that is called at the beginning of the Ajax request.
OnCompleteThe OnComplete property specifies a name of JavaScript function that is called at the end of the Ajax request.
OnSuccessThe OnSuccess property specifies a name of JavaScript function that is called when the Ajax request is successful.
OnFailureThe OnFailure property specifies a name of JavaScript function that is called if the Ajax request fails.
LoadingElementIdWhile an Ajax request is being made you can display a progress message or animation to the end user. The LoadingElementId indicates an ID of a DOM element that is serving this purpose. Note that the Ajax helper will simply display and hide this element. Displaying some progress message of animation inside this element is your responsibility.
LoadingElementDurationThe LoadingElementDuration property specifies a time duration in milliseconds that controls the duration of the progress message or animation.
UpdateTargetIdThe UpdateTargetId property specifies an ID of a DOM element that will be populated with the HTML returned by the action method.
InsertionModeThe InsertionMode property governs how the HTML replacement should occur in a DOM element as specified by UpdateTargetId property. The possible values are InsertAfter, InsertBefore and Replace.