C# 在同一视图中创建和创建表
我正在尝试连接两个表,并从这些连接的结果中显示一个表,以及在同一视图中显示一个create表单 我只是跟着答案走 下面是我的模型类C# 在同一视图中创建和创建表,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我正在尝试连接两个表,并从这些连接的结果中显示一个表,以及在同一视图中显示一个create表单 我只是跟着答案走 下面是我的模型类 public class CategoryViewModel { public AB_ProductTypeCategory categoryCreate { get; set; } public IEnumerable<AB_ProductTypeCategory> categoryList { get; set; } } publi
public class CategoryViewModel
{
public AB_ProductTypeCategory categoryCreate { get; set; }
public IEnumerable<AB_ProductTypeCategory> categoryList { get; set; }
}
public partial class AB_ProductTypeCategory
{
public string ProductCategoryID { get; set; }
public string ProductTypeID { get; set; }
public string ProductCategoryNameEn { get; set; }
}
这是创建控制器的方法
[HttpGet]
public ActionResult ProductCategory_Create()
{
Product_Type_DropDownListEn();
Product_Type_DropDownListAr();
return View();
}
[HttpPost]
public ActionResult ProductCategory_Create(AB_ProductTypeCategory product_category)
{
Product_Type_DropDownListEn(product_category.ProductTypeID);
Product_Type_DropDownListAr(product_category.ProductTypeID);
if (ModelState.IsValid)
{
..
}
return View(product_category);
}
这是列表视图页面“ProductCategory\u列表”
@model IEnumerable
@DisplayNameFor(model=>model.ProductCategoryID)
...
这是我试图显示列表视图和创建视图的页面
@model project_name.Models.AB_ProductTypeCategory
@{
}
@{Html.RenderAction("ProductCategory_List", "Home");}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Create New Product Category</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ProductCategoryID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProductCategoryID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProductCategoryID, "", new { @class = "text-danger" })
</div>
</div>
.....
@model project\u name.Models.AB\u ProductTypeCategory
@{
}
@{Html.RenderAction(“ProductCategory_List”,“Home”);}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
创建新的产品类别
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.ProductCategoryID,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.ProductCategoryID,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.ProductCategoryID,“,新的{@class=“text danger”})
.....
如果要同时显示这两个视图,则需要将视图设置为CategoryViewModel
,并且需要将CategoryViewModel
的实例返回到视图中(或者需要使用@Html.Action()
调用单独的控制器方法来呈现部分视图。我不明白你说的话,我能举个小例子吗?完全按照你链接的问题(不是答案!)来做,但将模型的一个实例传递给视图-在这个问题上,它应该是var model=new EventoViewModel{LEventos=db.Eventos.ToList()};返回视图(模型);
你是说像这样的东西吗公共视图结果列表(){var model=new EventoViewModel{LEventos=db.Eventos.ToList()};返回视图(模型);}
@StephenMuecke您是说不为列表创建新的查看页面,只需将两个代码嵌入同一个查看页面?
@model IEnumerable<project_name.Models.AB_ProductTypeCategory>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ProductCategoryID)
</th>
...
@model project_name.Models.AB_ProductTypeCategory
@{
}
@{Html.RenderAction("ProductCategory_List", "Home");}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Create New Product Category</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ProductCategoryID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProductCategoryID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProductCategoryID, "", new { @class = "text-danger" })
</div>
</div>
.....