MVC 5 Razor - 显示文本而不是id

时间:2016-04-05 10:24:45

标签: c# json asp.net-mvc razor model-view-controller

我构建项目管理系统和项目表保持项目类型id - 以及项目类型的值存储在另一个表(ProjectTypes)中。 我是通过json发送所有项目的,我不能提取类型名称,只有ID ... 我该怎么处理呢?

项目类:

public class Project
{
    //Connection with DB Layer
    DataLayer dal = new DataLayer();
    private const string CClassName = "Project";

    /*All Properties of Project object*/

    #region Properties

    public int ID { get; set; }
    public string NameHeb { get; set; }
    public string NameEng { get; set; }
    public int type_id { get; set; }
    public int Category { get; set; }
    public int lecturer1 { get; set; }
    public int lecturer2 { get; set; }
    public string shortDescription { get; set; }
    public string Description { get; set; }
    public string NeededKnowledge { get; set; }
    public int StageID { get; set; }
    public int thumbnailID { get; set; }
    public byte isActive { get; set; }
    public byte isApproved { get; set; }
    public byte isAttached { get; set; }
}

项目类型:

public class ProjectType
{
    //Connection with DB
    DataLayer dal = new DataLayer();

    #region Properties
    /* Straight object properties */
    public int DepID { get; set; }
    public string value { get; set; }
    public byte isActive { get; set; }
    #endregion
}

控制器:

[AllowAnonymous]
public JsonResult ProjectsByJSON()
{
    List<Project> objProjects = new Project().GetAllProjects();
    return Json(objProjects, JsonRequestBehavior.AllowGet);
}

查看:

<div id="login">
    <div class="Table" id="ProjectsTbl">

        <div class="Heading">
            <div class="Cell">
            </div>
            <div class="Cell">
                <p>Project name</p>
            </div>
            <div class="Cell">
                <p>Type</p>
            </div>
            <div class="Cell">
                <p>Description</p>
            </div>
            <div class="Cell">
                <p>stage</p>
            </div>
        </div>

        <script language="javascript">
            $.get("ProjectsByJSON", null, BindData);

            function BindData(curProjects)
            {
                var tbl = $("#ProjectsTbl");

                for (i=0; i<curProjects.length;i++)
                {
                    var row = "<div class=\"Row\">" +
                            "<div class=\"Cell\"></div>" +
                            "<div class=\"Cell\">" + curProjects[i].NameHeb + "</div>" +
                            "<div class=\"Cell\">" + curProjects[i].Name + "</div>" +
                            "<div class=\"Cell\">" + curProjects[i].shortDescription + "</div>" +
                            "<div class=\"Cell\">" + curProjects[i].Stage + "</div>" +
                         "</div>";
                    tbl.append(row);
                }
            }
        </script>

    </div>
</div>

1 个答案:

答案 0 :(得分:0)

我认为最好的方法是通过连接两个表(Project和ProjectTypes)来编码并返回相同的代码。然后你将获得Id和Name。