asp.mcv如何将字符串转换为布尔标志

时间:2016-10-31 09:24:54

标签: c# asp.net razor

我正在尝试登录页面。但是当我按 F5 页面时,这一行会出错:

(function () {
            'use strict';
            var controllerId = 'SeriesEditor';
            angular.module('app').controller(controllerId, ['apiFactory', "$filter", SeriesEditor]);

            function SeriesEditor(wfapi, $filter) {
                var vm = this;

                vm.Init = function () {                   
                    vm.Levels = [];                  
                }

     vm.loadLevelsForSeries = function () {

                    var mReq = new Models.services.LevelListRequest();
                    api.get(mReq)
                        .then(function (data) {
                            vm.Levels = data.Results;
                        });
                }
     }
        })();

Login.cs

flag = Convert.ToBoolean(cmd.ExecuteScalar());

Index.cshtml

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace LoginFormApp.Models
{
public class Login
{
    [Required(ErrorMessage = "Username is required")] 
    [Display(Name = "Username")]   
    public string username { get; set; }
    [Required(ErrorMessage = "Password is required")]
    [Display(Name = "Password")]
    public string password { get; set; }
    public bool checkUser(string username, string password) 
    { 
        bool flag = false;
        string connString =        ConfigurationManager.ConnectionStrings["MyDatabaseEntities"].ConnectionString    ; 
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("Select count(*) from Login     where username='" + username + "' and password='" + password + "'", conn);
            flag = Convert.ToBoolean(cmd.ExecuteScalar());
            return flag;
        }
    } 
}
} 

show.cshtml

@model LoginFormApp.Models.Login
@{
 Layout = null;
}
 <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>DNDB Login</title>
</head>
<body>
<div style="margin:0 auto; text-align: center; border: 2px; border-style:   solid; width:400px;background-color:lightgrey">
    @using (Html.BeginForm())
    {
        <table>
            <tr>
                <td>@Html.LabelFor(m => m.username)</td> 
                <td>@Html.TextBoxFor(m => m.username)</td> 
                <td>@Html.ValidationMessageFor(m => m.username)</td> 
            </tr>
            <tr>
                <td>@Html.LabelFor(m => m.password)</td> 
                <td>@Html.PasswordFor(m => m.password)</td> 
                <td>@Html.ValidationMessageFor(m => m.password)</td> 
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" value="Submit" />
                </td> 
            </tr>
            <tr>
                <td></td>
                <td>
                    @ViewBag.Message
                </td>
            </tr>
        </table>
    }
</div>
</body>
</html>

Homecontroller.cs

@model LoginFormApp.Models.Login
@{
Layout = null;
}
<!DOCTYPE html>
 <html>
 <head>
<meta name="viewport" content="width=device-width" />
<title>Show</title>
</head>
<body>
<div>
    <h3> Hi! @Model.username</h3> 
</div>
</body>
</html>

提前致谢。

2 个答案:

答案 0 :(得分:0)

你做错了, ExecuteScalar()总是返回第一列的值,因此在您的情况下,它返回不能直接转换为布尔值的总计数。

int totalCount = Convert.ToInt32(cmd.ExecuteScalar());
flag = totalCount > 0;

答案 1 :(得分:0)

您可以使用

flag = (int)cmd.ExecuteScalar() == 1;