gpt4 book ai didi

c# - CS1061 : 'IEnumerable<>' does not contain a definition for '' and no extension method '' accepting a first argument of type 'IEnumerable<>'

转载 作者:太空宇宙 更新时间:2023-11-03 19:53:18 25 4
gpt4 key购买 nike

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

我收到这个错误:CS1061:“IEnumerable”不包含“Name”的定义,并且找不到接受“IEnumerable”类型的第一个参数的扩展方法“Name”。

这是我的 Controller 公共(public) Action 结果创建() { 返回索引();

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

最佳答案

您的创建 View 被强类型化为 Person 的集合entity( IEnumerable<Person> ),在您看来,您正在执行类似 Model.Name 的代码(Html 辅助方法中的model=>model.Name)。这里的模型是 IEnumerable<Person> IEnumerable 没有 Name属性(property)!。

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

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

关于c# - CS1061 : 'IEnumerable<>' does not contain a definition for '' and no extension method '' accepting a first argument of type 'IEnumerable<>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36240150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com