Eon
Eon

Reputation: 3974

How do I set the default values of MVC textAreaFor<> boxes?

Ok, here is my situation, which has come to this... dramatic pause...

I have the following TextAreaFor<> boxes in my MVC view:

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Monday) %>
        </div>
        <div class="editor-field">
            <%: Html.TextAreaFor(model => model.Wednesday, 6, 40, new { })%>
            <%: Html.ValidationMessageFor(model => model.Monday) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Wednesday) %>
        </div>
        <div class="editor-field">
            <%: Html.TextAreaFor(model => model.Wednesday, 6, 40, new {})%>
            <%: Html.ValidationMessageFor(model => model.Wednesday) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Friday) %>
        </div>
        <div class="editor-field">
            <%: Html.TextAreaFor(model => model.Friday, 6, 40, new {}) %>
            <%: Html.ValidationMessageFor(model => model.Friday) %>
        </div>

For those with a lot of experience, you might have noticed that I am using strongly Typed views to lessen development time, so a lot of the default items are here.

I need to know, how do I set the default values for the TextAreaFor<> boxes? I.E, I might want text in it which will serve as a Template. Whatever the user adds to these textAreas will be stored in a database (Back end for that is already complete and functional). As seen in the post ASP.NET MVC - How can I set the Default Value in a Strongly Typed TextArea?, I am not sure I can use this method, since I am not sure if the model variable will be sent back to my controller.

And another quick question (QuestionCeption) If I save data from these textAreas to the database (Using SQL Server Express 2008), will it conserve the new line characters?

Here is a sample of default values I will need.

Traditional:
Healthy Lifestyle:
Chill out:
Vegetarian:
Soup:
Sandwich:
No thanks.

Upvotes: 0

Views: 1550

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038890

You could do that inside the controller action that is rendering this view:

public ActionResult Index()
{
    MyViewModel model = ...
    model.Friday = "some default value";
    return View(model);
}

and you will have a corresponding POST action which will retrieve the values:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // model.Friday will contain the text entered by the user in the corresponding textarea
    // here you could store for example the values in the database
    ...
}

And another quick question (QuestionCeption) If I save data from these textAreas to the database (Using SQL Server Express 2008), will it conserve the new line characters?

Yes, you just need to save the text as-is in the database without doing any transformations. This will work when you display later the text in a textarea but if you want to display it for example in a <div> you will have to HTML encode it and replace the new lines by <br/>. For example you could write a custom HTML helper for this job.

Upvotes: 3

Related Questions