简体   繁体   English

不包含定义和扩展方法

[英]Does not contain a definition for and no extension method

I get the error 我收到了错误

'Renter' does not contain a definition for 'RenterName' and no extension method etc 'Renter'不包含'RenterName'的定义,也没有扩展方法等

But my renter class does contain 'RenterName'. 但我的租用者类确实包含'RenterName'。 Does anyone know why this is happening? 有谁知道为什么会这样? i Have tried to resolve this my deleting the partial view and rewriting it but to off no avail it still gives me the error. 我试图解决这个问题,我删除部分视图并重写它但是无法使用它仍然给我错误。

My Class 我的课

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") { }

}

As you can see the Renter Class has a string RenterName. 如您所见,Renter Class有一个字符串RenterName。

The Partial view is the following 部分视图如下

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

Here is my Home Index Page hopefully it will help someone find whats wrong with my code. 这是我的主页索引页面,希望它能帮助别人找到我的代码有什么问题。

 @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>');
            }
        });
    }

And 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);
    }
}

In your foreach segment, you can try replace the DisplayFor with Display 在foreach细分中,您可以尝试将DisplayFor替换为Display

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

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

In your views I see that you are binding to IEnumberables but end up trying to bind to a field on them like for example : 在您的视图中,我看到您绑定到IEnumberables但最终尝试绑定到它们上的字段,例如:

@model IEnumerable<RealEstate.Models.Renter>

but the binding looks like 但绑定看起来像

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

for this binding to truly work you would want to do something like 对于这种绑定到真正的工作,你会想要做的事情

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

so that it generates the correctly mark up to wire up things. 这样它就可以生成正确的标记来连接东西。 I see the same thing in your other view as well, that might be the cause of this issue. 我在你的另一个观点中也看到了同样的事情,这可能是导致这个问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM