使用复选框保存到m-2-m

时间:2013-09-19 16:04:06

标签: c# asp.net-mvc post checkbox many-to-many

所以我正在尝试使用[Slauma的答案] [1]

到目前为止我做了什么,

模型 ViewModelProspectUsers

    public int Id { get; set; }
    public string User { get; set; }
    public IEnumerable<ViewModelUserProspectSelect> Prospects { get; set; }

模型 ViewModelUserProspectSelect

    public int ProspectID { get; set; }
    public string Name { get; set; }
    public bool IsSelected { get; set; }

查看 UserInProspect

@model OG.ModelView.ViewModelProspectUsers

@using (Html.BeginForm())
{

<div class="container">
    <div class="contentContainer">
        @Html.HiddenFor(model => model.Id)
        Prospects for User <h3>@Html.DisplayTextFor(model => model.User)</h3>
    </div>
    <div class="contentContainer2">
        <h5>Please Select Prospects you wish to assign to this User.</h5>
        <p></p>

            @Html.EditorFor(model => model.Prospects)

    </div>
    <input type="submit" value="Save changes" />
    @Html.ActionLink("Cancel", "Index")
</div>
}

编辑器视图位于ChangeUserInfo / EditorTemplates / * _ ViewModelUserprospectSelect.cshtml *

@model OG.ModelView.ViewModelUserProspectSelect

@Html.HiddenFor(model => model.ProspectID)
@Html.HiddenFor(model => model.Name)
test
@Html.LabelFor(model => model.IsSelected, Model.Name)
@Html.EditorFor(model => model.IsSelected)

[获取] 方法 UserInProspect 操作

public ActionResult UsersInProspect(int id = 0)
    {
        var data = db.UserProfiles
        .Where(s => s.UserID == id)
        .Select(s => new
        {
            ViewModel = new ViewModelProspectUsers
            {
                Id = s.UserID,
                User = s.UserName
            },
            prospects = s.Prospects.Select(c => c.ProspectID)
        })
        .SingleOrDefault();

        if (data == null)
            return HttpNotFound();

        // Load all companies from the DB
        data.ViewModel.Prospects = db.Prospect
            .Select(c => new ViewModelUserProspectSelect
            {
                ProspectID = c.ProspectID,
                Name = c.ProspectName
            })
            .ToList();

        // Set IsSelected flag: true (= checkbox checked) if the company
        // is already related with the subscription; false, if not
        foreach (var c in data.ViewModel.Prospects)
            c.IsSelected = data.prospects.Contains(c.ProspectID);

        return View(data.ViewModel);

    }

[HttpPost] 方法 UserInProspect 操作

    public ActionResult UsersInProspect(ViewModelProspectUsers viewModel)
    {
        if (ModelState.IsValid)
        {
            var subscription = db.UserProfiles.Include(s => s.Prospects)
                .SingleOrDefault(s => s.UserID == viewModel.Id);

            if (subscription != null)
            {
                // Update scalar properties like "Amount"
                //subscription.Prospects = viewModel.Prospects;
                //subscription. = subscription.
                //List<string> myList = new List<string>();
                //myList = viewModel.Prospects.Cast<String>().ToList();

                //IEnumerable<dbProspect> Isubscription = subscription.Prospects;
                ////or explicit:
                //var iPersonList = (IEnumerable<dbProspect>)myList;


                // or more generic for multiple scalar properties
                // _context.Entry(subscription).CurrentValues.SetValues(viewModel);
                // But this will work only if you use the same key property name
                // in ViewModel and entity

                foreach (var prospect in viewModel.Prospects)
                {
                    if (prospect.IsSelected)
                    {
                        if (!subscription.Prospects.Any(
                            c => c.ProspectID == prospect.ProspectID))
                        {
                            // if company is selected but not yet
                            // related in DB, add relationship
                            var addedProspect = new dbProspect { ProspectID = prospect.ProspectID };
                            db.Prospect.Attach(addedProspect);
                            subscription.Prospects.Add(addedProspect);
                        }
                    }
                    else
                    {
                        var removedProspect = subscription.Prospects
                            .SingleOrDefault(c => c.ProspectID == prospect.ProspectID);
                        if (removedProspect != null)
                            // if company is not selected but currently
                            // related in DB, remove relationship
                            subscription.Prospects.Remove(removedProspect);
                    }
                }

                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        return View(viewModel);
    }

1 个答案:

答案 0 :(得分:2)

  

使用添加了对我的控制器的引用   OG.Views.ChangeUsersInfo.EditorTemplates;

再次删除它。 &#34; EditorTemplates&#34;不应该是命名空间。

  

添加了编辑模板   查看/ ChangeUsers /信息/ EditorTemplates / ViewModeluserProspectSelect.cs

&#34;编辑模板&#34;不是C#(.cs)代码文件,而是Razor视图(.cshtml)。将文件ViewModelUserProspectSelect.cs移动到ViewModelProspectUsers.cs所在的文件夹,并将其名称空间更改为两个类(OG.Models)的相同名称。

(为什么路径中有一个子文件夹/Info/?或者它是一个拼写错误而只是Views/ChangeUsersInfo/EditorTemplates?我假设控制器的名称为ChangeUsersInfoController,右?)

然后在ViewModelUserProspectSelect.cshtml文件夹中创建一个新文件Views/ChangeUsersInfo/EditorTemplates,其中包含来自其他答案的视图,此文件:

@model OG.Models.ViewModelUserProspectSelect

@Html.HiddenFor(model => model.ProspectID)
@Html.HiddenFor(model => model.Name)

@Html.LabelFor(model => model.IsSelected, Model.Name)
@Html.EditorFor(model => model.IsSelected)

主视图中的contentContainer2 div元素应如下所示:

<div class="contentContainer2">
    <h5>Please Select Prospects you wish to assign to this User.</h5>

    @Html.EditorFor(model => model.Propects)
</div>
相关问题