ASP.NET MVC 3剃须刀:传递数据视图控制器 [英] ASP.NET MVC 3 Razor: Passing Data from View to Controller

查看:155
本文介绍了ASP.NET MVC 3剃须刀:传递数据视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是全新的万物.NET。我有一个HTML表单一个非常基本的网页。我想的onsubmit从视图控制器发送表单数据。我已经看到了类似的帖子这个,但没有涉及到有新的杂交剃刀​​语法的答案。我该怎么做的onsubmit,以及如何从控制器访问数据?谢谢!

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!!

推荐答案

你可以用你的视图控件要在Html.Beginform传递。

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

例如:

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

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

}

当Submit按钮是pressed那Beginform内的一切都将提交给ControllerName控制你的ActionMethodName的方法。

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

在控制器端您可以访问所有从这样的观点接收到的数据:

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.Get(嗒嗒)

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")

您还可以传递参数给你的控制器的情况下直接使用的FormCollection发送整个页面:

You can also pass parameters to your controllers directly without sending the entire page with 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;

}

或者,你可以结合这两种方法,并通过与沿线的FormCollection具体参数。这是给你的。

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

希望它帮助。

编辑:当我在写其他用户提到你一些有用的链接,以及。一起来看看。

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

这篇关于ASP.NET MVC 3剃须刀:传递数据视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆