Click here to Skip to main content
15,892,674 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionWeb api insert failed but code seems fine Pin
DotNetFellow1-Jul-16 19:14
DotNetFellow1-Jul-16 19:14 
QuestionRadiobuttonlist choice disables requiredfieldvalidator in textboxes and other radiobuttonlist Pin
Codeaddict742330-Jun-16 9:25
Codeaddict742330-Jun-16 9:25 
QuestionRe: Radiobuttonlist choice disables requiredfieldvalidator in textboxes and other radiobuttonlist Pin
ZurdoDev30-Jun-16 10:28
professionalZurdoDev30-Jun-16 10:28 
AnswerRe: Radiobuttonlist choice disables requiredfieldvalidator in textboxes and other radiobuttonlist Pin
Codeaddict742330-Jun-16 10:35
Codeaddict742330-Jun-16 10:35 
GeneralRe: Radiobuttonlist choice disables requiredfieldvalidator in textboxes and other radiobuttonlist Pin
Richard Deeming30-Jun-16 10:52
mveRichard Deeming30-Jun-16 10:52 
GeneralRe: Radiobuttonlist choice disables requiredfieldvalidator in textboxes and other radiobuttonlist Pin
Codeaddict742330-Jun-16 11:16
Codeaddict742330-Jun-16 11:16 
GeneralRe: Radiobuttonlist choice disables requiredfieldvalidator in textboxes and other radiobuttonlist Pin
Richard MacCutchan30-Jun-16 22:26
mveRichard MacCutchan30-Jun-16 22:26 
QuestionAngularjs is not refreshing data on the CSHTML page Pin
indian14330-Jun-16 2:39
indian14330-Jun-16 2:39 
Hi All,

I have three Action methods on in a Controller in ASP.Net MVC application and I have one cshtml page, cshtml page is using an AngularJS controller which in turn calls 2nd action method.
First Action method calls the View that is cshtml page that cshtml page calls the AngularJS controller which executes second Action method to get the data onto the cshtml page.
Data refreshing is working upto this.
Now the 3rd Action method which is inserting data into the database, when it is calling the first action method then the AngularJS code on the cshtml file is not executing to call the 2nd action method in the Controller.

Here is my View code (this part
not refreshing so that I can have the latest data that's added)
@using MVCWithWebApiApp.Models;
@using System.Linq;

@model IEnumerable<MVCWithWebApiApp.Models.User>

@{
    ViewBag.Title = "Users List";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Users List</title>
    <script src="~/Scripts/jquery-2.2.4.min.js"></script>
    <script src="~/Scripts/angular.min.js"></script>
    <script src="~/Scripts/myApp.js"></script>
</head>

<body><br />
   <div data-ng-app="myApp" data-ng-controller="userController">
        <table style="border:none 0px gray;">
            <tr>
              <td>
        <table style="border:solid 1px gray;">
            <tr>
                <td style="border:solid 1px gray;">User Id</td>
                <td style="border:solid 1px gray;">User Name</td><br />
            </tr>
            <tr data-ng-repeat="usr in users">
                <td><a href="/User/UpdateUser/{{usr.UserId}}"> {{usr.UserId}} </a> </td>
                <td><a href="/User/UpdateUser/{{usr.UserId}}">{{usr.UserName}}</a></td><br />
            </tr>
        </table>

<pre>
            </td>
            <td>
                 @Html.ActionLink("Create User", "CreateUser")
            </td>
        </tr>
    </table>

</body>
</html>


Here is my Controller code:
using MVCWithWebApiApp.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCWithWebApiApp.Controllers
{
    public class UserController : Controller
    {
        public ActionResult Index()
        {
            var dbContext = new MVCDBContext();

            List<User> listOfUsers;
            listOfUsers = dbContext.Users.ToList();

            return View(listOfUsers);
        }

        public ActionResult GetUsers()
        {
            var dbContext = new MVCDBContext();

            List<User> listOfUsers;
            listOfUsers = dbContext.Users.ToList();

            return Json(listOfUsers, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /User/Create
        public ActionResult CreateUser()
        {<br />
            return View();
        }

        //
        // POST: /User/Create
        [HttpPost]
        public ActionResult CreateUser(FormCollection collection)
        {
            try
            {
                string Id = Request.Form["UserId"];
                string UserName = Request.Form["UserName"];
                int UserId;

                var dbContext = new MVCDBContext();
                User user = new User();
                user.UserId = (int.TryParse(Id, out UserId) ? UserId : 0);
                user.UserName = UserName;

                dbContext.Users.Add(user);
                dbContext.SaveChanges();

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

My Model is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCWithWebApiApp.Models
{
    public class User
    {
        private int _userId;
        public int UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        private string _userName;
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

    }
}

And the .js file of the AngularJS code
var myApp = angular.module('myApp', []);

myApp.controller('userController', ['$scope', '$http', function ($scope, $http) {
    $scope.users = "";
    $http.get("/User/GetUsers")
    .success(function (result) {        
        $scope.users = result;
    })
    .error(function (result) {
        console.log(result);
    });
}]);

Any help is going to be very very helpful thanks
Thanks,

Abdul Aleem

"There is already enough hatred in the world lets spread love, compassion and affection."

AnswerRe: Angularjs is not refreshing data on the CSHTML page Pin
John C Rayan30-Jun-16 21:24
professionalJohn C Rayan30-Jun-16 21:24 
GeneralRe: Angularjs is not refreshing data on the CSHTML page Pin
indian1431-Jul-16 0:40
indian1431-Jul-16 0:40 
GeneralRe: Angularjs is not refreshing data on the CSHTML page Pin
John C Rayan1-Jul-16 1:18
professionalJohn C Rayan1-Jul-16 1:18 
GeneralRe: Angularjs is not refreshing data on the CSHTML page Pin
indian1431-Jul-16 4:15
indian1431-Jul-16 4:15 
GeneralRe: Angularjs is not refreshing data on the CSHTML page Pin
John C Rayan4-Jul-16 0:22
professionalJohn C Rayan4-Jul-16 0:22 
QuestionWhen to use ASP.NET Webforms, MVC, Web API Pin
larsp77729-Jun-16 0:04
larsp77729-Jun-16 0:04 
AnswerRe: When to use ASP.NET Webforms, MVC, Web API Pin
F-ES Sitecore30-Jun-16 1:03
professionalF-ES Sitecore30-Jun-16 1:03 
GeneralRe: When to use ASP.NET Webforms, MVC, Web API Pin
larsp77730-Jun-16 1:37
larsp77730-Jun-16 1:37 
GeneralRe: When to use ASP.NET Webforms, MVC, Web API Pin
F-ES Sitecore30-Jun-16 2:12
professionalF-ES Sitecore30-Jun-16 2:12 
GeneralRe: When to use ASP.NET Webforms, MVC, Web API Pin
larsp77730-Jun-16 2:22
larsp77730-Jun-16 2:22 
GeneralRe: When to use ASP.NET Webforms, MVC, Web API Pin
F-ES Sitecore30-Jun-16 3:02
professionalF-ES Sitecore30-Jun-16 3:02 
GeneralRe: When to use ASP.NET Webforms, MVC, Web API Pin
larsp77730-Jun-16 3:13
larsp77730-Jun-16 3:13 
GeneralRe: When to use ASP.NET Webforms, MVC, Web API Pin
Ronald.A11-Jul-16 4:05
Ronald.A11-Jul-16 4:05 
GeneralRe: When to use ASP.NET Webforms, MVC, Web API Pin
Ronald.A11-Jul-16 3:47
Ronald.A11-Jul-16 3:47 
GeneralRe: When to use ASP.NET Webforms, MVC, Web API Pin
larsp77711-Jul-16 3:56
larsp77711-Jul-16 3:56 
GeneralRe: When to use ASP.NET Webforms, MVC, Web API Pin
Ronald.A11-Jul-16 4:03
Ronald.A11-Jul-16 4:03 
QuestionLooking for help implementing a progress bar in ASP Pin
turbosupramk328-Jun-16 9:35
turbosupramk328-Jun-16 9:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.