Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

5/29/16

How to type caste IEnumerable to an object list using lameda expression and foreach loop

           List o1= new List();
           
           var output = db.TaxOnProduct("Chai", 2, "Beverages").ToList();
           List o1 = output.Select(x => new Output { CategoryName = x.CategoryName.ToString(), ProductName = x.ProductName, TotalAmountAfterTax = x.TotalAmountAfterTax, TotalBeforeAfterTax = x.TotalBeforeAfterTax }).ToList();
           foreach (var test in output)
           {
               Output objoutput= new Output();
               objoutput.ProductName = test.ProductName;

               o1.Add(objoutput);
           }

i used entity frame work and get data using store procedure then used lameda expression to put output in list of object

10/15/14

Submit form using ajax in case of multiple form

Submit form using ajax in case of multiple form
@model WebApplication2.Models.Person
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
    $(document).ready(function () {
        $("#Button1").click(function () {
            $.ajax({
                url: "/Home/Method",
                type: 'POST',
                data: $("#Form2").serialize(),
                success: function (response) {
                    document.getElementById("Firstname").value = response
                   alert(response);
                },
                error: function (xhr) {
                    alert("Something went wrong, please try again");
                }
            });
        });
    });
</script>
@using (@Html.BeginForm("Index", "Home", FormMethod.Post, new { id="Form1" }))
{
    <div class="jumbotron">
        @Html.TextBoxFor(model => model.Firstname)
        <br />
       <br />
       <br />
    </div>
}
<input id="Button1" type="submit" value="SubmitBtn" />
<br />
@using (@Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "Form2" }))
{
    <div class="jumbotron">
        @Html.TextBoxFor(model => model.Firstname)
        <br />
        <br />
        <br />
    </div>
}

How to submit form value in mvc


How to submit form value

The value can be submitted through submit button and ajax button

I have create one form which has below which has one input box and two button one submit and other normal button

@model WebApplication2.Models.Person

 

@{

    ViewBag.Title = "Index";

}

 

<script src="~/Scripts/jquery-1.10.2.js"></script>

<script>

    $(document).ready(function () {

        $("#Button2").click(function () {

            $.ajax({

                url: "/Home/Method",

                type: 'POST',

                data: $('form').serialize(),

                success: function (response) {

                    document.getElementById("Firstname").value = response

                },

                error: function (xhr) {

                    alert("Something went wrong, please try again");

                }

            });

 

        });

 

    });

 

</script>

@using (@Html.BeginForm("Index", "Home", FormMethod.Post, new { autocomplete = "off" }))

{

 

    <div class="jumbotron">

        @Html.TextBoxFor(model => model.Firstname)

 

        <br />

        <br />

        <br />

        <input id="Button1" type="submit" value="SubmitBtn" />

        <br />

        <input id="Button2" type="button" value="Enter" />

 

 

    </div>

 

}

 

Model

   public class Person

    {

        public string Firstname { get; set; }

    }

Controller code

        [HttpPost]

        public string Index(Person person)

        {

            string form = "Submit response ";

            return form;

        }

        [HttpPost]

        public string Method(Person person)

        {

            string form = person.Firstname;

 

            return form;

        }

To submit the form

@using (@Html.BeginForm("Index", "Home", FormMethod.Post, new { autocomplete = "off" }))

 

using submit button we need to define strongly typed control so that value can be retrieved in the submit action “index” defined in the form name

@Html.TextBoxFor(model => model.Firstname)

10/8/14

how to post form data using ajax call in mvc


If we have to post full form data using AJAX request then we can use

$("form").serialize()

And if we have more than one form on the page  then we need to provide form name in the ajax call

$(document).ready(function () {

        $("#_buttonSearch").click(function () {

            alert("test");

            $.ajax({

                url: "/Home/Search",

                type: 'POST',

                data: $('#test').serialize(),

            });

        });

    });

8/21/14

how Serializing entity using entity framework

XmlDocument myXml = new XmlDocument();
XPathNavigator xNav = myXml.CreateNavigator();
Employee e1 = new Employee();
XmlSerializer x = new XmlSerializer(employee.GetType());
using (var xs = xNav.AppendChild())
{
    x.Serialize(xs, employee);
}
e1.Department_Id = employee.Department_Id;
e1.FirstName = employee.FirstName;
e1.Gender = myXml.OuterXml;
e1.Id = employee.Id;
e1.LastName = employee.LastName;
e1.Salary = employee.Salary;
db.Employees.Add(e1);
db.SaveChanges();

courtesy :https://www.udemy.com/blog/csharp-serialize-to-xml/

8/20/14

display dropdown


ViewBag.toAccount = new SelectList(db.GetIEnumerableAccountDetails(userID, "SI"), "AccountID", "AccountName");//,id

ViewBag.FromAccount = new SelectList(db.GetIEnumerableAccountDetails(userID, "SI"), "AccountID", "AccountName");

8/13/14

Jquery to enable or disable the button

  $("#button1").click(function () {
            $("#radio2").button({ disabled: true });
             alert($('input:radio[name=radio1]:checked').val());
         }
            );
         $("#button2").click(function () {
             $("#radio2").button({ disabled: false });
             alert('button clicked');
         }

8/7/14

Regular Expression for speaial charater

[RegularExpression(@"([a-zA-Z\d]+[\w\d]*|)[a-zA-Z]+[\w\d.]*", ErrorMessage = "Invalid username")] public string Username { get; set; }

8/6/14

about tool tip

http://www.codeproject.com/Tips/710097/Jquery-Tooltip-in-ASP-NET-MVC

how to change value in input box on click event using java script

    function calcualte()
    {
        alert("this method is called")
        document.getElementById("Text1").value = 16;
    }


how to set dropdown vLUE IN MVC

@Html.DropDownListFor(model => model.FromAccount, (IEnumerable<SelectListItem>)ViewData["FromAccount"], "Select from Account")

FILL THE VIEW BAG
ViewBag.toAccount = new SelectList(db.GetIEnumerableAccountDetails(userID, "SI"), "AccountID", "AccountName");

      public IEnumerable GetIEnumerableAccountDetails(int userID, string psp)
        {
            SqlConnection con = Getcon();
            SqlCommand cmd = new SqlCommand("GetUserAccount", con);
            cmd.Parameters.AddWithValue("@pSp", psp);
            cmd.Parameters.AddWithValue("@puserID", userID);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Close();
            return dt.AsEnumerable().Select(row =>
            {
                return new AccountDetail
                {
                    AccountID = Convert.ToInt32(row["AccountID"]),
                    AccountName = row["AccountName"].ToString()

                };
            });
        }

Java Script function to check value interger or not in mvc

    function CheckVal()
    {
        var data = document.getElementById("nameVal").value;
        if (isNaN(data)) {
            alert("data is should be integer")
            false;
        }
        else
            true;
    }
view
@Html.TextBox("nameVal")

8/5/14

this is example to change controller name to other name in the url

routes.MapRoute( "sample",

"nick/{action}",

new { controller = "Sample", action = "Verify" });

this is example to change controller name to other name in the url
);

good article about mvc routing

http://www.campusmvp.net/blog/asp-net-mvc-friendlier-action-names-and-controllers

8/4/14

calling action on button click

<input type="button" value="Create" onclick="location.href='@Url.Action("FromAccount", "Employee")'" />

jquery grid