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

时间:2014-08-18 18:43:09

标签: c# ajax asp.net-mvc-4 entity-framework-5

有点坚持这个,但我感谢所有我在这里向所有人学习。我有一个webgrid,当你选择其中一行时加载一个部分视图。显示公司信息,部分显示更多详细信息。效果很好。

为每家公司分配了特定用户,并且可以将用户分配到多个公司的多个角色。我正在尝试将用户添加到partialview以便可以选择进行编辑,但是我在控制器中覆盖部分视图的每次尝试都不会导致任何更改。任何建议都将不胜感激。

模型

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; }

}

查看

@{
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>

控制器

// 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());

1 个答案:

答案 0 :(得分:0)

看看最后的答案,它可能会有所帮助 {{3P>