3

I'm trying to learn some ServiceStack stuff. For now, I've succesfully completed this tutorial (almost completed): http://www.ienablemuch.com/2012/12/self-hosting-servicestack-serving.html

The next step I want to perform is creating html form with submit button and capture data entered, for example into variables in C# code.

I know how to create html form inside cshtml file, but I have no idea how to caputre entered POST data in this case - with ServiceStack. Can you provide any tips/code samples.

Also, there is something I don't understand in this tutorial:

19 . To use strongly-typed model for your razor page, use inherits directive. Create a model first, it must be public and its Copy to Output Directory property must be set to Copy if newer too

What is this "Model" exactly and how to create it? And, section 20, in cshtml code:

@inherits ViewPage

So, what is "ViewPage" and how exactly this statement works?

user1209216
  • 7,404
  • 12
  • 60
  • 123
  • 1
    Razor will compile CS code in your page into a class, with "inherits" you say which base class it has to use (ViewPage is the base class for Razor's HTML pages without model). If you make it typed (ViewPage) then it _should_ (I didn't try ServiceStack) capture posted data to your model without anything else to do. – Adriano Repetti Apr 10 '13 at 12:01
  • Ok, but what is "Model" in this case? Tutorial says I should create my model first but I'm not sure what does it mean? If I only create class and put it into VievPage<>, there is compile cshtml error saying this class is not found (I mean ServiceStack output, not Visual Studio compilation error). – user1209216 Apr 10 '13 at 12:04
  • It's the class with data passed from the controller to the view. Probably in a post method you're more interested to that data in the controller than in the view. class should be public and accessible (same output directory). – Adriano Repetti Apr 10 '13 at 12:30

1 Answers1

2

Solved. So, what is needed to be done:

  • create public class with public properties inside. Properties names should match post/get values in my form

  • add this: @inherits ViewPage<BillionaireServiceStackRazorSelfHosting.MyClass> to cshtml

After form submit, corresponding properties are set. I don't know if this is good way (please correct me if it doesn't), but I've added some code to "set" handler inside MyClass property, instead of adding code to cshtml that uses captured values.

Everything seems to be working as I wanted to.

user1209216
  • 7,404
  • 12
  • 60
  • 123