当我发送表单时,我的复选框值结果始终为NULL

时间:2016-04-01 08:09:02

标签: asp.net-mvc checkbox

我有一个表单,其中填充了HairTags表中的一些复选框,

一切正常,我在我的表单中显示我的复选框,但当我选择其中一些并尝试发送时,所有值结果为NULL并且我不将它们保存在我的文件Creationtag中

在我的模特中

//CHECKBOXES
public class HairTagModel
{
    //[Key]
    public int Value { get; set; }
    public string Text { get; set; }
    public bool IsChecked { get; set; }
}



public class CreationHairTagsModel
{
    //public Creation Creation { get; set; }
    //public List<HairTagModel> CreationHairTags { get; set; }

    public CreationHairTagsModel()
    {
        Creation = new Creation();
    }
    public Creation Creation { get; set; }

    private ApplicationDbContext creationdb = new ApplicationDbContext();

    public List<HairTagModel> CreationHairTags
    {
        get
        { 
            var HairTagList = creationdb.HairTags.ToList();

            List<HairTagModel> obj = new List<HairTagModel>();

            foreach (var tags in HairTagList)
            {
                obj.Add(new HairTagModel
                {
                    Text = tags.HairTagTitle,
                    Value = tags.HairTagId,
                    IsChecked = false
                });
            }

            return obj;
        }
    }
}

在我的页面

<div class="col-md-12">
    @for (int i = 0; i < Model.CreationHairTags.Count; i++)
    {
        @Html.CheckBoxFor(m => Model.CreationHairTags[i].IsChecked)
        @Model.CreationHairTags[i].Text
        @Html.HiddenFor(m => Model.CreationHairTags[i].Value)
        @Html.HiddenFor(m => Model.CreationHairTags[i].Text)<br />
    }
</div>

在我的控制器中

[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult CreationUpload(CreationHairTagsModel creationbind, IEnumerable<HttpPostedFileBase> files)
{
    if (ModelState.IsValid)
    {   
    ///...CODE HIDDEN...

       //Tags
        StringBuilder sb = new StringBuilder();
        foreach (var item in creationbind.CreationHairTags)
        {
            if (item.IsChecked)
            {                                        
                sb.Append(item.Value + ",");
            }
        }
        creationbind.Creation.Creationtag = sb.ToString();
    }
}

我的代码有什么问题请帮忙解释一下?

1 个答案:

答案 0 :(得分:0)

您的模型没有为Lis​​t提供setter的方法。只是一个吸气剂。

基本上你已经将该字段设为只读。我不会涉及此代码所具有的SOLID违规行为。你可以用不同的方式从模型绑定器获取数据,试试

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>




<div class="container container-table">
  <!-- Main div to hold all Data -->

  <!-- Login Form -->
  <div class="row vertical-center-row">

    <h2 class="text-center">Login</h2>
    <form class="form-horizontal" action="login.html">
      <div class="form-group">
        <label for="inputEmail" class="control-label col-md-2 col-md-offset-3">Email</label>
        <div class="col-md-4">
          <input name="email" type="email" class="form-control" placeholder="Email" required>
        </div>
      </div>
      <div class="form-group">
        <label for="inputPassword" class="text-center control-label col-md-2 col-md-offset-3">Password</label>
        <div class="col-md-4">
          <input name="password" type="password" class="form-control" placeholder="Password" required>
        </div>
      </div>
      <div class="form-group">
        <div class="col-md-offset-5 col-md-4">
          <div class="checkbox">
            <label>
              <input type="checkbox">Remember me</label>
          </div>
        </div>
      </div>
      <button name="remember_me" type="submit" class="center-block btn btn-primary">Sign in</button>
    </form>
  </div>
</div>

模型绑定器应该使用它在表单列表中找到的值填充creationHairTags参数。