填充数据库中的复选框

时间:2015-01-14 16:42:18

标签: c# asp.net-mvc entity-framework

我想显示爬升列表,然后用户可以选择攀爬。

我这样做:

public ActionResult GetClimbs(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    UserProfile user = db.userProfiles
        .Include(i => i.Climbs)

        .Where(i => i.Id == id)
        .Single();
    PopulateAssignedClimbData(user);
    if (user == null)
    {
        return HttpNotFound();
    }
    return View(user);
}



private void PopulateAssignedClimbData(UserProfile user)
{
    var allClimbs = db.Climbs;
    var userClimbs = new HashSet<int>(user.Climbs.Select(c => c.climbID));
    var viewModel = new List<AssignedClimb>();
    foreach (var climb in allClimbs)
    {
        viewModel.Add(new AssignedClimb
        {
            ClimbID = climb.climbID,
            Title = climb.Name,
            Assigned = userClimbs.Contains(climb.climbID)
        });
    }
    ViewBag.Climbs = viewModel;
}

并且对于视图我有这个:

@model ContosoUniversity.Models.UserProfile
@using ContosoUniversity.Source
@{
    ViewBag.Title = "Edit";
}


@using (Html.BeginForm("GetClimbs", "Account", FormMethod.Post, new { id = "form_Id", enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.HiddenFor(model => model.Id)


        <table>
            <tr>

        @{
            int cnt = 0;
            List<ContosoUniversity.ViewModels.AssignedClimb> climbs = ViewBag.Climbs;


            foreach (var climb in climbs)
            {  
                if (cnt++ % 3 == 0)
                {
                    @:</tr><tr>
                }
                @:<td>
                    <input type="checkbox"
                           name="selectedClimbs"
                           value="@climb.ClimbID"
                           @(Html.Raw(climb.Assigned ? "checked=\"checked\"" : ""))/>

                           @climb.ClimbID @: @climb.Title




                 @:</td>
            }
            @:</tr>
        }
            </table>



    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

但是每当我通过这一行得到消息时:

foreach (var climb in climbs)

虽然我在数据库中有一些攀登,但攀登是空的。

它也不会在控制器中输入GetClimbs方法。

所以视图是一个标签,但是如何从标签触发动作方法:PopulateAssignedClimbData(user);此方法位于帐户控制器

谢谢

这是我对所有标签的编辑:

<div id="tabs">
    <ul>
        <li><a href="#tabs-1">Personal information</a></li>
        <li><a href="#tabs-2">Profile Photo</a></li>
        <li><a href="#tabs-3">Other Photos</a></li>
        <li><a href="#tabs-4">Climbs</a></li>
    </ul>
    <div id="tabs-1">
        @Html.Partial("_GetUserProfile", Model)
    </div>
    <div id="tabs-2">
        @Html.Partial("_GetProfilePicture", Model)
    </div>
    <div id="tabs-3">
        @Html.Partial("_GetOtherImages", Model)
    </div>
    <div id="tabs-4">
        @Html.Partial("_GetClimbTab", Model)
    </div>

</div>

这是在做这项工作:

$.get('@Url.Action("PopulateAssignedClimbData", "Account", new { id = Model.Id })', function (data) {
                $('#_GetClimbTab').html(data);
            });

0 个答案:

没有答案