提交表单

时间:2017-07-05 18:10:36

标签: c# asp.net .net razor model-view-controller

此处的问题是,在提交表单时,UserCustomerClaims对象中的ApplicationUserEditViewModel属性列表为null; for循环中的复选框的响应没有或正在进入控制器。

查看

@model  WaterManagementSystem.Features.Admin.ApplicationUserEditViewModel

@{
  ViewBag.Title = "Edit User";
  Layout = "_Layout";
}

<form asp-action="UpdateUser" asp-controller="Admin" method="post">

    ...
  <div style="color: White;">

  @foreach (var userClaim in Model.UserCustomerClaims)
  { 
    <div>
        <input type="checkbox" asp-for="@userClaim.HasClaim" id="@userClaim.ID" name="@userClaim.ID" checked="@(userClaim.HasClaim)" /> 
        <input type="hidden" asp-for="@userClaim.ID" /> @userClaim.UserClaim.Type
    </div>
  }
  </div>
  <div>
    <div>
      <button type="submit" class="btn btn-default">Save</button>
    </div>
  </div>
</form>

查看模型

public class ApplicationUserEditViewModel : ApplicationUserViewModel
{
  ...
  private List<UserCustomerClaim> userCustomerClaims_ = null;

  public ApplicationUserEditViewModel() : this(new ApplicationUser())
  {
    userCustomerClaims_ = new List<UserCustomerClaim>();
  }

  public List<UserCustomerClaim> UserCustomerClaims
  {
    get
    {
      return userCustomerClaims_;
    }
  }


  public class UserCustomerClaim
  {
    public UserCustomerClaim(Claim userClaim, bool hasClaim)
    {
      UserClaim = userClaim;
      HasClaim = hasClaim;
      ID = Guid.NewGuid();
    }

    public Guid ID { get; set; }

    public Claim UserClaim { get; set; }
    public bool HasClaim { get; set; }
  }
}

控制器

[HttpPost]
public async Task<ActionResult> UpdateUser(ApplicationUserEditViewModel vm)
{
  ...
}

建议的副本不使用asp-for标记助手。

0 个答案:

没有答案