2017-04-07 81 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