ASP. NET MVC Light Tutorial Step by Step 5--first form

Source: Internet
Author: User

In the previous section we put the message list in the index view, it is time to add a message form, so that users add their own messages.

First, add an action method named "Write" in HomeController.

         Public actionresult Write ()        {            return  View ();        }

The Write action method is nothing to explain, then add the corresponding write view.

We still use an empty view template. Add the following HTML code to the view.

<Body>    <H1>MVC Message Board</H1>    <formAction= "/home/save"Method= "POST">        <label for= "nickname">Nickname</label>        <inputname= "nickname"type= "text" />        <BR/><BR/>        <label for= "Content">Content</label>        <textareaname= "Content"cols= " the"rows= "5" ></textarea>        <BR/><BR/>        <inputtype= "Submit"value= "Submit" />    </form></Body>

Now we can browse this page, click Run, smooth words can see the following page form.

Familiar with the HTML friends will not be unfamiliar with this code, a bit of ASP or PHP experience will feel familiar and kind. Note that the form Action property value is "/home/save" and does not appear to be a specific file. In fact, save is a HomeController action method, but this method does not exist, and now we will add this method.

Add the Save action method to the HomeController.

         Public void Save ()        {            messages. ADD (new Message {nickname = request["nickname"], Content = request["  content"], ReleaseDate = DateTime.Now});            Response.Redirect ("/home/index");        }

request["nickname"] to get the value of the input control with the Name property "nickname" in the form, if the form is in Get mode, should be obtained through request.querystring, and if the form is post, apply Request.Form to get the corresponding value. You can also express these two ways directly with request, as shown in the code. If you have previous programming experience with a certain web background script, such as asp/asp.net and so on, then the current code is no different than it used to be. Note that the Save method will redirect the Web page to index.

After the Save method is added, we can start running. If the index view content is displayed at the beginning of the page, then manually add "/home/write" to the address bar, just like this.

Add some content to the page.

Click Submit to see our first message.

ASP. NET MVC Light Tutorial Step by Step 5--first form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.