1

さまざまなシナリオで返される「WebGridColumn」を定義する webgridextensions クラスがあります。

 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 = "Scheduled"の場合、アクションリンクの1つのセットを表示し、そうでない場合は別のセットを表示します...

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

どうすればこれを達成できますか?

ありがとう

4

1 に答える 1

2

次のように、三項式 ?:を使用できるはずです。

columns: grid.Columns(
    model.AppointmentStatus == "Scheduled" ? 
        grid.ApptLinksWCancel(Html) : grid.ApptLinksWSchedule(Html)
),

編集

より複雑な場合は、追加のパラメーターを取る拡張メソッドを追加できます。

public static WebGridColumn ApptLinks(this WebGrid grid, HtmlHelper html, string status)
{
    if (status == "Schedule")
        return grid.ApptLinksWSchedule(html);
    else
        return grid.ApptLinksWCancel(html);
}

同じように使用します。

columns: grid.Columns(
    grid.ApptLinks(Html, model.AppointmentStatus),
    ...
),
于 2013-08-22T23:01:21.897 に答える