Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将网格绑定到视图中的IEnumerable';视图模型_C#_Asp.net Mvc_Entity Framework_Kendo Ui - Fatal编程技术网

C# 将网格绑定到视图中的IEnumerable';视图模型

C# 将网格绑定到视图中的IEnumerable';视图模型,c#,asp.net-mvc,entity-framework,kendo-ui,C#,Asp.net Mvc,Entity Framework,Kendo Ui,我正在为应用程序构建一个用户配置文件页面。该页面允许用户查看和编辑基本数据字段(姓、名等)。此外,还有一个与他们链接的用户网格。用户应该能够在此网格上执行一些基本的CRUD操作。如何将此网格绑定到IEnumerable(或comparable),该IEnumerable(或comparable)作为viewmodel的属性传递给视图 视图模型如下所示: using System.Collections.Generic; namespace Pricing.Models.ViewModel {

我正在为应用程序构建一个用户配置文件页面。该页面允许用户查看和编辑基本数据字段(姓、名等)。此外,还有一个与他们链接的用户网格。用户应该能够在此网格上执行一些基本的CRUD操作。如何将此网格绑定到IEnumerable(或comparable),该IEnumerable(或comparable)作为viewmodel的属性传递给视图

视图模型如下所示:

using System.Collections.Generic;

namespace Pricing.Models.ViewModel
{
    public class UserProfileUpdateViewModel
    {
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public IEnumerable<LinkedUserViewModel> LinkedUsers { get; set; }
    }
}
使用System.Collections.Generic;
命名空间Pricing.Models.ViewModel
{
公共类UserProfileUpdateViewModel
{
public int UserId{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共IEnumerable链接器{get;set;}
}
}
“我的用户配置文件”视图当前看起来如下所示。请注意第38行的TODO注释,它标记了当前已损坏的内容

@model Pricing.Models.ViewModel.UserProfileUpdateViewModel
<div class="panel-body">
    <h2>User Profile</h2>
    @using (Html.BeginForm("UpdateProfile", "Account", FormMethod.Post))
    {
        @Html.AntiForgeryToken()
        @Html.HiddenFor(profile => profile.UserId)
        <div class="form-horizontal">
            <div class="form-group">
                <label class="control-label col-md-2">Username</label>
                <div class="col-md-10" style="padding-top:7px">
                    @User.Identity.Name
                </div>
            </div>
        </div>
        <div class="form-horizontal">
            <div class="form-group">
                @Html.LabelFor(profile => profile.FirstName, "First Name", new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(profile => profile.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(profile => profile.FirstName, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
        <div class="form-horizontal">
            <div class="form-group">
                @Html.LabelFor(profile => profile.LastName, "Last Name", new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(profile => profile.LastName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(profile => profile.LastName, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
        <div class="form-horizontal">
            <div class="form-group">
                @Html.LabelFor(profile => profile.LinkedUsers, "Linked Users", new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    //TODO: This bind fails, I need to bind to the IENumerable property of UserProfileUpdateViewModel
                    @(Html.Kendo().Grid<Freds.Retail.DirectPricing.Models.ViewModel.UserProfileUpdateViewModel>()
                        .Name("LinkedUsers")
                        .ToolBar(tools =>
                        {
                            tools.Create().Text("Add Linked User");
                        })
                        .Editable(editable => editable.Mode(GridEditMode.InCell).CreateAt(GridInsertRowPosition.Bottom).DisplayDeleteConfirmation(false))
                        .Columns(columns =>
                        {
                            columns.Bound(p => p.LinkedUsersId).Visible(false).ClientTemplate("#= Id #" +
                            "<input type='hidden' name='LinkedUsers[#= index(data) #].Id' value='#= Id #' />"
                            );
                            columns.Bound(p => p.LinkedUserName).Visible(false).ClientTemplate("#= LinkedUserName #" +
                            "<input type='hidden' name='LinkedUsers[#= index(data) #].LinkedUserName' value='#= LinkedUserName #' />"
                            );
                            columns.Command(command => command.Destroy()).Width(110);
                        })
                        .Scrollable()
                        .Sortable()
                        .HtmlAttributes(new { style = "width:75%" })
                        .DataSource(source => source
                            .Ajax()
                            .Model(model =>
                            {
                                model.Id(d => d.Id);
                            })
                            .Batch(true)
                            .ServerOperation(false)
                            .Events(events => events.Error("error_handler"))
                            .Read(read => read.Action("LinkedUser_Read", "Account"))
                        )
                    )
                </div>
            </div>
        </div>
        <div class="form-horizontal">
            <div class="form-group">
                <label class="control-label col-md-2"></label>
                <div class="col-md-10">
                    <input type="submit" value="Save Changes" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
</div>
@model Pricing.Models.ViewModel.UserProfileUpdateViewModel
用户配置文件
@使用(Html.BeginForm(“UpdateProfile”、“Account”、FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(profile=>profile.UserId)
用户名
@User.Identity.Name
@LabelFor(profile=>profile.FirstName,“FirstName”,新的{@class=“controllabel col-md-2”})
@EditorFor(profile=>profile.FirstName,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(profile=>profile.FirstName,“,new{@class=“text danger”})
@LabelFor(profile=>profile.LastName,“LastName”,新的{@class=“controllabel col-md-2”})
@EditorFor(profile=>profile.LastName,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(profile=>profile.LastName,“,new{@class=“text danger”})
@LabelFor(profile=>profile.LinkedUsers,“链接用户”,新的{@class=“controllabel col-md-2”})
//TODO:此绑定失败,我需要绑定到UserProfileUpdateViewModel的IENumerable属性
@(Html.Kendo().Grid())
.名称(“LinkedUsers”)
.工具栏(工具=>
{
tools.Create().Text(“添加链接用户”);
})
.Editable(Editable=>Editable.Mode(GridEditMode.InCell).CreateAt(GridInsertRowPosition.Bottom).DisplayDeleteConfirmation(false))
.列(列=>
{
columns.Bound(p=>p.LinkedUserId).Visible(false).ClientTemplate(“#=Id#”)+
""
);
columns.Bound(p=>p.LinkedUserName).Visible(false).ClientTemplate(“#=LinkedUserName#”+
""
);
Command(Command=>Command.Destroy()).Width(110);
})
.Scrollable()
.Sortable()
.HtmlAttributes(新的{style=“width:75%”)
.DataSource(source=>source
.Ajax()
.Model(Model=>
{
model.Id(d=>d.Id);
})
.Batch(真)
.ServerOperation(错误)
.Events(Events=>Events.Error(“错误处理程序”))
.Read(Read=>Read.Action(“LinkedUser\u Read”,“Account”))
)
)
}

您应该传递IEnumerable本身,而不是类型

@(Html.Kendo().Grid<Model.LinkedUsers>()
@(Html.Kendo().Grid())

您应该传递IEnumerable本身,而不是类型

@(Html.Kendo().Grid<Model.LinkedUsers>()
@(Html.Kendo().Grid())

啊。好的,谢谢。我认为这是有意义的。那么,我应该从UserProfileUpdateViewModel中完全删除IEnumerable属性吗?因为,我认为如果网格绑定到它自己的LinkedUsers模型,IEnumerable属性的读取值将为空。我仍然不清楚如何传回该网格上的任何创建、更新或销毁函数网格如果网格的数据不是UserProfileUpdateViewModel.@CollinM.Barrett为什么删除它?的一部分,您只需在创建时检查列表是否为null,不呈现网格,仅在更新/删除时呈现。关于如何添加/创建/删除,您可以按照我使用的示例:Html.Kendo().grid(Model.OrderProjects)没有啊。好的,谢谢。我认为这是有意义的。那么,我应该从UserProfileUpdateViewModel中删除IEnumerable属性吗?因为,我认为如果网格绑定到它自己的LinkedUsers模型,IEnumerable属性的读取值将为空。我仍然不清楚我将如何传回t上的任何创建、更新或销毁函数网格如果网格的数据不是UserProfileUpdateViewModel的一部分。@CollinM.Barrett为什么要删除它?您只需在创建时检查列表是否为空,不呈现网格,并且仅在更新/删除时呈现。关于如何添加/创建/删除,您可以按照我使用的示例:Html.Kendo().grid(Model.OrderProjects)没有