使用复选框在mvc中创建用户时添加功能

时间:2014-11-21 16:38:28

标签: asp.net-mvc asp.net-mvc-4 checkbox

我想在mvc中创建具有特殊功能的用户。当用户打算创建时,我想为每个用户分配一些特殊功能,比如拥有自己房子的特定用户,使用复选框选择拥有自己的汽车。特定功能位于不同的表名为feature。那么如何在创建用户时为用户添加这些功能。

我创建了一个名为ViewModelUserWithFeature

的视图模型
public class ViewModelUserWithFeature
{
        public User User { get; set; }
        public Feature Feature { get; set; }
        public List<Feature> feature { get; set; }
        public IEnumerable<User> IUser { get; set; }
        private UserDbContext userDbContext;
        private IUserService userService;

 public void ViewUserList()
        {
            userService = new RoleService(userDbContext);
            IUser = userService.GetUsers();

        }
        public void AddNewUser(User userAdd)
        {
            userService = new UserService(userDbContext);
            User = userService.AddUser(userAdd);
            userService.SaveUser();
        }
}

这是我的视图,其中我想要两个文本框和一个功能列表,这些功能将通过复选框选择并附加到用户。

@model App.ViewModel.ViewModelUserWithFeature
 @using (Html.BeginForm("Create", "User", FormMethod.Post))
    {
 <div>
 @Html.TextBoxFor(m => m.User.UserName)
                @Html.ValidationMessageFor(m => m.UserName)
            </div>
 <div>
 @Html.TextBoxFor(m => m.User.UserAddres)
                @Html.ValidationMessageFor(m => m.UserAddres)
            </div>
@for(int i=0; i < Model.Feature; i++)
{
<div class="cb"><input type="checkbox" name="checkbox"></div>
 <div class="per-content">
<label for="1"> Model.Feature.FeatureName</div>
}
<div>
 <button type="submit" id="btn-rd">Submit</button>
</div>
}

控制器

[HttpPost]
        public ActionResult Create(User user)
        {
 ViewModelUserWithFeature viewModelUserWithFeature = new ViewModelUserWithFeature(usertDbContext);
                if (ModelState.IsValid)
                {


                    viewModelUserWithFeature.AddNewUser(user);


                }
                return RedirectToAction("Index", viewModelUserWithFeature);
}

无法实现我迄今为止所提到的尝试。请帮忙。提前谢谢。

2 个答案:

答案 0 :(得分:0)

尝试使用此功能,在您的功能模型中添加新属性

public bool isFeatureOf { get; set; }

也在您的模型中为AddNewUser方法将其更改为

    public void AddNewUser(User userAdd,List<Feature> features)
    {
        userService = new UserService(userDbContext);
        User = userService.AddUser(userAdd);
        userService.SaveUser();

        //featureService = new FeatureService(yourdbcontext)
        foreach (Feature item in features)
        {
            //save to db
          featureService.SaveFeature(item,User.Id);
          //i don't know if in your database you already have a table,colum or something to map the features by user
        }

    }

然后在你看来

for(int index=0; index < Model.Features.Count(); index++)
{
  @Html.HiddenFor(m=>Model.Features[index].NameFeature) 
  @Html.Raw(Model.Features[index].NameFeature)
  @Html.CheckBoxFor(m=>Model.Features[index].isFeatureOf)

}

在您的视图中,您还需要更改此

     <div>
        @Html.TextBoxFor(m => m.User.UserName)
            @Html.ValidationMessageFor(m => m.UserName)
        </div>
    <div>
       @Html.TextBoxFor(m => m.User.UserAddres)
            @Html.ValidationMessageFor(m => m.UserAddres)
        </div>

为:

      <div>
      @Html.TextBoxFor(m =>Model.User.UserName)
            @Html.ValidationMessageFor(m => Model.User.UserName)
        </div>
   <div>
         @Html.TextBoxFor(m => m.User.UserAddres)
            @Html.ValidationMessageFor(m =>Model.User.UserAddres)
        </div>
在您的控制器中

更改您的参数以获得整个模型

    [HttpPost]
    public ActionResult Create(ViewModelUserWithFeature model)
    {
           if (ModelState.IsValid)
            {
              model.AddNewUser(model.User,model.Features);
            } 
    return RedirectToAction("Index", viewModelUserWithFeature);
    }

希望这可以帮到你

答案 1 :(得分:0)

使用视图模型来表示您显示和编辑的内容

public class FeatureVM
{
  public int ID { get; set; }
  public string Name { get; set; }
  public bool IsSelected { get; set; }
}

public class UserVM
{
  public string Name { get; set; }
  public string Address { get; set; }
  public List<FeatureVM> Features { get; set; }
}

控制器

public ActionResult Create()
{
  UserVM model = new UserVM();
  model.Features = // map all available features
  return View(model);
}

[HttpPost]
public ActionResult Create(UserVM model)
{

}

查看

@model UserVM
@using(Html.BeginForm())
{
  @Html.LabelFor(m => m.Name)
  @Html.TextBoxFor(m => m.Name)
  @Html.ValidationMessageFor(m => m.Name)
  .....
  for(int i = 0; i < Model.Features.Count; i++)
  {
    @Html.HiddenFor(m => m.Features[i].ID)
    @Html.CheckBoxFor(m => m.Features[i].IsSelected)
    @Html.LabelFor(m => m.Features[i].IsSelected, Model.Features[i].Name)
  }
  <input type="submit" value="Create" />
}