2017-03-02 76 views
0

下午夥計們,顯示一條消息,用戶如果字段爲空

我是新來的C#和IM試圖創建一個if語句來檢查兩個字段的值,如果這兩個領域都是空白,給再向用戶顯示消息。

我的兩個領域是出生日期與日期時間的數據類型和年齡爲int的數據類型。我使用studio 2013,並有一個C#mvc項目。

在我create.cshtml頁我使用剃刀語法這兩個表單域。

@Html.EditorFor(model => model.client.DateOfBirth, new { htmlAttributes = new { @class = "form-control", @placeholder = "dd-mm-yyyy" } }) 

@Html.EditorFor(model => model.client.AgeNotKnown, new { htmlAttributes = new { @class = "form-control", @placeholder = "Age in Years" } }) 

用戶需要提交表單時,以填補在任一個或其他字段。

在我的控制,我的HttpPost爲的ActionResult創建。我已經嘗試了幾種方法來通過if語句獲得這個工作,但我似乎得到了這個工作。

我已經使用string.IsNullOrEmpty方法試過了,但是因爲這些日期時間和int數據類型,這是不行的。見下面的示例。

if (string.IsNullOrEmpty(clientViewRecord.client.AgeNotKnown));    
      { 
       MessageBox.Show("please an Age if DOB not known"); 
      } 

db.ClientRecords.Add(clientViewRecord.client); 
       db.SaveChanges(); 
       return RedirectToAction("MainMenu", "Home"); 

我得到三個錯誤的位置:

  1. 參數1:不能從轉換'詮釋?爲「字串」

  2. 名稱「MessageBox的」不存在於當前上下文3)關於「string.IsNullOrEmpty(字符串)」的 最好重載的方法匹配存在具有 一些無效參數

  3. 最好對於「string.IsNullOrEmpty(串)」重載的方法匹配具有 一些無效參數

另一種方式我試圖得到這個工作在以下...

 if (clientViewRecord.client.AgeNotKnown == 0 || clientViewRecord.client.DateOfBirth == 0);    
      { 
       MessageBox.Show("please enter an Age or if DOB"); 
      } 

      db.ClientRecords.Add(clientViewRecord.client); 
      db.SaveChanges(); 
      return RedirectToAction("MainMenu", "Home");   

我得到同樣的問題的消息框,爲出生日期以下申請。

  1. 運算符'=='不能應用於'System.DateTime?'類型的操作數。和'int'

我是新來的C#但認爲我有點在正確的線條,我認爲只需要一點幫助或任何建議。

問候 貝蒂

+0

'MessageBox'是窗體窗體(它在web應用程序中沒有用)。添加一個'ModelState'錯誤並返回視圖,以便消息顯示在你的'@ Html.ValidationSummary()' - ModelState.AddModelError(「」,「你的錯誤信息」);返回查看(模型);' –

+0

並使您的屬性可以爲空 - 'int? AgeNotKnown'和'DateTime? DateOfBirth',以便用'if(DateOfBirth.HasValue)' –

+0

測試它們。但是,處理這個uis的正確方法是創建一個實現'IClientValidatable'的自定義驗證屬性,以便同時獲得客戶端和服務器端驗證 - [完全指南在ASP.NET MVC 3驗證 - 第2部分](http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2 ) –

回答

0

?意味着nullable其默認值將是null如果我使用int? i TS默認值將是null如果我使用int其默認值將是0

有關驗證您可以在MVC使用ModelState進行驗證,並添加自定義驗證

@Html.ValidationMessage("validation") 
    @Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control", @placeholder = "dd-mm-yyyy" } }) 
    @Html.EditorFor(model => model.AgeNotKnown, new { htmlAttributes = new { @class = "form-control", @placeholder = "Age in Years" } }) 

我用@Html.ValidationMessage("validation")它會顯示確認消息,如果我返回任何和在你的action

if (cls.DateOfBirth == null && cls.AgeNotKnown == null) 
      { 
       ModelState.AddModelError("validation","Please select one"); 
       return View(); 
      } 

在行動它會檢查條件如果兩者都是null,那麼它將返回到相同的視圖,包括在視圖中定義的keyvalidation的驗證消息。

0

所有MessageBox類的首先是屬於Windows窗體。如果你想要用戶,你應該使用驗證。我喜歡jquery validation

要添加控制這個樣子,只是把一個斷點到您的代碼,並期待希望數據從表單來的。這樣你可以決定你想如何控制你的數據。

System.DateTime?和int ?,這個問號告訴你這些變量可以爲null。所以你應該考慮一下比較。

0

首先創建視圖模型對這種驗證

public class ClientViewModel 
{ 
    [Required, DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)] 
    public DateTime Date { get; set; } 

    [Required] 
    public int Age { get; set; } 
} 

在查看

@model Models.ClientViewModel @*reference to your model usally create in Models folder*@ 

@Html.ValidationSummary() @* add this for validation summary displays here.*@ 

@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control", @placeholder = "dd-mm-yyyy" } }) 
@Html.ValidationMessageFor(m => m.Date) 

@Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control", @placeholder = "Age in Years" } }) 
@Html.ValidationMessageFor(m => m.Age) 

@* Add jqueryval bundles which is created in app_start/BundleConfig *@ 
@section Scripts{ 
    @Scripts.Render("~/bundles/jqueryval") 
    } 

在控制器

//Change the name of PostMethod 
public ActionResult PostMethod(Models.ClientViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     // do your stuff 
    } 


    return View(); 
} 
相關問題