简体   繁体   English

将参数从HTML页面的View页面传递给控制器

[英]passing parameters from HTML form of View page to the controller

i am working in MVC. 我在MVC工作。 I have a View say view1 which consist of a HTML Form as given below: 我有一个View say view1,它包含一个HTML表单,如下所示:

    <form action='/Exercise/Exercise4_SubmitForm' method="post">
    Name: <input type="text" name="name" /><br />
    Emp: <input type="text" name="id" /><br />
    DateofBirth: <input type="text" name="Dateofbirth" /><br />
    Age: <input type="text" name="age" /><br />
    .
    so on
    .
    .
    <input type="submit" name="submit" value="Save" />
    </form>

I have 17 textboxes from which i have mentioned only 4 above. 我有17个文本框,我之前只提到了4个。

Now mvc action method must be 现在必须使用mvc动作方法

    public ActionResult Exercise4(string name, string code, string age, string dateofBirth,...)
    {
      //my code here
    }

My question is that Is there any way by which i dont have to specify each 17 parameters in my mvc action method as given above. 我的问题是,我有没有办法在上面给出的mvc动作方法中指定每个17个参数。 Because it will be possible to have 70 parameter next time then specifying each parameter is very tedious work. 因为下次有可能有70个参数然后指定每个参数是非常繁琐的工作。 "I dont want to use HTML Helpers." “我不想使用HTML Helpers。”

Pls help me!!! 请帮助我!!!

Create a Model 创建一个模型

public class YourModel
{
    public string name {get;set;}
    public string code {get;set;}
    public string age {get;set;}
    public string date {get;set;}
    .
    .
    .
}

Then, in the top of the view put 然后,在视图的顶部放

@model Namespace.YourModel

After that switch all your inputs with 之后切换所有输入

@Html.EditorFor(m => m.name)
@Html.EditorFor(m => m.code)
@Html.EditorFor(m => m.age)
@Html.EditorFor(m => m.date)

If your unsure on how to do this last part, create a new View and check the Create a strongly-typed view and choose your model out of the combobox. 如果您不确定如何执行此最后一部分,请创建一个新视图并选中创建强类型视图并从组合框中选择您的模型。 Select also a Scaffold Template accordingly to what you need. 根据您的需要选择脚手架模板。 Is that a form to insert, edit, delete, list.... 这是一个插入,编辑,删除,列表....

The action that receives the Post will recieve your model instead 接收邮件的操作将接收您的模型

public ActionResult Exercise4(YourModel model)
    {
      //my code here
    }

If I were you I'd look into: http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-5 first 如果我是你,我会先看看: http//www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-5

The fact is that the fields in your form would then have to be a part of a Model (your're using MVC right?) and then the post will contain your Model. 事实是,您的表单中的字段必须是模型的一部分(您正在使用MVC吗?)然后帖子将包含您的模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM