C# 无法使用MVC在网格中显示详细信息
其中我有一个文本框和按钮,当用户输入一个值并按下提交按钮时,我需要在同一视图的剑道UI网格中显示这些值 为了这个目的,我这样做了 这是我的模型C# 无法使用MVC在网格中显示详细信息,c#,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,kendo-grid,C#,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,Kendo Grid,其中我有一个文本框和按钮,当用户输入一个值并按下提交按钮时,我需要在同一视图的剑道UI网格中显示这些值 为了这个目的,我这样做了 这是我的模型 namespace KendoSampleMVCApp.Models { public class TextBoxGrid { public string EnteredValue { get; set; } public List<EmployeeDetails> employees;
namespace KendoSampleMVCApp.Models
{
public class TextBoxGrid
{
public string EnteredValue { get; set; }
public List<EmployeeDetails> employees;
}
public class EmployeeDetails
{
public string EmployeeId { get; set; }
public string ManagerId { get; set; }
}
}
答案是否定的,不能在一个视图中使用两个模型。你没有理由要这么做。视图模型的整个点被设计为存储它所对应的视图的所有数据 创建一个
EnterValuesViewModel
类,并使用来自两个业务实体的所有必要数据对其进行实例化。然后键入该类的视图
public class EnterValuesViewModel
{
public string EnteredValue { get; set; }
public List<EmployeeDetailsViewModel> Employees { get; set; }
}
public class EmployeeDetailsViewModel
{
public string EmployeeId { get; set; }
public string ManagerId { get; set; }
}
或者,查看。请查看我的编辑,如果我在我的模型中这样做,我如何在视图中访问这些项目…抱歉,我需要从EmployeeDetailsViewModel中访问ienumerable项目,这就是我提到这一行的原因
ienumerable
我需要更改这一行的任何内容吗我可以在视图中删除这一行ienumerable
您不需要吗需要(并且不能)绑定视图两次。使用Model.Employees
。如果需要,可以创建局部视图,将其强键入到EmployeeDetailsViewModel
。从你的主要观点来看。听起来你需要学习一些教程。有人知道如何访问视图中的教程吗。。
@model KendoSampleMVCApp.Models.TextBoxGrid
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm("PostValues","EnterValuesGrid",FormMethod.Post))
{
<div>
<fieldset>
<legend>Enter Textbox Value</legend>
<div class="editor-field">
@Html.TextBoxFor(m=>m.EnteredValue)
</div>
<p>
<input type="submit" name="Submitbutton1" value="Submit1" />
</p>
</fieldset>
</div>
}
@model IEnumerable<KendoSampleMVCApp.Models.EmployeeDetails> <----- error at this line
<h2>Index</h2>
@using (Html.BeginForm())
{
@(Html.Kendo().Grid<KendoSampleMVCApp.Models.EmployeeDetails>()
.Name("grid")
.Columns(columns => {
columns.Bound(p => p.EmployeeId).Filterable(false).Width(100);
columns.Bound(p => p.ManagerId).Filterable(false).Width(100);
})
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.HtmlAttributes(new { style = "height:430px;" })
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Read(read => read.Action("Orders_Read", "EnterValuesGrid"))
)
)
}
public class ParentViewModel
{
public EmployeeDetails EmployeeDetails { get; set; }
public TextBoxGrid TextBoxGrid { get; set; }
}
public class EnterValuesViewModel
{
public string EnteredValue { get; set; }
public List<EmployeeDetailsViewModel> Employees { get; set; }
}
public class EmployeeDetailsViewModel
{
public string EmployeeId { get; set; }
public string ManagerId { get; set; }
}
@model EnterValuesViewModel
@Html.TextBoxFor(m=>m.EnteredValue)
@foreach(EmployeeDetailsViewModel emp in Model.Employees)
{
@* Stuff about each employee goes here *@
}