C# ASP.NET MVC 3 Razor:将数据从视图传递到控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10956721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:55:49  来源:igfitidea点击:

ASP.NET MVC 3 Razor: Passing Data from View to Controller

c#asp.net-mvcasp.net-mvc-3razor

提问by Kyle M

I'm brand new to all things .NET. I have a very basic web page with an HTML form. I want 'onsubmit' to send the form data from the View to the Controller. I've seen similar posts to this but none have answers involving the new-ish Razor syntax. What do I do with 'onsubmit', and how do I access the data from the Controller? Thanks!!

我对 .NET 的所有事物都是全新的。我有一个带有 HTML 表单的非常基本的网页。我希望“onsubmit”将表单数据从视图发送到控制器。我看过类似的帖子,但没有一个涉及新式 Razor 语法的答案。我如何处理“onsubmit”,以及如何从 Controller 访问数据?谢谢!!

采纳答案by InspiredBy

You can wrap your view controls you want to pass in Html.Beginform.

您可以将要传递的视图控件包装在 Html.Beginform 中。

For example:

例如:

@using (Html.BeginForm("ActionMethodName","ControllerName"))
{
 ... your input, labels, textboxes and other html controls go here

 <input class="button" id="submit" type="submit" value="Submit" />

}

When Submit button is pressed everything inside of of that Beginform will be submitted to your "ActionMethodName" method of the "ControllerName" controller.

当按下提交按钮时,Beginform 内部的所有内容都将提交给“ControllerName”控制器的“ActionMethodName”方法。

ON the controller side you can access all received data from the view like this:

在控制器端,您可以像这样从视图中访问所有接收到的数据:

public ActionResult ActionMethodName(FormCollection collection)
{
 string userName = collection.Get("username-input");

}

collection object above will contain all your input entries that we submitted from the form. You can access them by name just like you would access any array: collection["blah"] or collection.Get("blah")

上面的集合对象将包含我们从表单提交的所有输入条目。您可以像访问任何数组一样按名称访问它们: collection["blah"] 或 collection.Get("blah")

You can also pass parameters to your controllers directly without sending the entire page with FormCollection:

您还可以直接将参数传递给您的控制器,而无需使用 FormCollection 发送整个页面:

@using (Html.BeginForm("ActionMethodName","ControllerName",new {id = param1, name = param2}))
{
 ... your input, labels, textboxes and other html controls go here

 <input class="button" id="submit" type="submit" value="Submit" />

}

public ActionResult ActionMethodName(string id,string name)
{
 string myId = id;
 string myName = name;

}

Or you can combine both of these methods and pass specific parameters along with the Formcollection. It's up to you.

或者,您可以将这两种方法结合起来,并与 Formcollection 一起传递特定参数。由你决定。

Hope it helps.

希望能帮助到你。

edit: while I was writing other users referred you to some helpful links as well. Take a look.

编辑:在我写文章时,其他用户也向您推荐了一些有用的链接。看一看。

回答by Justin

Defining a form in the following way:

通过以下方式定义表单:

@using (Html.BeginForm("ControllerMethod", "ControllerName", FormMethod.Post))

@using (Html.BeginForm("ControllerMethod", "ControllerName", FormMethod.Post))

Will make a call to the method "ControllerMethod" in the controller "ControllerName". In the method you can accept a model, or other data types as inputs. See thistutorial for examples using forms and razor mvc.

将调用控制器“ControllerName”中的方法“ControllerMethod”。在该方法中,您可以接受模型或其他数据类型作为输入。有关使用表单和 razor mvc 的示例,请参阅教程。