我想,以檢查用戶是否在一個隊列,如果因此增加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語句在控制器和視圖哪些修改?
謝謝!如果我的方法是: public ActionResult Create(CustomCohort cc)。它與Result相同嗎? – Sophie
我應該在控制器中放置這行代碼:「ViewBag.Result = MyInsertMethod()」?由於「MyInsertMethod()」已經在控制器中...謝謝! – Sophie
在你的例子中'''ViewBag.Whatever'''進入動作('''Create(CustomCohort cc)''')。實際的插入在他們自己的獨立方法中是最好的,因爲控制器操作中的所有東西,SQL查詢,數據處理和設置View都會讓人困惑;) – Gerino