具有密钥ContactID的ViewData项的类型为System.Int32,但必须是IEnumerable类型 [英] The ViewData item that has the key ContactID is of type System.Int32 but must be of type IEnumerable

查看:123
本文介绍了具有密钥ContactID的ViewData项的类型为System.Int32,但必须是IEnumerable类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的MVC页面上有简单的网格,单击编辑按钮点击更新记录时,它显示错误



具有密钥ContactID的ViewData项目的类型为System.Int32,但必须是IEnumerable类型< SelectListItem>



如果我错了,我有以下代码我的控制器

  public  ActionResult Edit(< span class =code-keyword> int  id)
{
捐赠模型= _dService.GetDonation(id,applicationID);
GetDonationType();
GetContactList();
GetProjectType(model.ProjectId);
GetCategory();
if (model!= null
返回 查看(模型);
其他 返回 查看( );
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit( int id,捐赠模式)
{

尝试
{
_dService.SaveDonation(model,applicationID);
return RedirectToAction( Index);
}
catch
{
ModelState.AddRuleViolations(model.GetRuleViolations());
return 查看(模型);
}
}



和以下是View I am Stuck的代码部分

  @ model  CMg.Data.Donation 

< fieldset>
< table class = data-table>
< tr>
< td>
< label for = ContactID> Donner名称:< / label>
< / td>
< td>
@ Html .DropDownList( ContactID ,(SelectList)(ViewContext.TempData [ Contacts] ?? ViewData [< span class =code-string> Contacts]))
@ Html .ValidationMessage( ContactID 字段必需
@ Html .Hidden( ApplicationID,Model.ApplicationID ??(System.Guid)Helpers.Utility.GetApplicationID)
< / td> ;



一些建议请



谢谢

解决方案

你错了绑定数据下拉。

在这个地方。

@ Html.DropDownList(ContactID,(SelectList)(ViewContext.TempData [Contacts] ?? ViewData [Contacts]))

尝试

我的博客文章,其中我将下拉列表与列表绑定以及为空。



尝试在堆栈溢出中查看此问题的解决方案。

http://stackoverflow.com/questions/11226780/binding-data-to-dropdownlist-mvc-razor [ ^ ]


< blockquote>我有同样的问题。在我的情况下发生的是模型包含SelectList属性。但是,当模型从视图提交到Edit Post方法时,不会填充这些属性。因此,当Edit Post方法的try / catch块发生错误时,模型被重新发送到View,但没有填充所需的属性,在我的例子中是DropDownListFor。我怀疑你的ViewData发生了类似的事情[联系人]



如果阻止你的更新代码放一个if(ModelState.IsValid),然后检查intellisense单步执行代码时将鼠标悬停在ModelState上。我希望您会发现模型的联系人区域为空。您需要找到的另一件事是导致该方法返回View(模型)的错误。


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

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

@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

解决方案

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[^]


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).


这篇关于具有密钥ContactID的ViewData项的类型为System.Int32,但必须是IEnumerable类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆