不包含的定义,也不包含扩展方法

本文关键字:包含 扩展 方法 定义 | 更新日期: 2023-09-27 18:04:09

我得到错误

'Renter'不包含'RenterName'的定义,也没有扩展方法等

但是我的renter类确实包含'RenterName'。有人知道为什么会这样吗?我已经试图解决这个问题,我删除了部分视图并重写了它,但无济于事,它仍然给了我错误。

我班上

public class LettingAgent
{
    [Key]
    public int Agentid { get; set; }        
    [Required(ErrorMessage = "You Need to Enter A Agent Name ")]
    public string AgentName { get; set; }
    public string Address { get; set; }
    public virtual List<Renter> Renters { get; set; }
}
public class Renter
{
    public int Renterid { get; set; }
    public string RenterName { get; set; }
    [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime DOB { get; set; }
    public int Agentid { get; set; }
    public virtual LettingAgent LettingAgent { get; set; }
}
public class LandLordDb : DbContext
{
    public DbSet<LettingAgent> LettingAgents { get; set; }
    public DbSet<Renter> Renters { get; set; }
    public LandLordDb() : base("LandLordDb") { }
}

你可以看到Renter类有一个字符串RenterName。

部分视图如下

@model IEnumerable<RealEstate.Models.Renter>
@if (Model.Any())
{
    <table id="ActorTable" class="table table-condensed table-striped"
           style="table-layout:fixed; margin-bottom: 0px">
        <tr>
            <th colspan="3">
                @ViewBag.AgentName
            </th>

        </tr>
        @if (Model != null)
        {
            foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.RenterName)
                    </td>
                </tr>
            }
        } @* closing of if *@
   </table>
    }
    else
     {<div><strong><mark>No Child registered for @ViewBag.AgentName</mark>      
     </strong></div>} 

这是我的主页索引页,希望它能帮助别人找到我的代码有什么问题。

 @model IEnumerable<RealEstate.Models.LettingAgent>
     @{
     ViewBag.Title = "Index";
     }
     <h2>Index</h2>
     <p>
     @Html.ActionLink("Create New", "Create")
    </p>
    <div class="container">
        <h1>Click the filter To Search <small>(<i class="glyphicon glyphicon-   filter"></i>)</small></h1>        
            <div class="row">
                <div class="col-md-6">
                    <div class="panel panel-primary">
                        <div class="panel-heading">
                            <h3 class="panel-title">Agents</h3>
                            <div class="pull-right">
                                <span class="clickable filter" data-toggle="tooltip" title="Toggle table filter" data-container="body">
                                    <i class="glyphicon glyphicon-filter"></i>
                                </span>
                            </div>
                        </div>
                        <div class="panel-body">
                            <input type="text" class="form-control" id="dev-table-filter" data-action="filter" data-filters="#dev-table" placeholder="Filter Developers" />
                        </div>
                        <table class="table table-hover" id="dev-table">
                            <thead>
                                <tr>
                                    <th>@Html.DisplayNameFor(model => model.AgentName)</th>
                                    <th>@Html.DisplayNameFor(model => model.Address)</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var item in Model)
                                {
                                    <tr>
                                        <td>
                                            <span class="btn btn-xs btn-warning"
                                                  onclick="showRenter('@item.Agentid')">@Html.DisplayFor(modelItem => item.AgentName)</span>                                            
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.Address)
                                        </td>
                                        <td>
                                            <span class="btn btn-sm btn-warning" onclick="edit('@item.Agentid','@item.AgentName','@item.Address')">Edit</span>
                                            <span class="btn btn-sm btn-warning" onclick="filmDetails(@item.Agentid)">Details</span>
                                            @Html.ActionLink("Delete", "Delete", new { id = item.Agentid }, new { @class = "btn btn-danger btn-sm" })
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                </div>
                <div id="Detail" class="col-md-6">
                </div>
                <div>
                    <form id="CreateActor" hidden="">
                        <div id="time" class="form-group" style="margin-top:10px">     
                            <input type="hidden" name="Agentid">                          
                        </div>
                    </form>
                </div>
      </div>
        </div>
@section scripts
{
<script>
    $(function () {      // ready event
        toastr.success('Welcome To The Real Esate DataBase');
        toastr.options = {
            "progressBar": true,
        }
    });
    function edit(Agentid, AgentName, Address) {
        $.ajax({
            type: "GET",
            url: '@Url.Action("EditAgent")',
            data: { id: Agentid },
            success: function (data) {
                $('#Detail').hide();               
                $('#Detail').html(data);
                $('#Detail').fadeIn("slow")
                $('#Detail').find('input[name="Agentid"]').val(Agentid);
                $('#Detail').find('input[name="AgentName"]').val(AgentName);
                $('#Detail').find('input[name="Address"]').val(Address);            
            },
            error: function (data) {
                $('#Details').html('<h3>Error in retrieval</h3>');
            }
        });
    }
    function showRenter(Agentid) {
        $.ajax({
            type: "GET",
            url: '@Url.Action("ChildrenInCamp")',
            data: { id: Agentid },
            success: function (data) {
                $('#Detail').hide();               
                $('#Detail').html(data);
                $('#Detail').fadeIn("slow");
                $('#CreateActor').find('input[name="Agentid"]').val(Agentid);
                $('#CreateActor').find('input[name="RenterName"]').val("");
                $('#CreateActor').fadeIn("slow");
            },
            error: function (data) {
                $('#Detail').html('<h3>Error in retrieval</h3>');
            }
        });
    }

和My Controller

public class HomeController : Controller
{
    private LandLordDb db = new LandLordDb();
    // GET: LettingAgents
    public ActionResult Index()
    {
        return View(db.LettingAgents.ToList());
    }
    // GET: LettingAgents/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        LettingAgent lettingAgent = db.LettingAgents.Find(id);
        if (lettingAgent == null)
        {
            return HttpNotFound();
        }
        return View(lettingAgent);
    }
    // GET: LettingAgents/Create
    public ActionResult Create()
    {
        return View();
    }
    // POST: LettingAgents/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Agentid,AgentName,Address")] LettingAgent lettingAgent)
    {
        if (ModelState.IsValid)
        {
            db.LettingAgents.Add(lettingAgent);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(lettingAgent);
    }        
    public PartialViewResult EditAgent(LettingAgent id)
    {
        var mov = db.LettingAgents.Find(id);
        // db.SaveChanges();
        return PartialView("_EditAgent", mov);
    }
    // GET: LettingAgents/Delete/5
    public ActionResult Delete(int id)
    {
        return View(db.LettingAgents.Find(id));
    }
    // POST: LettingAgents/Delete/5
    [HttpPost, ActionName("Delete")]        
    public ActionResult DeleteConfirmed(int id)
    {            
        db.LettingAgents.Remove(db.LettingAgents.Find(id));
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    public PartialViewResult ChildrenInCamp(int id)
    {
        var act = db.LettingAgents.Find(id);
        @ViewBag.Agentid = id;
        @ViewBag.AgentName = act.AgentName;
        return PartialView("_ChildrenInCamp", act.Renters);
    }
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}

不包含的定义,也不包含扩展方法

在您的视图中,我看到您绑定到IEnumerable s,但最终试图绑定到它们上的字段,例如:

@model IEnumerable<RealEstate.Models.Renter>

但是绑定看起来像

@Html.DisplayFor(modelItem => item.RenterName) 

要使此绑定真正起作用,您需要做如下操作

@Html.DisplayFor(m => m.ToList()[i].RenterName) 

以便生成正确的标记来连接事物。我在你的另一个观点中也看到了同样的事情,这可能是导致这个问题的原因。

在foreach段中,您可以尝试将DisplayFor替换为Display

foreach (var item in Model)
{
    <tr>
        <td>
            @Html.Display(item.RenterName)
        </td>
    </tr>
}

有关DisplayExtensions的更多信息,可以转到https://msdn.microsoft.com/zh-cn/library/system.web.mvc.html.displayextensions(v=vs.118).aspx .