首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mvc 4 Webgrid条件htmlActionLinks

Mvc 4 Webgrid条件htmlActionLinks
EN

Stack Overflow用户
提问于 2013-08-22 22:55:03
回答 1查看 2.1K关注 0票数 1

我有一个webgridextensions类,定义了不同场景的"WebGridColumn“返回。

代码语言:javascript
复制
 public static WebGridColumn ApptLinksWSchedule(this WebGrid grid, HtmlHelper html)
    {

        return grid.Column(
            header: "",
            style: "actionColumn",
            format: item => new HtmlString(
                html.ActionLink("Schedule", "Schedule", new { ID = item.CourseExamApptID }, new { onclick = "return confirm('Are you sure you'd like to schedule this Appointment?');" }).ToString()

            )
        );
    }


    public static WebGridColumn ApptLinksWCancel(this WebGrid grid, HtmlHelper html)
    {

        return grid.Column(
            header: "",
            style: "actionColumn",
            format: item => new HtmlString(
                html.ActionLink("Edit", "Edit", new { ID = item.CourseExamApptID }).ToString() + " | " +
                html.ActionLink("Delete", "Delete", new { ID = item.CourseExamApptID }).ToString() + " | " +
                html.ActionLink("Cancel", "Cancel", new { ID = item.CourseExamApptID }, new { onclick = "return confirm('Are you sure you'd like to cancel this Appointment?');" }).ToString()
            )
        );
    }

在我看来,我需要根据模型属性的值有条件地应用htmlActionLinks。如果我的model.AppointmentStatus =“计划”,显示一组操作链接,如果不显示另一组.

代码语言:javascript
复制
<div class="webgrid-wrapper">
<div id="grid">
    @grid.GetHtml(
        tableStyle: "webgrid",
        headerStyle: "webgrid-header",
        footerStyle: "webgrid-footer",
        alternatingRowStyle: "webgrid-alternating-row",
        selectedRowStyle: "webgrid-selected-row",
        rowStyle: "webgrid-row-style",
        columns: grid.Columns(grid.Column("StudentID", "SMUId"),
                                grid.Column("StudentName", "Name"),
                                grid.Column("CourseName", "Course"),
                                grid.Column("InstructorName", "Professor"),
                                grid.Column("ExamLength", "ExamLength"),
                                grid.Column("SchedExamBeginTime", "Time In"),
                                grid.Column("SchedExamEndTime", "Time Out"),
                                grid.Column("Notes", "Notes"),
                                grid.Column("AppointmentStatus", "Status"),

                                //THIS IS WHat I'd like to display in a column...

                                        //if (model.AppointmentStatus == "Scheduled")
                                        //{
                                        //    html.ActionLink("Edit", "Edit", new { ID = item.CourseExamApptID }).ToString() + " | " +
                                        //    html.ActionLink("Delete", "Delete", new { ID = item.CourseExamApptID }).ToString() + " | " +
                                        //    html.ActionLink("Cancel", "Cancel", new { ID = item.CourseExamApptID }, new { onclick = "return confirm('Are you sure you'd like to cancel this Appointment?');" }).ToString()
                                        //}

                                        //html.ActionLink("Schedule", "Schedule", new { ID = item.CourseExamApptID }, new { onclick = "return confirm('Are you sure you'd like to schedule this Appointment?');" }).ToString()





                                //THIS IS MY ORIGINAL CODE THAT WORKS with only needing same column and link for every record...

                                //grid.ApptLinksWCancel(Html)


            )
         )
</div>

我怎样才能做到这一点?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-22 23:01:21

您应该能够为此使用三元表达?:,如下所示:

代码语言:javascript
复制
columns: grid.Columns(
    model.AppointmentStatus == "Scheduled" ? 
        grid.ApptLinksWCancel(Html) : grid.ApptLinksWSchedule(Html)
),

编辑

对于更复杂的内容,可以添加一个带有额外参数的扩展方法:

代码语言:javascript
复制
public static WebGridColumn ApptLinks(this WebGrid grid, HtmlHelper html, string status)
{
    if (status == "Schedule")
        return grid.ApptLinksWSchedule(html);
    else
        return grid.ApptLinksWCancel(html);
}

以同样的方式使用它:

代码语言:javascript
复制
columns: grid.Columns(
    grid.ApptLinks(Html, model.AppointmentStatus),
    ...
),
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18392086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档