2013-08-22 67 views
1

我有一個webgridextensions類「WebGridColumn」返回不同情況下的定義..的mvc 4的WebGrid條件htmlActionLinks

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 =「預定」,顯示出一組動作環節,如果沒有顯示出另一組...

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

如何做到這一點?

感謝

+0

對不起大家,我,M睡着了艾芬這裏,我需要的是在edits..Different鏈接根據狀態值... – foxtrotZulu

回答

2

您應該能夠使用這個ternary expression ?:,如:

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), 
    ... 
), 
+0

感謝的人,這是有用!我編輯了我的帖子,因爲我真正需要看到的是每行都有自己的一組動作鏈接,全部由約會狀態的值決定。 – foxtrotZulu