传递到字典中的模型项的类型为System.Collections.Generic.List

时间:2015-10-28 17:06:35

标签: c# asp.net-mvc

我是asp.net的新手,这是我的第一个应用程序。我正在开发一个管理保险请求的应用程序。模型Request包含文件上载。 addRequest添加一个请求并要求一个成员登录。每次我尝试运行addRequest时都会收到错误:

  

传递到字典中的模型项是类型的   'System.Collections.Generic.List`1 [Mutuelle.Domain.Entities.Request]',   但是这个字典需要一个类型的模型项   'Mutuelle.Domain.Entities.Request'。

此外,当我在代码中的任何位置放置一个断点并逐步运行它实际上有效,它不会停在任何一行。

这是控制器:

public ActionResult AddRequest()
        {
            Request request = new Request();
            return View(request);

        }


[HttpPost]
public ActionResult AddRequest([Bind(Exclude = "file")]Request request, HttpPostedFileBase file)
{
    try
    {
        if (!ModelState.IsValid)
        {
            return View(request);
        }
        else
        {
            BinaryReader fileReader = new BinaryReader(file.InputStream);
            byte[] fileb = fileReader.ReadBytes(file.ContentLength);

            request.file = fileb;

            RequestService requestMemberService = new RequestService();
            Member member = (Member)Session["logedAd"];
            request.nomMember = member.nom;
            request.eMailMember = member.eMail;
            request.member = member;
            member.listRequests.Add(request);
            requesMemberService.UpdateMember(member);
            request.member = member;
            requestMemberService.CreateRequest(request);

            Session.Remove("logedAd");
            Session.Add("logedAd", member);
            return RedirectToAction("Requests");

        }
    }

    catch
    {
        return RedirectToAction("Index", "Home");
    }

}

以下是观点:

@model Mutuelle.Domain.Entities.Request

@{
    ViewBag.Title = "AddRequest";
}

<h2>AddRequest</h2>

@using (Html.BeginForm("Addrequest", "Member", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Demande</h4>
        <hr />
        @Html.ValidationSummary(true)


        <div class="form-group">
            @Html.LabelFor(model => model.Nomrubrique, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Nomrubrique)
                @Html.ValidationMessageFor(model => model.Nomrubrique)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.file, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input style="margin-left:0px;cursor:pointer;" type="file" name="file" id="file" />
            </div>
        </div>






        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

0 个答案:

没有答案