Asp.net 在索引页中显示局部视图

Asp.net 在索引页中显示局部视图,asp.net,asp.net-mvc,asp.net-mvc-4,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我正在制作我的第一个.NETMVC4应用程序,只是为了尝试一下。我已经做了一个应用程序,它连接到MongoDB,您可以在那里存储检索的汽车数据。目前这是两种不同的观点。但我希望它们变成两个不同的局部视图,我可以在一个视图中显示。为此,我重新开始了我的申请 我做了一个汽车控制器: namespace MvcApplication1.Controllers { public class CarsController : Controller { public Acti

我正在制作我的第一个.NETMVC4应用程序,只是为了尝试一下。我已经做了一个应用程序,它连接到MongoDB,您可以在那里存储检索的汽车数据。目前这是两种不同的观点。但我希望它们变成两个不同的局部视图,我可以在一个视图中显示。为此,我重新开始了我的申请

我做了一个汽车控制器:

namespace MvcApplication1.Controllers
{
    public class CarsController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Create()
        {
            return PartialView();
        }
    }
}
我做了一个汽车模型

namespace MvcApplication1.Models
{
    public class InsertCarViewModel
    {
        public string Make { get; set; }
        public int NumberOfDoors { get; set; }
        public decimal DailyRentalFee { get; set; }
        public string DelimitedListOfCountries { get; set; }
    }
}
我已经为Indexview创建了Cars文件夹,并为部分视图(_Create.cshtml)创建了共享文件夹

我的索引视图:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @{Html.RenderPartial("_Create", Model.InsertCarViewModel)}
    </div>
</body>
</html>
@{
布局=空;
}
指数
@{Html.RenderPartial(“_Create”,Model.InsertCarViewModel)}
我的局部视图(_Create)

@model mvcapapplication1.Models.InsertCarViewModel
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
InsertCarViewModel
@LabelFor(model=>model.Make)
@EditorFor(model=>model.Make)
@Html.ValidationMessageFor(model=>model.Make)
@LabelFor(model=>model.numberofoors)
@EditorFor(model=>model.numberofoors)
@Html.ValidationMessageFor(model=>model.numberofoors)
@LabelFor(model=>model.dailrentalfee)
@EditorFor(model=>model.dailrentalfee)
@Html.ValidationMessageFor(model=>model.dailrentalfee)
@LabelFor(model=>model.DelimitedListOfCountries)
@EditorFor(model=>model.DelimitedListOfCountries)
@Html.ValidationMessageFor(model=>model.DelimitedListOfCountries)

}
我看不到我的局部视图,因此出错。
我错过了什么/做错了什么?我的索引视图需要模型吗

您可以只使用
Html.Partial
,无需在此处渲染Partial

<div>
    @Html.Partial("_Create", Model.InsertCarViewModel)
</div>

@Html.Partial(“_Create”,Model.InsertCarViewModel)
请这样做

注意:您必须提及您使用索引页的模型名称空间。

@model MvcApplication1.Models.InsertCarViewModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @{Html.RenderPartial("_Create", Model)}
    </div>
</body>
</html>
@model List<MvcApplication1.Models.InsertBikeViewModel>
@{
    Layout = null;
}

 @foreach ( var bikeItem in Model)
 {
    <div>  @bikeItem.Name </div>
    <div>  @bikeItem.Id </div>
 }


</body>
</html>
@model MvcApplication1.Models.InsertCicyleViewModel
@{
    Layout = null;
}

<div>  @cicyleItem .cicyleName </div>
<div>  @cicyleItem .cicyleId </div>
@model mvcapapplication1.Models.InsertCarViewModel
@{
布局=空;
}
指数
@{Html.RenderPartial(“_Create”,Model)}
并且您的局部视图都必须包含您要传递的相同名称空间

例如:

 @model MvcApplication1.Models.InsertCarViewModel
@{
    layout = null;
 }
<p> partial view</p>
@model mvcapapplication1.Models.InsertCarViewModel
@{
布局=空;
}
局部视图


您的主视图没有定义模型,您可以使用,因为在创建操作时,它更适合这里:

<div>
    @{  Html.RenderAction("Create", "Cars"); } // first parameter action name,
                                        //   second controller name
</div>

请创建如下类:我以您的为例

namespace MvcApplication1.Models
{
    public class InsertCarViewModel
    {
        public string Make { get; set; }
        public int NumberOfDoors { get; set; }
        public decimal DailyRentalFee { get; set; }
        public string DelimitedListOfCountries { get; set; }

        public List<InsertBikeViewModel> Bike { get; set; }
        public InsertCicyleViewModel Cicyle { get; set; }
    }

    public class InsertBikeViewModel
    {
        public string Name { get; set; }
        public int Id { get; set; }
    }

    public class InsertCicyleViewModel
    {
        public string cicyleName { get; set; }
        public int cicyleId { get; set; }
    }
}
namespace mvcapapplication1.Models
{
公共类InsertCarViewModel
{
公共字符串Make{get;set;}
公共int NumberOfDoors{get;set;}
公共十进制DailyRentalFee{get;set;}
公共字符串分隔符DlistofCountries{get;set;}
公共列表{get;set;}
public inserticyleviewmodel Cicyle{get;set;}
}
公共类InsertBikeViewModel
{
公共字符串名称{get;set;}
公共int Id{get;set;}
}
公共类InsertCicyleViewModel
{
公共字符串cicyleName{get;set;}
public int cicyleId{get;set;}
}
}
您的主要索引必须如下所示

@model MvcApplication1.Models.InsertCarViewModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @{
        Html.RenderPartial("_CreateBike", Model.Bike)
        }
    </div>
    <div>
        @{
        Html.RenderPartial("_CreateCicyle", Model.Cicyle)
        }
    </div>
</body>
</html>
@model mvcapapplication1.Models.InsertCarViewModel
@{
布局=空;
}
指数
@{
Html.RenderPartial(“\u CreateBike”,Model.Bike)
}
@{
RenderPartial(“_createcityle”,Model.cityle)
}
在下面的代码中,我将在局部视图中显示自行车列表

注意:我将下面的部分视图命名为“\u CreateBike”,它应该与我在索引视图中定义的部分视图相匹配。

@model MvcApplication1.Models.InsertCarViewModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @{Html.RenderPartial("_Create", Model)}
    </div>
</body>
</html>
@model List<MvcApplication1.Models.InsertBikeViewModel>
@{
    Layout = null;
}

 @foreach ( var bikeItem in Model)
 {
    <div>  @bikeItem.Name </div>
    <div>  @bikeItem.Id </div>
 }


</body>
</html>
@model MvcApplication1.Models.InsertCicyleViewModel
@{
    Layout = null;
}

<div>  @cicyleItem .cicyleName </div>
<div>  @cicyleItem .cicyleId </div>
@型号列表
@{
布局=空;
}
@foreach(模型中的var bikeItem)
{
@比基特姆,名字
@比基特姆
}
还有你对Cicyle类的另一部分看法

注意:我在部分视图下的名称为“\u createcityle”,该视图应与我在索引视图中定义的视图匹配。

@model MvcApplication1.Models.InsertCarViewModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @{Html.RenderPartial("_Create", Model)}
    </div>
</body>
</html>
@model List<MvcApplication1.Models.InsertBikeViewModel>
@{
    Layout = null;
}

 @foreach ( var bikeItem in Model)
 {
    <div>  @bikeItem.Name </div>
    <div>  @bikeItem.Id </div>
 }


</body>
</html>
@model MvcApplication1.Models.InsertCicyleViewModel
@{
    Layout = null;
}

<div>  @cicyleItem .cicyleName </div>
<div>  @cicyleItem .cicyleId </div>
@model mvcapapplication1.Models.inserticyleviewmodel
@{
布局=空;
}
@cicyleItem.cicyleName
@cicyleItem.cicyleId

发布您收到的错误?请发布您的局部视图代码和错误…还应使用分号
Html.RenderPartial(“_Create”,Model.InsertCarViewModel)
的末尾,类似这样的
{Html.RenderPartial(_Create,Model.InsertCarViewModel);}
您的索引视图还必须需要Partialview中的标题“@Model mvcapapplication1.Models.InsertCarViewModel”。请仔细阅读我的代码。索引视图的模式是什么?请尝试一下。您必须在视图中提及模式。请让我知道它是否对您有帮助。是的,这很有效,谢谢!现在,如果我想在同一索引视图中添加另一个局部视图,我可以在索引页面中添加另一个@model吗?为什么在索引中定义model,Op没有在索引中使用model,
RenderAction()
非常适合这种情况不,您不能在单个页面中添加多个@model。你可以使用汽车类中的任何其他类来代替它。例子:!st以bike的名称创建一个类似bikeClass的类,然后在carclass中为bikeClass创建一个对象。然后在你的索引视图中,你可以像@Model.bike那样调用bikeClass(你可以使用它来传递到另一个局部视图)。这对我不起作用,可能是因为我不知道什么是“关心”?如果要将多个局部视图添加到一个索引中,这是更好的解决方案吗?@bbvanee第一个参数是操作名称,第二个参数是控制器名称,这是我输入的错别字,而不是我写的CaresThank