Select-tag上的Ng模型返回null / undefined

时间:2015-06-26 12:23:25

标签: angularjs

我试图保存用户创建的帖子,我一直在用户名上留空,以便后端崩溃。

HTML:

<select name="user" class="input">
   <option value="" ng-model="post.Username" ng-repeat="user in users">{{user.Name}}</option>
</select>

控制器:

var NewPostController = function ($scope, $location, $routeParams, Post) {
    $scope.post = new Post();
    $scope.save = function () {
        Post.save($scope.post, function () {
            console.log($scope.post);
            $location.path('/');
        });
}

这是我的DAL部分:

public void AddPost(Post post)
    {
        SqlConnection con = new SqlConnection("Server=localhost;Database=XXX;User Id=XXX;Password=XXX;");
        string sql = "INSERT INTO Post (Title, Comment, Date, Imagelink, Username) VALUES(@Title, @Comment, @Date, @Imagelink, @Username)";
        con.Open();
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.AddWithValue("@Title", post.Title);
        cmd.Parameters.AddWithValue("@Comment", post.Comment);
        cmd.Parameters.AddWithValue("@Date", "2015");
        cmd.Parameters.AddWithValue("@Imagelink", post.ImageLink);
        cmd.Parameters.AddWithValue("@Username", post.Username);
        cmd.ExecuteNonQuery();
    }

1 个答案:

答案 0 :(得分:1)

你应该选择ng-model。此外,您需要选择的值为username(user.Name)。

试试这个:

<select name="user" class="input" ng-model="post.Username">
 <option value="{{user.Name}}" ng-repeat="user in users">{{user.Name}}</option>
</select>