CS1061:' IEnumerable<>'不包含''的定义并没有延伸方法''接受类型' IEnumerable<>'的第一个参数。

时间:2016-03-26 19:52:39

标签: c# asp.net-mvc linq api

我是.net MVC的新手。请帮我解决我的问题。

我收到此错误: CS1061:' IEnumerable'不包含'姓名'的定义没有推广方法'姓名'接受类型' IEnumerable'的第一个参数。可以找到。

这是我的控制器      public ActionResult Create()         {             return Index();         }

    [HttpPost]
    public ActionResult Create(Person model)
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:23793");

        client.PostAsJsonAsync<Person>("api/person", model)
            .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());

        return RedirectToAction("Index");
    }

在这里我的观点

           @model IEnumerable<PersonDemoMVC.Models.Person>

        @{
            ViewBag.Title = "Create";
        }

        <h2>Create</h2>


        @using (Html.BeginForm()) 
        {
            @Html.AntiForgeryToken()

            <div class="form-horizontal">
                <h4>Person</h4>
                <hr />
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                <div class="form-group">
                    @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })
                    </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>

        <script src="~/Scripts/jquery-1.10.2.min.js"></script>
        <script src="~/Scripts/jquery.validate.min.js"></script>
        <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

3 个答案:

答案 0 :(得分:5)

您的创建视图强烈输入Person实体(IEnumerable<Person>)的集合,并且在您的视图中,您执行的代码如Model.Name model =&gt; model。在Html帮助器方法中命名)。这里Model是IEnumerable<Person>,IEnumerable没有Name属性!。

不要使用集合,而是在创建视图中使用Person的单个实例作为模型。

@model PersonDemoMVC.Models.Person
<h2>Create</h2>
@using(Html.BeginForm())
{
   @Html.TextBoxFor(s=>s.Name)
   <!-- Your existing form elements -->
   <input type="submit" />
}

答案 1 :(得分:0)

您将模型定义为IEnumerable。 稍后您尝试在模型上调用属性Name 行:@ Html.LabelFor(model =&gt; model.Name,htmlAttributes:new {@class =&#34; control-label col-md-2&#34;})

你应该:

  1. 将模型更改为:PersonDemoMVC.Models.Person
    1. 循环遍历模型以接收单个Person对象实例。

答案 2 :(得分:0)

由于您使用的模型是可枚举的,因此请使用foreach或for循环

@model IEnumerable<PersonDemoMVC.Models.Person>

        @{
            ViewBag.Title = "Create";
        }

        <h2>Create</h2>


        @using (Html.BeginForm()) 
        {
            @Html.AntiForgeryToken()

            <div class="form-horizontal">
                <h4>Person</h4>
                <hr />
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
     @foreach(var item in model)
       {
                <div class="form-group">
                    @Html.LabelFor(i => item.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(i => item.Name, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(i => item.Name, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(i => item.Age, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(i => item.Age, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(i => item.Age, "", new { @class = "text-danger" })
                    </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>

        <script src="~/Scripts/jquery-1.10.2.min.js"></script>
        <script src="~/Scripts/jquery.validate.min.js"></script>
        <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
相关问题