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

Monday, May 14, 2012

Using jQuery and JSON Action methods in MVC

  1. jQuery + JSON Action Methods = Cool
    It is easy to return a JSON object instead of a view.
    public JsonResult Create(string CategoryName)
    {
        var category = new Models.Category();
        category.Name = CategoryName;
        category.URLName = CategoryName.ToLower().Replace(" ", "-");
        categoryRepository.Add(category);
        categoryRepository.Save();
    
        return Json(category);
    }
    <script class="str" type="<span">
    "text/javascript" language="javascript">
        $("#CreateNewCategory").click(function() {
            $.getJSON("/category/create/",
                      { "CategoryName": $("#NewCategoryName").val() },
                      CategoryAdded);
                  });
    
                  function CategoryAdded(category) {
                      $("#CategoryList").append("
    
  2. + category.URLName + "/cat\">" + category.Name + "
  3. "
    ); } </script>

Sunday, May 13, 2012

How to call javascript function on the change of Dropdown List in ASP.NET MVC?

How to call javascript function on the change of Dropdown List in ASP.NET MVC?
Create a java-script function:
Call the function:
<%:Html.DropDownListFor(x => x.SelectedProduct,
new SelectList(Model.Products, "Value", "Text"),
"Please Select a product", new { id = "dropDown1",
onchange="selectedIndexChanged()" })%>