C# – CS1061: ‘IEnumerable<>‘ does not contain a definition for ” and no extension method ” accepting a first argument of type ‘IEnumerable<>‘

apiasp.net-mvcclinq

I'm new to .net MVC Please help me to solve my problem.

I'm getting this error:
CS1061: 'IEnumerable' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'IEnumerable' could be found.

here is my controller
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");
    }

And here my view

           @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>

Best Answer

Your Create view is strongly typed to a collection of Person entity(IEnumerable<Person>) and in your view you are executing code like Model.Name (model=>model.Name in Html helper methods). Here Model is a IEnumerable<Person> and IEnumerable does not have a Name property !.

Instead of using a collection, Use a single instance of Person as your model in your create view.

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