0

im new to mvc, in normal asp.net i could do this in a hearbeat but im still struggling to get my head around mvc!

basically im not using a DB for this application, its a simple tool that modifies large amounts of text and outputs it to the user.

because the amount of text is so large i want the user to be able to save and load the text they have entered by downloading a text file. ive figured out how to save it, but i just cant seem to find a way that works to load the text.

for this example however lets just work with a standard string as i know how to parse my save file and get the text into the appropriate strings. What i dont know is how to get these strings into the textboxes. There are no models that are attached to this as its not being validated or stored on the server.

View1 (how my textareas are laid out)

  <label for="p1s1">Sentance 1:</label>
  <%= Html.TextArea("p1s1", new { @class = "textbox" })%> <p>

Is it best to create a new view and then pass the paramaters into view1? im really stuck here!

Thanks in advance

2
  • 1
    BTW: Scott, if you are new to MVC, I'd recommend you to start using Razor as view engine. I found it much easier to work with and understand. It fits better to a used C# developer. Commented Jul 4, 2012 at 14:31
  • Thanks for the advice andre - ill look into it for future projects. Could you possibly elaborate on the method you mentioned in kamils post please? Commented Jul 4, 2012 at 15:03

3 Answers 3

1

Create strongly-typed View ( typed by Your Model) enter image description here

and use helper,like: Html.TextAreaFor(model=>model.myProperty)

For example:

class User 
{
(...)
public string Description {get;set;}
}

if Your View is typed by User use:

Html.TextAreaFor(model=>model.Description)
Sign up to request clarification or add additional context in comments.

4 Comments

He doesn't need a view typed for a class. It would be enough to declare the model as string and to pass the string at the Action return (return View("text");). It feels a bit exagerated the way you are explaining.
If Scott wrote he is new in mvc i want to give Him "step by step" answer. And passing to a view only one text is , i think, a quite rare situation. Passing complex object is standard.I've chosen more useful example.
It's the standard indeed, but I think it's just not necessary in this scenario. Anyway, I see your point
thanks for the response kamil, i think i understand it it could be a long night - i have over 100 of these text areas that ill need to modify! Andre could you possibly elaborate on your method please?
0

Models in mvc are not only for when you want to save data to databases or even store them on the server. Actually it is recommended that you create models just to reflect the data needs of your views (called viewModels) and then link those to you domain model if need be. So I suggest you create a model and pass the string from view to controller and vice versa easily. Then you can use @Kamil's suggestion of the TextArea helper to display the model.

Comments

0

check this out:

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        //do your text magix
        return View("Index", model: "your text here");
    }
}

View:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<String>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <title>Index</title>
</head>
<body>
    <div>
        <label for="p1s1">Sentence 1:</label>
        <%= Html.TextArea("p1s1", Model, new { @class = "textbox" })%>
    </div>
</body>
</html>

Hope this helps. Otherwise, let me now. Regards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.