我想,以检查用户是否在一个队列,如果因此增加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