This is my get
[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();
}
}
This is my post
[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);
}
}
This is my 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; }
}
My View has a dropdownlistfor like
@Html.DropDownListFor(m => m.Subject, (SelectList)ViewBag.Subjects, new { @class = "form-control" })
When I GET, everything is fine. When I POST, the data gets saved but in UI I get an error saying
The ViewData item that has the key 'Subject' is of type 'System.String' but must be of type 'IEnumerable'