我是否应该在MVC 5中绑定POST中的html.dropdownlistfor?

时间:2014-03-11 12:43:11

标签: asp.net-mvc-5

  

这是我的获取

[Authorize]
public ActionResult SendMessage()
{
    var model = new MessageModel();
    var subjects = this.GetDistributionLists();
    if (subjects != null) {
        ViewBag.Subjects = new SelectList(subjects, "Value", "Text");
    }
    //Get patients list
    var patients = this.GetPatientsList();
    if (patients != null)
    {
        ViewBag.Patients = new SelectList(patients, "Value", "Text");
    }
    if (Request.IsAjaxRequest())
    {
        return PartialView("SendMessagePartial");
    }
    else
    {
        return View();
    }
}
  

这是我的帖子

[HttpPost]
[Authorize]
public ActionResult SendMessage(MessageModel model)
{
    try {
      if (ModelState.IsValid)
      {
          XmlDocument requestXml = XmlUtil.CreateRequestDocument("message", new
          {
              Message = model.MessageBody,
              Type = model.Subject,
              PatientID = model.PatientId,
              RecipientID = model.RecipientId,
              IsUrgent = model.IsUrgent ? "1" : "0"
          });
        //save message logic here
      }
    }
    catch (Exception ex)
    {
        ModelState.AddModelError("ServerMessage", ex.Message);
    }
    if (Request.IsAjaxRequest())
    {
        return PartialView("SendMessagePartial", model);
    }
    else
    {
        return View(model);
    }
}
  

这是我的模特

public class MessageModel
{
    [DisplayName("RecipientId")]
    public int RecipientId { get; set; }

    [DisplayName("Sender")]
    [StringLength(255)]
    public string Sender { get; set; }

    [DisplayName("SenderId")]
    public int SenderId { get; set; }

    [DisplayName("Message")]
    [StringLength(4000)]
    [Required]
    public string MessageBody { get; set; }

    [DisplayName("Subject")]
    [StringLength(255)]
    [Required]
    public string Subject { get; set; }

    [DisplayName("Patient")]
    [Required]
    public int PatientId { get; set; }

    public bool IsUrgent { get; set; }
}
  

我的视图有一个类似

的下拉列表
@Html.DropDownListFor(m => m.Subject, (SelectList)ViewBag.Subjects, new { @class = "form-control" })
  

当我获得时,一切都很好。   当我发布POST时,数据会被保存但在UI中我会收到一条错误

     

具有“主题”键的ViewData项是类型   'System.String'但必须是'IEnumerable'类型

1 个答案:

答案 0 :(得分:1)

选项不会在表单中回发。您将不得不再次创建它们:

[HttpPost]
[Authorize]
public ActionResult SendMessage(MessageModel model)
{
    try {
      if (ModelState.IsValid)
      {
          XmlDocument requestXml = XmlUtil.CreateRequestDocument("message", new
          {
              Message = model.MessageBody,
              Type = model.Subject,
              PatientID = model.PatientId,
              RecipientID = model.RecipientId,
              IsUrgent = model.IsUrgent ? "1" : "0"
          });
        //save message logic here
      }
    }
    catch (Exception ex)
    {
        ModelState.AddModelError("ServerMessage", ex.Message);
    }
    var subjects = this.GetDistributionLists();
    if (subjects != null) {
        ViewBag.Subjects = new SelectList(subjects, "Value", "Text");
    }
    if (Request.IsAjaxRequest())
    {
        return PartialView("SendMessagePartial", model);
    }
    else
    {
        return View(model);
    }
}