简体   繁体   中英

Add data to model on partialview load

Kinda stuck on this one but I appreciate all I'm learning from everyone here on SO. I have a webgrid that loads a partialview when you select one of the rows. 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. 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());

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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