c# - hide the default value of a textBoxFor -


note - asp.net mvc 4 application using razor 2 views... have textbox in view waiting user input data, data makes values properties of model want save db.

the data associated textbox has [required] tab in model. cannot save model without value textbox model not valid.

i know can add @value @htmltextboxfor line, put means value displayed user.

is there away have default value hidden, user see's placeholder text "value" saved db. ideas...?

textbox..

  @html.textboxfor(n => n.article.title, new { @class = "span4 m-wrap", rows = 1 , @placeholder = "enter news articles main subject or title here" , @value = "title"}) 

controller

   if (modelstate.isvalid)     newsarticle newnews = new newsarticle();     newnews.title = newsarticle.article.title; 

you can add id textbox follows:

@html.textboxfor(n => n.article.title, new { @class = "span4 m-wrap", rows = 1 , @placeholder = "enter news articles main subject or title here" , @value = "", @id="txttitle"}) 

then call following jquery function on form submit event

$(function(){        $("form").submit(function () {         if($("#txttitle").val() == ""){             $("#txttitle").val("default value");         }     }); }); 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -