Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have simple grid on my MVC page, When I click to update The Record by clicking on the Edit Button it shows me Error

"The ViewData item that has the key ContactID is of type System.Int32 but must be of type IEnumerable<SelectListItem>"

Where I am wrong I have following Code of My controller
SQL
public ActionResult Edit(int id)
        {
            Donation model = _dService.GetDonation(id, applicationID);
            GetDonationType();
            GetContactList();
            GetProjectType(model.ProjectId);
            GetCategory();
            if (model != null)
                return View(model);
            else return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, Donation model)
        {

            try
            {
                _dService.SaveDonation(model, applicationID);
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddRuleViolations(model.GetRuleViolations());
                return View(model);
            }
        }

and Following is the code section of View I am Getting Stuck
SQL
@model CMg.Data.Donation
 
        <fieldset>            
            <table class="data-table">
            <tr>
            <td>
                <label for="ContactID">Donner Name:</label>
            </td>
            <td>
                @Html.DropDownList("ContactID", (SelectList)(ViewContext.TempData["Contacts"] ?? ViewData["Contacts"]))
                @Html.ValidationMessage("ContactID", "Field Required")
                @Html.Hidden("ApplicationID",Model.ApplicationID ?? (System.Guid)Helpers.Utility.GetApplicationID)
            </td>

Some Suggestions Please

Thank You
Posted
Comments
Karthik. A 19-Jun-12 15:58pm    
I don't see anything in your controller that assigns a List<SelectListItem> to ViewData["Contacts"] or TempData["Contacts"]. Probably the data you store in either of these 2 is wrong. And I guess @Html.DropDownList("ContactID", (SelectList)(ViewContext.TempData["Contacts"] ?? ViewData["Contacts"])) should read as @Html.DropDownList("ContactID", (List<SelectListItem>)(ViewContext.TempData["Contacts"] ?? ViewData["Contacts"])). Can you post your full code from the controller?
M Ali Qadir 20-Jun-12 5:34am    
I have put if(ModelState.IsValid) And in else { return RedirectToAction("Edit")}
and I have change the GridEditMode.Inline to GridEditMode.Inform and It is working.

You are wrong at binding the data to drop down.
at this place.
@Html.DropDownList("ContactID", (SelectList)(ViewContext.TempData["Contacts"] ?? ViewData["Contacts"]))
try
my blog post where i bind the drop down with a list as well as empty.
or
try look at solution for this question in stack overflow.
http://stackoverflow.com/questions/11226780/binding-data-to-dropdownlist-mvc-razor[^]
 
Share this answer
 
I had the same issue. What was happening in my case is that the model contained SelectList properties. But when the model was submitted from the view to the Edit Post method, these properties were not populated. So when an error occurred in the Edit Post method's try/catch block, the model was re-sent to the View but without the properties needed to populate, in my case, the DropDownListFor's. I suspect something similar is happening with your ViewData["Contacts"]

Put a if(ModelState.IsValid) if block around your update code and then check the intellisense by hovering over ModelState while you step through the code. I expect you will find the "Contacts" area of your model is null. The other thing you will need to find is the error that is causing the method to return View(model).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900