2

我的反馈表中有复选框,看起来
在此处输入图像描述
我在模型中添加了复选框

  namespace CorePartners_Site2.Models
 {
public class CareerForm
{
    //...
    public List<CheckBoxes> EmploymentType { get; set; }                                       
 }

public class CheckBoxes
{
    public string Text { get; set; }
    public bool Checked { get; set; }
}
 }

添加到我的控制器

    [HttpGet]
    public ActionResult CareerForm()
    {
        CareerForm model = new CareerForm();

        model.EmploymentType = new List<CheckBoxes>
        {
            new CheckBoxes { Text = "Fulltime" },
            new CheckBoxes { Text = "Partly" },
            new CheckBoxes { Text = "Contract" }
        };

        return View(model);
    }

但后来我需要将选定的复选框添加到电子邮件中,但我不知道该怎么做。
我试过了

 public ActionResult CareerForm(CareerForm Model, HttpPostedFileBase Resume)
    {
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.BodyEncoding = Encoding.UTF8;
        string message = //...
                         "Type: " + Model.EmploymentType;
        msg.Body = message;

    //....
    }

但我收到的电子邮件只是文本类型:System.Collections.Generic.List`1[CheckBoxes]
如何使其正常工作?

4

3 回答 3

1

model.EmploymentType 是一个 List<CheckBoxes>

model.EmploymentType = new List<CheckBoxes>

您将不得不使用索引访问它的值。您正在转换为字符串 aSystem.Collection.Generic.

于 2013-05-27T07:21:26.013 回答
1

类似以下的东西

string message = "Type: ";

foreach(var item in Model.EmploymentType)
{
    if (item.Checked)
        message += item.Text;
}
于 2013-05-27T07:23:28.920 回答
1

您需要从列表中的每个选中复选框中获取文本值。

这是一个列表:

model.EmploymentType = new List<CheckBoxes>...

你想要检查的:

var checked = model.EmploymentType.Where(x => x.Checked);

然后你想要Text那些盒子里的属性:

string message = "Type: " + checked.Text;

把它放在你的控制器动作中,我希望它看起来像这样:

public ActionResult CareerForm(CareerForm Model, HttpPostedFileBase Resume)
{
    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
    msg.BodyEncoding = Encoding.UTF8;
    string message = "Type: ";
    foreach(var box in Model.EmploymentType.Where(x => x.Checked)) {
        message += box.Text + " ";
    }
    msg.Body = message;
}
于 2013-05-27T07:28:09.820 回答