简体   繁体   English

在部分视图加载中向模型添加数据

[英]Add data to model on partialview load

Kinda stuck on this one but I appreciate all I'm learning from everyone here on SO. Kinda坚持了这一点,但是我非常感谢我从这里向每个人学习的所有知识。 I have a webgrid that loads a partialview when you select one of the rows. 我有一个Webgrid,当您选择行之一时会加载一个partialview。 Displays company information and the partial displays more details. 显示公司信息,部分显示更多详细信息。 Works great. 效果很好。

Each company is assigned certain users, and users can be assigned to multiple roles in multiple companies. 每个公司都被分配了某些用户,并且可以将用户分配给多个公司中的多个角色。 I'm trying to add the users to the partialview so they can be selected for editing, but every attempt I make to override the partialview in the controller results in no change. 我正在尝试将用户添加到partialview,以便可以选择它们进行编辑,但是我所做的每次尝试在控制器中覆盖partialview都不会导致更改。 Any suggestions would be greatly appreciated. 任何建议将不胜感激。

Model 模型

public class Clients
{
    [Key]
    public int ID { get; set; }
    public int EINC { get; set; }
    public string CompanyId { get; set; }
    public string CompanyName { get; set; }
    public ClientUsers[] ClientUserList  { get; set; }
    public ClientUsers AddedItem { get; set; }
}

public class ClientUsers
{
    public string UserName { get; set; }
    public string ReportingCompanyID { get; set; }
    public Guid UserId { get; set; }

}

View 视图

@{
MyName.Models.Clients clients = new MyName.Models.Clients();
}
...
           @{
                var grid = new WebGrid(Model, canPage: true, rowsPerPage: 30, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridContent");
                grid.Pager(WebGridPagerModes.NextPrevious);}
            <div id="gridContent">

                @grid.GetHtml(tableStyle: "webGrid",
                        headerStyle: "header",
                        alternatingRowStyle: "alt",
                        selectedRowStyle: "select",
                        columns: grid.Columns(
                        grid.Column("Id", format: (i) => i.GetSelectLink(i.CompanyId), style: "compid"),
                        grid.Column("CompanyName", "CompanyName", style: "description")
                 ))

            @if (grid.HasSelection)
            {
                clients = (MyName.Models.Clients)grid.Rows[grid.SelectedIndex].Value;
                Html.RenderPartial("~/Views/Client/_ClientAdminDetails.cshtml", clients);

            }

PartialView 局部视图

@model MyName.Models.Clients
<h3>@Model.CompanyName<br /><em>@Model.CompanyId</em></h3>

Controller 控制者

// create a List and add query results to it, then add list object to model
// and return to partialview

        var q = from a in db.AllowedCompanies
                join p in db.CustomUserProfiles
                    on new { a.CustomUserProfileUserId, a.CustomUserProfileID }
                    equals new { CustomUserProfileUserId = p.UserId, CustomUserProfileID = p.ID }
                where
                  a.ReportingCompanyID == clients
                orderby
                  p.UserName
               // select new ClientUser()
               // {
               //     UserName = p.UserName,
               //     ReportingCompanyID = a.ReportingCompanyID,
               //     UserId = p.UserId
               // };

       // ObservableCollection<ClientUser> userlist = new ObservableCollection<ClientUser>(q.ToList());

看看最后的答案,也可能是有帮助请点击

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM