不包含定义和扩展方法

时间:2016-08-27 01:31:55

标签: c# asp.net asp.net-mvc razor

我收到错误

  

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

但我的租借者类确实包含'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 Class有一个字符串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>');
            }
        });
    }

和我的控制器

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

2 个答案:

答案 0 :(得分:0)

在您的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

答案 1 :(得分:0)

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

@model IEnumerable<RealEstate.Models.Renter>

但绑定看起来像

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

对于这种真正有效的绑定,你会想做类似

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

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