Sophie
Sophie

Reputation: 13

print to webpage in C# code

I'm trying to check whether user is adding dupicate IDs in one cohort and if so, I need to print to the webpage a warning: "ID already exists in the cohort" and prevent the user from entering the existing ID again. Now that I did the checking in the if statement but I do not know how to print out to the webpage in the ELSE statement.

Here is the code in the C# file that I'm trying to modify in the Controller:

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 
    }

And I don't know how should I edit the cshtml file in the Views accordingly.. Here is what I had in my 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 my question is: What should I add in the ELSE statement in the Controller and what to modify in the Views?

Upvotes: 0

Views: 810

Answers (1)

Gerino
Gerino

Reputation: 1983

If the warning is non-fatal, i.e. the flow should continue like with everything going smoothly, then you need to return more than one information from your code that does the insert: did it succeed and what, if any, warnings are.

That means that you'll be probably better off with some kind of Result class:

Now, there are many ways to do this, this is a quickly written one that hopefully will give you an idea about what I'm talking about. Note, that you can skip the Result class and go straight for returning List<string>, but it hides your actual intention from whoever is going to read this code in the future (you included). Returning a class with a descriptive name and properties means it's super-easy to understand the idea behind the code with just skimming over it.

The Result class is maximally simplified, if you want you can use it to transfer other information (how many succeeded, or which one failed etc., to for example display that information in your View).

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

Then in your method that does the inserts:

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

In Controller:

ViewBag.Result = MyInsertMethod(...);

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

Upvotes: 1

Related Questions