增加功能同时使用复选框MVC中创建用户 [英] Adding features while creating User in mvc using checkbox

查看:75
本文介绍了增加功能同时使用复选框MVC中创建用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建具有特殊功能的MVC用户。当用户要创建我想一些特别的功能分配给喜欢有自己的房子特定用户的每一个用户,使用复选框选择有他自己的车。特定的功能驻留在不同的表命名的功能。那么,我怎么可以添加这些功能与用户在创建用户。

i want to create users with special features in mvc. when user is going to create i want to assign some special feature to each user like particular user having his own house, having his own car using checkbox selection. the particular feature is reside in different table named feature. then how can i add those features with user while creating the user.

我已经创建了一个名为ViewModelUserWithFeature视图模型

i have created a view model named 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();
        }
}

这是我的观点中,我希望两个文本框和它打算通过复选框以选择的功能列表,并与用户的连接。

here is my view in which i want to two textboxes and a list of features which are going to select by checkbox and attached with the user.

@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);
}

无法做到这一点是我迄今为止尝试过我已经提到。请帮忙。先谢谢了。

not able to achieve that what i have tried so far i have mentioned . please help. thanks in advance.

推荐答案

使用视图模型重新present你显示和编辑

Use view models to represent what you display and edit

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" />
}

这篇关于增加功能同时使用复选框MVC中创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆