asp.net mvc case Tutorial (based on asp.net mvc beta)-fourth: passing form data

Source: Internet
Author: User

Objective

Through the previous articles, we have been able to easily use ASP.net MVC to render the page and data. However, there is one big problem unresolved: How to work with form data. For example, we will implement the bulletin release function, the user must be in a form page input title, body, and so on, and then submit, and then the form data to be passed to the corresponding place to the business logic component processing.

Under the traditional asp.net, the Model1 mode is used, each ASPX page has a aspx.cs file with the same name, and when the form is submitted, the default data is submitted to a method in this Aspx.cs file with the same name. However, in asp.net mvc, this method is not available because we can no longer use the code file with the same name to process the ASPX commit request (but this does not mean that the code file with the same name will not be used, in fact, it is still executed, but we do not advocate dealing with any logic in it). , however, it is sometimes used to perform some initialization operations. ), so what should be done? Not much, we illustrate the problem by example.

Below we step-by-step to complete the "MVC Bulletin Release System" Bulletin release function, and so on to finish this function, the above problem is clear.

First, modify an error ...

Here, first of all, like to apologize, because in the first article, I made a mistake. is to have one less attribute in the entity class announceinfo of the bulletin. Now, we add a property called Cateogry in Announceinfo, which is an int, which indicates which category the bulletin belongs to.

I am very sorry for this mistake.

Create an Input information page

Below, officially start our work. First, I want to create a page that lets the user enter the announcement information. And we know that in asp.net mvc you can't directly request an ASPX file, and any requests go through controller, so we first create a new controller class under the Controllers directory named Announcecontroller. Delete the automatically generated index method, and create a new action method named release, as follows.

AnnounceController.cs:

1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.Mvc;
6using System.Web.Mvc.Ajax;
7using MVCDemo.Models;
8using MVCDemo.Models.Interfaces;
9using MVCDemo.Models.Entities;
10
11namespace MVCDemo.Controllers
12{
13  public class AnnounceController : Controller
14  {
15    public ActionResult Release()
16    {
17      ICategoryService cServ = ServiceBuilder.BuildCategoryService();
18      List<CategoryInfo> categories = cServ.GetAll();
19      ViewData["Categories"] = new SelectList(categories, "ID", "Name");
20       return View("Release");
21    }
22  }
23}

This is the action method to render the form page and see what it does: it first takes out all the categories and then converts them to the SelectList type into the ViewData and finally renders the release view.

Why do I have to remove all categories? Because we want to have a Drop-down list box that lists all the bulletin names in the announcement, so that the user can choose which category to publish the bulletin to. SelectList is the type of asp.net mvc that is used to bind to a drop-down list. It has a lot of overloaded constructs, where I use three parameters, which represent the enumeration that generated the data, the field name bound to value, and the field name bound to the list name. Here, all the taxonomy entity collections are bound to the Drop-down list, and the ID property is the value, and the Name property acts as the names displayed in the list box.

If we don't need a drop-down list box to display all the categories, then the release method needs just one line to return View ("release");

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.