I'm trying to get my own viewmodel after submit my form and I got the following error: "Unable to cast object of type 'System.String[]' to type 'System.String'"
This is my code:
ViewMode:
public class SoftwarePackages
{
public string[] PermissionsList { get; set; }
}
aspx
<%: Html.ListBoxFor(model => model.PermissionsList, new List<SelectListItem>(), new { size = 10 })%>
controller
[HttpPost]
public ActionResult Index(SoftwarePackages softwarePackages)
{
code...code...code
}
what is the problem?
Thank you
zurdoI开发者_开发问答L
The following works fine for me:
Model:
public class SoftwarePackages
{
public string[] PermissionsList { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new SoftwarePackages
{
Items = new SelectList(new[]
{
new SelectListItem { Value = "1", Text = "item 1" },
new SelectListItem { Value = "2", Text = "item 2" },
new SelectListItem { Value = "3", Text = "item 3" },
}, "Value", "Text")
};
return View(model);
}
[HttpPost]
public ActionResult Index(SoftwarePackages softwarePackages)
{
// do something with softwarePackages.PermissionsList
...
}
}
View:
<% using (Html.BeginForm()) { %>
<%: Html.ListBoxFor(model => model.PermissionsList, Model.Items, new { size = 10 })%>
<input type="submit" value="OK" />
<% } %>
I found the problem:
In my viewmodel I had the following attributes:
[Required(ErrorMessage = "Please provide a Description!")]
[StringLength(255, ErrorMessage = "Description must be less than 255 characters!")]
[DisplayName("Description")]
and of course the "StringLength" attribute was which was try to cast this field to string.
Thank you for your help!
zurdoIL
As an aside you will not be able to get your model back with the code you've shown. The the ListBoxFor
(and DropDownListFor
) helper methods use that IEnumerable<SelectListItem>
to actually render their contents, and you are passing an empty collection of SelectListItems
into the method. You can use these extension methods to create an IEnumerable<SelectListItem>
from any IEnumerable<string>
:
public static class IEnumerableStringExtensions
{
public static IEnumerable<SelectListItem> ToSelectListItemList(this IEnumerable<string> items)
{
return IEnumerableStringExtensions.ToSelectListItemList(items, new List<string>());
}
public static IEnumerable<SelectListItem> ToSelectListItemList(this IEnumerable<string> items, string selectedValue)
{
return IEnumerableStringExtensions.ToSelectListItemList(items, new List<string>());
}
public static IEnumerable<SelectListItem> ToSelectListItemList(this IEnumerable<string> items, IEnumerable<string> selectedValues)
{
List<SelectListItem> listitems = new List<SelectListItem>(items.Count());
foreach (string s in items)
{
listitems.Add(new SelectListItem()
{
Text = s,
Value = s,
Selected = selectedValues.Contains(s)
});
}
return listitems;
}
}
Use the methods like this:
<%:Html.ListBoxFor(m => m.PermissionList, Model.PermissionList.ToSelectListItemList(), new { size = 10 })%>
Using a sample project I was not able to duplicate your exception from the code you have provided (PermissionList
was just null), but at least the class above will get your model back to the server (more accurately the values that the user selected from the list).
精彩评论