2017-04-07 75 views
0

我想,以檢查用戶是否在一個隊列,如果因此增加dupicate ID的網頁,我需要打印到網頁中的警告:「ID已經存在於隊列」,並防止用戶從再次輸入現有ID。 現在我已經在if語句中進行了檢查,但是我不知道如何在ELSE語句中打印出網頁。打印在C#代碼

這是在C#中的文件,我想在控制器中修改代碼:

foreach (string studentID in cc.students) 
{ 
    SqlDataReader rdr = null; 
    cmd = new SqlCommand(@"select * from CustomCohortStudent where PUID = @StudentID and cohortID = @cohortID", sqlConn); 
    rdr = cmd.ExecuteReader(); 
    if (rdr == null) 
    { 
     cmd = new SqlCommand(@"insert into CustomCohortStudents(cohortID, PUID) values (@id,@StudentID)", sqlConn); 
    } 
    else 
    { 
     // code to print warning to the webpage 
    } 

而且我不知道我應該如何在查看相應地編輯CSHTML文件.. 以下是我在我的index.cshtml文件:

<h2>Cohorts</h2> 

<a href="CustomCohort/Create">Create</a><br /><br /> 

<table class="table"> 
    <tr> 
     <!-- 
     <th> 
      @Html.DisplayNameFor(model => model.id) 
     </th> 
     --> 
     <th> 
      @Html.DisplayNameFor(model => model.name) 
     </th> 
     <th> 
      <!--Actions--> 
     </th> 
    </tr> 

    @foreach (var item in Model) 
    { 
     <tr> 
      <!-- 
      <td> 
       @item.id 
      </td> 
      --> 
      <td> 
       @item.name 
      </td> 
      <td> 
       <a href="CustomCohort/Edit/@item.id">Edit</a> | 
       <a href="CustomCohort/Delete/@item.id">Delete</a> 
      </td> 
     </tr> 
    } 
</table> 

所以我的問題是:我應該添加else語句在控制器和視圖哪些修改?

回答

1

如果警告非致命性,即流動應該繼續像現在一切進展順利,那麼你需要從你的代碼,不會插入返回多於一個信息:沒有它成功,什麼,如果有的話,警告是。

這意味着,你將可能與某種結果類的更好:

現在,有很多方法可以做到這一點,這是一個飛快地寫着一個有希望會給你一個想法是什麼我在說。請注意,您可以跳過Result課程並直接返回List<string>,但它隱藏了將來要閱讀此代碼的人(包括您)的實際意圖。用一個描述性的名字和屬性返回一個類意味着通過簡單地理解代碼背後的想法是非常容易的。

如果您希望您可以使用它來傳輸其他信息(有多少成功或哪一個失敗等,例如在您的視圖中顯示該信息),則Result類是最大限度的簡化。

public class Result{ 
    public List<string> Warnings {get;} = new List<string>(); 
} 

然後在你的方法,做插件:

public Result MyInsertMethod(...){ 
    var result = new Result(); 
    foreach (string studentID in cc.students) 
    { 
     SqlDataReader rdr = null; 
     cmd = new SqlCommand(@"select * from CustomCohortStudent where PUID = @StudentID and cohortID = @cohortID", sqlConn); 
     rdr = cmd.ExecuteReader(); 
     if (rdr == null) 
     { 
      cmd = new SqlCommand(@"insert into CustomCohortStudents(cohortID, PUID) values (@id,@StudentID)", sqlConn); 
     } 
     else 
     { 
      // code to print warning to the webpage 
      result.Warnings.Add("Something was not awesome"); 
     } 
    } 
    return result; 
} 

在控制器:

ViewBag.Result = MyInsertMethod(...); 

在視圖:

<h2>Cohorts</h2> 

if(ViewBag.Result != null){ 
    foreach(var warn in ((Result)ViewBag.Result).Warnings){ 
     <span class="warning">@warn</span> 
    } 
} 

<a href="CustomCohort/Create">Create</a><br /><br /> 
+0

謝謝!如果我的方法是: public ActionResult Create(CustomCohort cc)。它與Result相同嗎? – Sophie

+0

我應該在控制器中放置這行代碼:「ViewBag.Result = MyInsertMethod()」?由於「MyInsertMethod()」已經在控制器中...謝謝! – Sophie

+0

在你的例子中'''ViewBag.Whatever'''進入動作('''Create(CustomCohort cc)''')。實際的插入在他們自己的獨立方法中是最好的,因爲控制器操作中的所有東西,SQL查詢,數據處理和設置View都會讓人困惑;) – Gerino