Incoming list data to the controller in a strongly typed view

Source: Internet
Author: User

Source of the problem:

While chatting with a technical teacher, the teacher asked me an MVC form submission question and asked: How do I pass an object with a list property to the controller when the form is submitted?

At this point, I was a little bit, used to be like a single attribute of the form submission, did not have a list property of the form submission. Finally spared a lap, or honestly answer the teacher, do not know, did not deal with.

Since the problem has come out, and not yet, this is certainly not possible, go back, start simulating the scene, testing to solve the problem:

1. Prepare two entity classes first

   public class article    {public        string Title {get; set;}        public string Content {get; set;}        Public list<articletype> Types {get; set;}    }   public class ArticleType    {public        string Name {get; set;}        public string Dirc {get; set;}    }

2. Controller and corresponding view

  Public classTestcontroller:controller {//        //GET:/test/         PublicActionResult Index () {viewbag.mes = "prepare test";returnView (); } [HttpPost] Publicactionresult Index (article ent) {if(Ent. types!=NULL&& Ent. Types.count>0) Viewbag.mes = "The number of types under article is:" + ent. Types.count;returnView (); }    }
@model testui.models.article@{viewbag.title = "Index";}<Divstyle= "padding:20px">@ViewBag. Mes<formAction= "Index"Method= "POST">        <P>            <span>Title:</span>@Html. textboxfor (x = x.title)</P>        <P>            <span>Content:</span>@Html. textareafor (x = x.content)</P>@for (var i = 0; I<2; i++) {<p>                 <span>Type @ (i):</span><BR>                 <span>Name:</span>@Html. textboxfor (x = x.types[i]. Name)<span>Describe:</span>@Html. textboxfor (x = x.types[i]. DIRC)</P>        }               <P>            <Buttontype= "Submit">Submit</Button>        </P>    </form></Div>

3. Result Run diagram

Not before the submission

After submission

The results are clear, and this is a solution that is valid for both the post and get commits that have been tested.

Reasons for inference to be successful:

It is also due to the parsing mechanism of MVC's strongly typed view.

The essence of the form submission is the name of the form label, which is the value of the form label as the parameter.

@for (var i = 0; i < 2; i++) {    <p>       <span> type @ (i):</span><br>       <span> Name: </ Span>       @Html. textboxfor (x = x.types[i]. Name)       <span> description:</span>       @Html. textboxfor (x = x.types[i]. DIRC)    </p> }

X.types[i] Data values are added to the list.

Since we have not studied the underlying principles of MVC, we can only make the above inference, if there are errors, please correct me.

Incoming list data to the controller in a strongly typed view

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.