1

In my view I have a tag with an ng-bind attribute that is showing the correct boolean value:

<span id="ShowFlag" name="ShowFlag" ng-bind="session.view.showFlag"></span>

When the form is posted on the server side I would like to bind this to a property on the relevant model.

public bool ShowFlag { get; set; }

However, this is always returning false, whereas the value shown in Span tag is showing correctly as true on the page. Is there something obvious I'm missing here?

3
  • In chrome developer console in the network tab check what you're actually posting back to the server. Maybe the answer lies there. Commented Dec 3, 2015 at 15:11
  • The issue seems to be with the MVC model binding- I can see this is being set in the property on the model to false when debugging the POST controller action. Commented Dec 3, 2015 at 15:21
  • So you're sure the actual post contains a 'true' at ShowFlag? Are other fields being correctly set? Commented Dec 3, 2015 at 15:37

3 Answers 3

2

I think you're something you're missing about how AngularJs binding works. if you want to get a value from the server into an angular model you can use Razor to get that data into JavaScript (the best place is in your Angular controller.)

Here is a quick sample I put together.

This is code from the MVC Controller. In this example we are using Model data and ViewBag data.

public ActionResult Index()
{
    dynamic model = new ExpandoObject();
    model.ShowFlag = "True";

    ViewBag.ShowFlag = "ViewBag True";
    return View(model);
}

This is what the view looks like including reference so Angular, JQuery and the code for the AngularJs app and controller:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Demo</title>
</head>
<body>
    <div>
        <h2>Sample For Stack Overflow</h2>

        <div ng-app="glennapp">
            <div ng-controller="testController">
                <input type="text" ng-model="showFlag" />
                <input type="text" ng-model="showFlag2" />

                <div>
                    <span ng-bind="showFlag" ></span>
                    <span ng-bind="showFlag2" ></span>
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript" src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="//code.angularjs.org/1.4.8/angular.min.js"></script>
    <script type="text/javascript">
        var mainApp = angular.module('glennapp', ['glennControllers']);
        var glennControllers = angular.module('glennControllers', []);
        glennControllers.controller('testController', ['$scope', function ($scope) {

            $scope.showFlag = '@ViewBag.ShowFlag';
            $scope.showFlag2 = '@Model.ShowFlag';
        }]);
    </script>
</body>
</html>

Another option would be to create an MVC action that returns JsonResult and then write some JavaScript to make an Ajax call and retrieve the data.

Sign up to request clarification or add additional context in comments.

Comments

1

When posting a form only input and select tag values are passed to the server in you case ShowFlag is a span, so you need to make it an input:

<input type="checkbox" id="ShowFlag" name="ShowFlag" ng-bind="session.view.showFlag"/>

If you are posting to server with ajax, make sure that you serialize your model properly:

for example for the following action:

public ActionResult (FlagsConatiner container)
{
   //
} 

public class FlagsConatiner 
{
   public bool ShowFlag { get; set; }
} 

Serialized model should look like this:

{
   "ShowFlag":"true"
}

Comments

0

As pointed out above, you must use an input for the binding to be successful. I used the following which is now working:

<input type="hidden" id="ShowFlag" name="ShowFlag" ng-value="session.view.showFlag">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.