asp.net mvc - print to webpage in C# code -


i'm trying check whether user adding dupicate ids in 1 cohort , if so, need print webpage warning: "id exists in cohort" , prevent user entering existing id again. did checking in if statement not know how print out webpage in else statement.

here code in c# file i'm trying modify in controller:

foreach (string studentid in cc.students) {     sqldatareader rdr = null;     cmd = new sqlcommand(@"select * customcohortstudent puid = @studentid , cohortid = @cohortid", sqlconn);     rdr = cmd.executereader();     if (rdr == null)     {         cmd = new sqlcommand(@"insert customcohortstudents(cohortid, puid) values (@id,@studentid)", sqlconn);     }     else     {         // code print warning webpage      } 

and don't know how should edit cshtml file in views accordingly.. here had in index.cshtml file:

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

so question is: should add in else statement in controller , modify in views?

if warning non-fatal, i.e. flow should continue going smoothly, need return more 1 information code insert: did succeed , what, if any, warnings are.

that means you'll better off kind of result class:

now, there many ways this, written 1 give idea i'm talking about. note, can skip result class , go straight returning list<string>, hides actual intention whoever going read code in future (you included). returning class descriptive name , properties means it's super-easy understand idea behind code skimming on it.

the result class maximally simplified, if want can use transfer other information (how many succeeded, or 1 failed etc., example display information in view).

public class result{     public list<string> warnings {get;} = new list<string>(); } 

then in method inserts:

public result myinsertmethod(...){     var result = new result();     foreach (string studentid in cc.students)     {         sqldatareader rdr = null;         cmd = new sqlcommand(@"select * customcohortstudent puid = @studentid , cohortid = @cohortid", sqlconn);         rdr = cmd.executereader();         if (rdr == null)         {             cmd = new sqlcommand(@"insert customcohortstudents(cohortid, puid) values (@id,@studentid)", sqlconn);         }         else         {             // code print warning webpage              result.warnings.add("something not awesome");         }     }     return result; } 

in controller:

viewbag.result = myinsertmethod(...); 

in view:

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

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -