Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Model IEnumerable Ajax.BegNotify到控制器为null_Model_Controller_Null_Ajax.beginform - Fatal编程技术网

Model IEnumerable Ajax.BegNotify到控制器为null

Model IEnumerable Ajax.BegNotify到控制器为null,model,controller,null,ajax.beginform,Model,Controller,Null,Ajax.beginform,我有一个视图,其中包含通过模型传递的用户列表。我想做的是为每个用户使用一个按钮,它将用户ID传递回控制器,以便我可以存储它。类似于“相似/不相似”按钮 @model IEnumerable<FindaRoom.Models.FilterViewModel> <h3>List of friends</h3> <div class="row"> @using (Ajax.BeginForm("Add", "User", null, new Aj

我有一个视图,其中包含通过模型传递的用户列表。我想做的是为每个用户使用一个按钮,它将用户ID传递回控制器,以便我可以存储它。类似于“相似/不相似”按钮

@model IEnumerable<FindaRoom.Models.FilterViewModel>
<h3>List of friends</h3>
<div class="row">
    @using (Ajax.BeginForm("Add", "User", null, new AjaxOptions
                    {
                        HttpMethod = "POST",
                        OnSuccess = "SuccessMessage",
                        OnFailure = "FailMessage",
                    }, FormMethod.Post))
    {
        foreach (var user in Model)
        {
            <div class="col-md-3">
                <div class="thumbnail">
                    <div>
                        @Html.EditorFor(modelitem => user.FbInfo.UserId)
                    </div>
                    <input type="submit" value="Match Me" />

                </div>
            </div>
        }
    }
</div>
我的模型也是如此

public class FilterViewModel
{
    public FbInfo FbInfo { get; set; }
    public Questions Questions { get; set; }
}
public class FbInfo
    {
        public FbInfo()
        {
            this.friendsList = new List<friends>();
            this.mutualFriendsList = new List<mutualFriends>();
        }
        ...
        [Key]
        [ForeignKey("UserId")]
        public ApplicationUser User { get; set; }
        ...
    }
public class Questions
    {

            [Key]
            public int id { get; set; }
            [ForeignKey("UserId")]
            public ApplicationUser User { get; set; }
            public string UserId { get; set; }

    }

如果有人有线索,那会很有帮助。我已经在这上面停留了一段时间,不明白为什么控制器中的文本值是空的。

您的方法是错误的。代码的输出是带有模型列表和多个提交按钮的单一表单。我可以向你推荐我解决类似问题的方法

@model IEnumerable<FindaRoom.Models.FilterViewModel>
<h3>List of friends</h3>

<div class="row">
    @foreach (var user in Model)
    {
        <div class="col-md-3">
            <div class="thumbnail">
                <button data-button="@user.FbInfo.UserId" class="btn btn-danger btn-match">Match Me</button>
            </div>
        </div>
    }
</div>

@section scripts {
    <script>
        $('.btn-match').click(function () {
            _self = this;
            var userId = $(_self).attr('data-button');
            $.ajax({
                type: 'POST',
                url: '@Url.Action("Add", "User")',
                data: {
                    userId: userId
                },
                success: function (response) {
                    SuccessMessage();
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    FailMessage();
                }
            });
        })
    </script>
}

是的,我认为Ajax.beginform不是正确的方法。谢谢你的快速回复。
@model IEnumerable<FindaRoom.Models.FilterViewModel>
<h3>List of friends</h3>

<div class="row">
    @foreach (var user in Model)
    {
        <div class="col-md-3">
            <div class="thumbnail">
                <button data-button="@user.FbInfo.UserId" class="btn btn-danger btn-match">Match Me</button>
            </div>
        </div>
    }
</div>

@section scripts {
    <script>
        $('.btn-match').click(function () {
            _self = this;
            var userId = $(_self).attr('data-button');
            $.ajax({
                type: 'POST',
                url: '@Url.Action("Add", "User")',
                data: {
                    userId: userId
                },
                success: function (response) {
                    SuccessMessage();
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    FailMessage();
                }
            });
        })
    </script>
}
[HttpPost]
public ActionResult Add(string userId)
{
    return new HttpUnauthorizedResult();
    //var user = db.Users.Find(userId);
    //var curUser = UserManager.FindById(User.Identity.GetUserId());
    //curUser.Matches.Add(user);
    //db.SaveChanges();
}