1

In the problem prepopulate Html.TextBoxFor in asp.net mvc 3 you can see an answer in which the following piece of code works correctly.

ViewBag.CompName = "Some Name";

Then in your view:

@Html.TextBoxFor(model =>model.Comps.CompName, new {@Value = ViewBag.CompName})

However, when the textbox gets the initial value of an empty string "", it seems to post a value of null for this textbox.

ViewBag.CompName = "";

This sends a null value instead of an empty string.

Is there any way of returning an empty string instead of null?

1
  • while adding in ViewBag, you can add string.empty instead of ViewBag.CompName if it is null Commented May 10, 2013 at 15:00

1 Answer 1

12

This is expected behavior. Try using DisplayFormat attribute.

[DisplayFormat(ConvertEmptyStringToNull=false)] on top of CompName property in your model.

See Reference

For Example:-

[DisplayFormat(ConvertEmptyStringToNull = false)]
public string CompName
{
    get { return _compName; }
    set { _compName= value; }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.