将参数从视图中的文本框传递到 ASP.Net MVC2 中的控制器
我正在通过构建一个小型示例网站来尝试 ASP.Net MVC2,该网站除其他功能外还为用户提供了“联系我们”页面。这个想法是允许用户输入他们的姓名、电子邮件地址、消息主题和消息。要发送消息,用户单击 ActionLink。这是视图:
<% Html.BeginForm(); %>
<div>
<%: Html.Label("Name")%>
<br />
<%: Html.TextBox("txtName", "",new { style = "width:100%" })%>
<br />
<%: Html.Label("Email address")%>
<br />
<%: Html.TextBox("txtEmail", "", new { style = "width:100%" })%>
<br />
<%: Html.Label("Subject")%>
<br />
<%: Html.TextBox("txtSubject", "", new { style = "width:100%" })%>
<br />
<%: Html.Label("Message")%>
<br />
<%: Html.TextBox("txtMessage", "", new { style = "width:100%" })%>
</div>
<div style='text-align: right;'>
<%:
Html.ActionLink("Send", "SentEmail", new { name = Html.g, sender = "txtEmail", subject = "txtSubject", message="txtMessage" })
%>
</div>
<% Html.EndForm(); %>
这个想法是,一旦单击 ActionLink,就会调用控制器中的一个方法,用户名、电子邮件地址、主题和消息将传递到该方法中。这是控制器中的方法:
public ActionResult SentEmail(string name, string sender, string subject, string message)
{
//Send email here and then display message contents to user.
ViewData["Name"] = name;
ViewData["Message"] = message;
ViewData["ThankyouMessage"] = "Thank you for contacting us. We will be in touch as soon as possible.";
return View();
}
但是...当我单击链接时,传递到该方法中的值为空。我尝试创建一条路线来执行此操作,但它也不起作用。我应该使用其他方法吗?
谢谢你,
莫里斯
I am trying out ASP.Net MVC2 by building a small sample website which, amongst other features provides the user with a 'Contact Us' page. The idea is to allow a user to enter their name, email address, message subject and message. To send the message the user clicks on an ActionLink. This is the view:
<% Html.BeginForm(); %>
<div>
<%: Html.Label("Name")%>
<br />
<%: Html.TextBox("txtName", "",new { style = "width:100%" })%>
<br />
<%: Html.Label("Email address")%>
<br />
<%: Html.TextBox("txtEmail", "", new { style = "width:100%" })%>
<br />
<%: Html.Label("Subject")%>
<br />
<%: Html.TextBox("txtSubject", "", new { style = "width:100%" })%>
<br />
<%: Html.Label("Message")%>
<br />
<%: Html.TextBox("txtMessage", "", new { style = "width:100%" })%>
</div>
<div style='text-align: right;'>
<%:
Html.ActionLink("Send", "SentEmail", new { name = Html.g, sender = "txtEmail", subject = "txtSubject", message="txtMessage" })
%>
</div>
<% Html.EndForm(); %>
The idea is once the ActionLink has been clicked a method in the controller is called into which the username, email address, subject and message will be passed. This is the method in the controller:
public ActionResult SentEmail(string name, string sender, string subject, string message)
{
//Send email here and then display message contents to user.
ViewData["Name"] = name;
ViewData["Message"] = message;
ViewData["ThankyouMessage"] = "Thank you for contacting us. We will be in touch as soon as possible.";
return View();
}
However... when I click the link the values which are passed into the method are null. I have tried creating a route to do this but it doesn't work either. Should I be using another method?
Thank you,
Morris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,实现你想要的比在你的示例中更容易。从未听说过模型类、模型绑定器和强类型视图?这里是
Model 类
然后在您的控制器中您应该有两个操作:第一个操作显示具有默认值的表单,第二个操作接收包含用户放置的数据的表单。这两个操作完全映射到 HttpGet 和 HttPost 动词。
要使用它,您的视图应该强类型化为
ContactUsModel
类,这就是所谓的 ModelBinder 的魔力。请在此处阅读有关 MVC 的更多信息。
Actually to achieve what you want to is easier than in your sample. Never heard about Model classes, Model Binder and strong typed views? Here thery are
Model class
Then in your controller you should have two action: the first that show the form with default values and the second that receive the form with the data placed by the user. These two actions maps exactly to the HttpGet and HttPost verbs.
To use this your view shal be strong typed to the
ContactUsModel
classthe magic of everything this is called ModelBinder. Please read more and more about MVC here.
操作链接不会触发 http post,也不会传递表单字段的值,只是一个 http get,不会传递任何表单数据 - 理想情况下,您可以使用输入提交按钮来发布数据。可以肯定的是,任何导致创建/更新数据的请求都应该通过 http post 完成,这是一种很好的做法。
提交按钮就像这样。
然后,您可以通过多种方式访问表单数据,首先您可以使用 FormCollection 来访问数据
其次,您可以使用方法参数并依赖模型绑定,但您必须确保字段名称与参数名称匹配,因此
...需要...
如果此表单没有发布到返回视图的同一操作,那么您还需要更改开始表单标记,理想情况下您也应该使用“using”。所以你会得到:
如果按钮不适合你的设计,你可以使用类似的东西:
这会产生相同的效果。要从 a 标签触发帖子,虽然你需要使用 javascript,我不记得确切的语法,但我想如果你使用 jquery,你会看到类似的东西:(形成一个单一的表单但
随后您创建了对 javascript 的依赖关系,实际上您应该尝试让您的网站正常降级,以便禁用 javascript 的访问者可以使用它。也许有更先进的方法可以在满足设计要求的同时实现这一点,但这可能会大量进入客户端代码,这可能超出了您对示例的需求。
The action link isn't going to trigger a http post nor will it pass in the values of your form fields, just a http get and not passing through any form data - ideally you'd use an input submit button to post the data. What is certain is that it is good practise that any request that causes creating/updating of data should be done via a http post.
Submit button would just be like.
You then have several ways of accessing the form data firstly you could use a FormCollection to access the data
Secondly you could use the method parameters and rely on model binding, but you must make sure field names match the parameter name so
...would require...
If this form isn't being posted to the same action thats return the view then you'd also need to change your begin form tag, ideal you should use 'using' with it as well. So you'd get:
If the button isn't suitable for your design you could use something like:
This would have the same effect. To trigger a post from an a tag though you'd need to look at using javascript, I can't remember the exact syntax but off hand I think if you used jquery you'd be looking at something like: (form a single form page only)
But then you create a dependency on javascript where as really you should try have your site degrade gracefully so it can be used by visitors with javascript disabled. There are perhaps more advanced way of achieving this whilst meeting design requirements but that can get heavily into client side code which is probably outside of what you want for your sample.