MVC多个复选框值到字符串

时间:2018-05-10 08:11:24

标签: asp.net-mvc forms

我正在尝试获取多个复选框值,然后将它们作为一个字符串传递给另一个属性。

在我看来,我有以下内容来获取复选框值:

    @Html.CheckBoxFor(m => m.Consent.ConsentToPost, new { @class = "consentGiven" })
    @Html.CheckBoxFor(m => m.Consent.ConsentToEmail, new { @class = "consentGiven" })
    @Html.CheckBoxFor(m => m.Consent.ConsentToTelephone, new { @class = "consentGiven" })
    @Html.CheckBoxFor(m => m.Consent.ConsentToTextMessage, new { @class = "consentGiven" })
    @Html.CheckBoxFor(m => m.Consent.ConsentNotGiven, new { @class = "consentNotGiven" })

然后在我的模型中,我正在尝试创建一个名为Consent的数组,然后将其转换为字符串,将其作为一个字符串传递给Additional Details属性:

public List<Consent> Consent { get; set; }

public override CallbackDetails ConvertToDto()
{
  var callbackDetails = base.ConvertToDto();
  callbackDetails.Postcode = Postcode;
  callbackDetails.AdditionalDetail = Consent.ToString();
  return callbackDetails; <---- Error - Cannot implicitly convert type 'xxxxxx.Models.Consent' to 'string'---
}


public class Consent {
   public bool ConsentNotGiven { get; set; }
   public bool ConsentToPost { get; set; }
   public bool ConsentToEmail { get; set; }
   public bool ConsentToTelephone { get; set; }
   public bool ConsentToTextMessage { get; set; }
}

在我的callbackDetails模型中,我有其他详细信息属性:

public string AdditionalDetail { get; set; }

我怎样才能做到这一点?谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

你试过覆盖Consent类的toString方法吗?

public class Consent {
   public bool ConsentNotGiven { get; set; }
   public bool ConsentToPost { get; set; }
   public bool ConsentToEmail { get; set; }
   public bool ConsentToTelephone { get; set; }
   public bool ConsentToTextMessage { get; set; }


   public override string ToString()
    {
        return this.ConsentNotGiven.toString() + " || " + this.ConsentToPost.toString() + " || " + this.ConsentToEmail.toString() + " || " + this.ConsentToTelephone.toString() + " || " + this.ConsentToTextMessage.toString();
    }
}