Thursday 15 April 2010

asp.net - Check for empty value in C# - Webmatrix -


I am creating an input field value where sometimes the value exists and sometimes I do not need to check it That's what the value is empty and will not show anything if it is not. Using my current code .IsNullOrEmpty:

  & lt; Input id = "Lead-entry" class = "Forms text box" type = "text" size = "5" name = "q15_2013Collections [0] []" value = "@ (queryinputvalue.FirstOrDefault (r = & gt; r .field_name.Equals ( "q15_2013Collections [0] []")) field_data.IsNullOrEmpty "":.? queryinputvalue.FirstOrDefault (r = & gt; R.field_name.Equals ( "q15_2013 resolution [0] []"))) .field_data) "/>   

Which gives me the error:

  Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: can not be binding order for a null reference < / Code>  

Thanks

Edit: This is the code I used to end:

Helpers.cshtml

  checkEmpty (IEnumerable & LT; dynamic & gt; queryinputvalue, string FIELD_ID) @helper {var reqValue = queryinputvalue.FirstOrDefault (r = & gt; r.field_name.Equals (FIELD_ID)); Var return_value = ""; If (reqValue! = Null) {return_value = reqValue.field_data; } And {return_value = ""; } @ Return_value}   

On page

  @ {IEnumerable & lt; Dynamic & gt; Queryinputvalue = db.Query ("select * document_data WHERE doc_id = @ 0", doc_id); } & Lt; Input type = "text" class = "Forms text box" id = Name = "Q4_arborLoan" size = "20" value "INPUT_4" = "@ Helpers.checkEmpty (queryinputvalue," q4_arborLoan ")" / & gt;    

Why are you doing this in view ? You'll need to check these conditions controller to be written down to their ViewModel :

  public class ViewModel {public string QueryValue {get; Set ;.}}   

and in your controller method you will simply populate this model and return it to the View page

  public ActionResult MyMethod () { ViewModel Model = New View Modell (); // Get some value from anywhere String Recovery = some value. FirstOver Default (R => R. FieldName.quals ("q15_2013 resolution [0] []")); String query value = string Empty (); If (queryValues! = Null) {queryValue = string.IsNullOrEmpty (reqValue.field_data)? "": Recall. Field_data; } Model.QueryValue = queryValue; See Return (Model); }   

Then, your view will be just type

  @model     

No comments:

Post a Comment