【问题标题】:Controlling custom message in IF Else在 IF Else 中控制自定义消息
【发布时间】:2014-02-26 19:34:40
【问题描述】:

我开发了文本框来添加日期范围以选择该日期范围内的特定记录。 我已经为此编写了 IF ELSE,如果两个文本框都为空,则显示错误消息,但问题是当我第一次调用页面时,它会显示相同的错误。我知道为什么,因为在第一次尝试文本框是空的,但如何控制呢?

public ActionResult ShowMyAtdByDate(String DateFrom, String DateTo) 
    {
        int empId = 0;
        int.TryParse((string)Session["Employee"], out empId); // parse variable to int and saves the result in empId
     //   IEnumerable<GetMyAtd_DateResult> MyAtdRecord = DataContext.GetMyAtd_Date(DateFrom,DateTo,empId).ToList();

        if (DateFrom != "" && DateTo == "" && empId > 0)
        {
            IEnumerable<GetMyAtd_DateResult> MyAtdRecord = DataContext.GetMyAtd_Date(DateFrom,null, empId).ToList();
            ViewBag.Dates = "Records for" + " " + DateFrom;
            return View(MyAtdRecord);

        }
        else if (DateFrom == "" && DateTo != "" && empId > 0)
        {
            IEnumerable<GetMyAtd_DateResult> MyAtdRecord = DataContext.GetMyAtd_Date( null, DateTo, empId).ToList();
            ViewBag.Dates = "Records for" + " " + DateTo;
            return View(MyAtdRecord);
        }
        else if (DateFrom != "" && DateTo != "" && empId > 0)
        {
            IEnumerable<GetMyAtd_DateResult> MyAtdRecord = DataContext.GetMyAtd_Date(DateFrom, DateTo, empId).ToList();
            ViewBag.Dates = "Records from" + " " + DateFrom + " " + "to" + " " + DateTo;
            return View(MyAtdRecord);
        }
        else if (DateFrom == "" && DateTo == "" && empId > 0)
        {
            IEnumerable<GetMyAtd_DateResult> MyAtdRecord = DataContext.GetMyAtd_Date(null, null, empId).ToList();
            ViewBag.Dates = "No dates selection";
            return View(MyAtdRecord);
        }
        else if(empId <=0 )
        {
            return RedirectToAction("IsAuth_Page","Home");
        }

        return View();


    }

查看:

@{

     var grid = new WebGrid(ViewData.Model, rowsPerPage: 25);

 }

 @if (Model.Count > 0)
 {
     <div id="AllMyAtd_ByDate">
       @grid.GetHtml(columns: grid.Columns(
                                        grid.Column("EmplID", "Employee ID"),
                                        grid.Column("EmplName", "Employee Name"),
                                        grid.Column("ShiftID", "Shift ID"),
                                        grid.Column("DateVisited", "Date of Visit"),
                                        grid.Column("InTime", "In Time"),
                                        grid.Column("TimeOut", "Time Out"),
                                        grid.Column("OverTime", "Over Time"),
                                        grid.Column("TotalWorkingTime", "Total Working Time")
                                      ))
     </div>
 }
 else
 {
     <h4 class="error">Sorry Record Doesn't Exist for selected date(s)</h4>  
 }

当我第一次浏览到这个页面时,它会出现,实际上只有当我将两个文本框都留空时才会出现。

【问题讨论】:

    标签: c# asp.net asp.net-mvc-3 c#-4.0 if-statement


    【解决方案1】:

    用户第一次进入这个页面时,它将是一个 GET 请求。您可以从 GET 方法中删除此逻辑(因为它不需要在第一次执行时执行)并将其放入 POST 方法中,因为这是在用户已提交表单。


    [HttpGet]
    public ActionResult ShowMyAtdByDate()
    {
        int empId = 0;
        int.TryParse((string)Session["Employee"], out empId);
    
        if (empId <= 0)
        {
            return RedirectToAction("IsAuth_Page", "Home");
        }
    
        return View();
    }
    
    [HttpPost]
    public ActionResult ShowMyAtdByDate(string dateFrom, string dateTo)
    {
        if (dateFrom != "" && dateTo == "" && empId > 0)
        {
            ...
        }
        else if (dateFrom == "" && dateTo != "" && empId > 0)
        {
            ...
        }
        etc...
    }
    

    并确保在您的视图中您的表单具有 POST 方法

    【讨论】:

    • 我的意思是您的控制器中需要有两种方法:一种用于 GET(几乎是空的(可能只有 if(empId
    • 我不太明白你的回答,先生,你能编辑我的代码吗?
    猜你喜欢
    • 2023-03-07
    • 2018-01-18
    • 2017-11-14
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多