locked
Message = "The parameter conversion from type 'System.String' to type 'System.Web.Mvc.SelectListItem' failed because no type converter can convert between these types." RRS feed

  • Question

  • User1548699010 posted

    I have a domain modle as follows (in EF entities):

    Public Class Photo

    {

         public int PhotoId {get; set;}

         public string PhotoTitle {get; set;}

         public string PhotoDesc{get; set;}

         ....

         public int PhotoCategoryId {get; set;}

    }

    and

    public class AlbumPhoto

    {

         public int AlbumPhotoId {get; set;}

         public int PhotoId {get; set;}

         public int AlbumId {get; set;}

    }

    These two entities have primary key-foregin key relationship.

    My view mode:

    public class AddPhotoViewModel
    {

    [Required(ErrorMessage = "Photo title is required!")]
    [StringLength(25, ErrorMessage = "Title may not be longer than 25 characters")]
    public string photoTitle
    {
    get;
    set;
    }

    [StringLength(150, ErrorMessage = "Title may not be longer than 150 characters")]
    public string photoDescription
    {
    get;
    set;
    }

    [StringLength(50, ErrorMessage = "Caption may not be longer than 50 characters")]
    public string photoCaption
    {
    get;
    set;
    }

    [Required(ErrorMessage = "Category ID is required!")]
    public int photoCategoryId
    {
    get;
    set;
    }

    public IEnumerable<SelectListItem> photoCategories
    {
    get;
    set;
    }
    }
    My repository has an extension class to retrieve categories from database:
    IEnumerable<SelectListItem> ToPhotoCategorySelecteListItems(this IEnumerable<PhotoCategory> categories, int id)
    
    
    My Controller:
    GET:
    public ActionResult AddPhotoToAlbum()
    {
    var addPhotoViewModel = new AddPhotoViewModel();

    var categories = galleryRepository.FindAllCategories();

    addPhotoViewModel.photoCategories = categories.ToPhotoCategorySelecteListItems(-1);

    return View("AddPhotoToAlbum", addPhotoViewModel);
    }
    POST:
    [HttpPost]
    public ActionResult AddPhotoToAlbum(AddPhotoViewModel addPhotoViewModel)
    {


    var errors = ModelState
    .Where(x => x.Value.Errors.Count > 0)
    .Select(x => new { x.Key, x.Value.Errors })
    .ToArray();



    if (ModelState.IsValid)
    {
                    ..............
                }
                else
                {b
                }
    }
    
    
    My problem is when I posted the form, I got modelstate error: 
    Message = "The parameter conversion from type 'System.String' to type 'System.Web.Mvc.SelectListItem' failed because no type converter can convert between these types."
    I do not knw what really the problem is. The form was displayed properly with all categories loaded to the dropdown list.
    I googled it for a while, but still no luck. But if I tried not selected any category in the dropdown list, modelstate returned TRUE, with any items selected, it returend FALSE.
    Please help.
    
    
    Bob
    Tuesday, August 30, 2011 5:52 PM

Answers

  • User-474980206 posted

    this code is bad:

            <div class="editor-field">
    @Html.DropDownListFor(model => model.photoCategories, Model.photoCategories, string.Empty)
    @Html.ValidationMessageFor(model => model.photoCaption)
    </div>

    the downdown list should be bound to a string or int property, not a collection, probably your photoCategoryID. the second parameter is the collection that list the values.  

            <div class="editor-field">
    @Html.DropDownListFor(model => model.photoCategoryID, Model.photoCategories, string.Empty)
    @Html.ValidationMessageFor(model => model.photoCategoryID)
    </div>
    note: the Value strings in PhotoCategories must be valid ints
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 31, 2011 5:52 PM

All replies

  • User197322208 posted

    My problem is when I posted the form, I got modelstate error: 
    Message = "The parameter conversion from type 'System.String' to type 'System.Web.Mvc.SelectListItem' failed because no type converter can convert between these types."

    Please remove get/set from

     public IEnumerable<SelectListItem> photoCategories

    What's your view and what's the name of the dropdown?
    Tuesday, August 30, 2011 6:24 PM
  • User-474980206 posted

    in your view you have rendered a input with phtotCategories as the name. please show te dropdown code from the view.

    Tuesday, August 30, 2011 6:35 PM
  • User-1942416292 posted

    If you want to make an DropDownList. Try this way.

    private void PopulateCategories(object selected = null)

    {

    var categories = galleryRepository.FindAllCategories();
    ViewBag.PhotoCategoryId = new SelectList(categories,"CategoryId","CategoryName",selected);

    }

    then your View will be

    @Html.DropDownListFor(m => m.PhotoCategoryId,null)

    Hope this help !

    Tuesday, August 30, 2011 8:05 PM
  • User1548699010 posted

    Here is the view:

    @using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
    <legend>AddPhotoViewModel</legend>

    <div class="editor-label">
    @Html.LabelFor(model => model.photoTitle)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.photoTitle)
    @Html.ValidationMessageFor(model => model.photoTitle)
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.photoDescription)
    </div>
    <div class="editor-field">
    @Html.TextAreaFor(model => model.photoDescription)
    @Html.ValidationMessageFor(model => model.photoDescription)
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.photoCaption)
    </div>
    <div class="editor-field">
    @Html.TextAreaFor(model => model.photoCaption)
    @Html.ValidationMessageFor(model => model.photoCaption)
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.photoCategories)
    </div>
    <div class="editor-field">
    @Html.DropDownListFor(model => model.photoCategories, Model.photoCategories, string.Empty)
    @Html.ValidationMessageFor(model => model.photoCaption)
    </div>

    <div>
    @Html.Partial("/Views/Shared/_FileUpload.cshtml")
    </div>
    <p>
    <input type="submit" value="Create" />
    </p>
    </fieldset>
    }
    
    
    Wednesday, August 31, 2011 12:24 PM
  • User1548699010 posted

    With this view, the dropdown list was populated properly.

    Wednesday, August 31, 2011 12:27 PM
  • User1548699010 posted

    I need to have a form to let user add photos to the Album, but not all of the fields need user input, so I just want to display required fields in the form, and also, user needs to select category, which is in another table(entity) but with a association in the table(entity) of Photo. I use viewmodel to put these fields together, and try to use a dropdown list to display all categories for selection. When posting the form, I need to have the categoryId insttead of the selected category name. I am very confusing how to do that, just googled for a while and came with this code. I really need help on this. I think this might be something to do with model binding.

    Wednesday, August 31, 2011 1:00 PM
  • User1548699010 posted

    As I googled and learn a bit more, I think that was the problem of model binding, do I need to write a custom model binder to solve this type of  problem?

    Wednesday, August 31, 2011 1:35 PM
  • User-474980206 posted

    this code is bad:

            <div class="editor-field">
    @Html.DropDownListFor(model => model.photoCategories, Model.photoCategories, string.Empty)
    @Html.ValidationMessageFor(model => model.photoCaption)
    </div>

    the downdown list should be bound to a string or int property, not a collection, probably your photoCategoryID. the second parameter is the collection that list the values.  

            <div class="editor-field">
    @Html.DropDownListFor(model => model.photoCategoryID, Model.photoCategories, string.Empty)
    @Html.ValidationMessageFor(model => model.photoCategoryID)
    </div>
    note: the Value strings in PhotoCategories must be valid ints
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 31, 2011 5:52 PM
  • User1548699010 posted

    Thanks. I also just figured it out that. ModelState.IsValid returns TRUE now.

    Wednesday, August 31, 2011 6:13 PM