简体   繁体   中英

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

But my renter class does contain '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.

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 (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 .

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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