从文本框在视图参数传递给在ASP.Net MVC2控制器 [英] Passing Parameters from textboxes in a view to a controller in ASP.Net MVC2

查看:310
本文介绍了从文本框在视图参数传递给在ASP.Net MVC2控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过构建一个小样本的网站,该网站,除其他功能,为用户提供了一个联系我们页面尝试ASP.Net MVC2。我们的想法是允许用户输入他们的姓名,电子邮件地址,邮件主题和邮件。要发送用户点击ActionLink的消息。这是视图:

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(); %>

我们的想法是,一​​旦ActionLink的被点击控制器中的方法被调用到其中的用户名,电子邮件地址,主题和邮件将被传递。这是在控制器中的方法:

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?

感谢您,

莫里斯

推荐答案

操作链接是不会触发一个HTTP POST也不会传递表单字段的值,只是一个HTTP GET并且没有经过任何表单数据 - 理想情况下你会使用输入提交按钮后的数据。可以肯定的是,它是很好的做法,导致任何请求创建/更新数据,应通过一个HTTP POST完成。

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.

提交按钮只会像。

<input type="submit" value="Send" />

您然后让访问表单数据的几种方法首先你可以使用的FormCollection访问数据

You then have several ways of accessing the form data firstly you could use a FormCollection to access the data

[HttpPost]
public ActionResult SendEmail(FormCollection collection)
{
    string email = collection["txtEmail"];
    ...
}

其次,你可以使用方法参数,并依靠模型约束力,但你必须确保字段名称相匹配的参数名称,这样

Secondly you could use the method parameters and rely on model binding, but you must make sure field names match the parameter name so

<%: Html.TextBox("txtEmail", "", new { style = "width:100%" })%>

...需要...

...would require...

[HttpPost]
public ActionResult SendEmail(string txtEmail)
{
    ...
}

如果这种形式不被发布到相同的动作,多数民众赞成返回视图,那么你也只需要修改你开始的表单标签,理想的你应该使用'使用'用它。所以,你会得到:

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:

<% using (Html.BeginForm("SendEmail", "<controller-name>"))
   { %>
.... form fields in here ...
<input type="submit" value="Send" />
<% } %>

如果该键不适合你的设计,你可以使用这样的:

If the button isn't suitable for your design you could use something like:

<input type="image" src="<%: Url.Content("~/Content/images/myimage.gif") %>" value="Send" />

此将具有相同的效果。要从标签触发后,虽然你需要看使用JavaScript,我不记得确切的语法,但不及手,我认为,如果你使用你会找什么东西像jQuery(形成一个单一的形式仅页)

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)

<a href="#" click="$('form').submit(); return false;">Send</a>

但随后创建于JavaScript的依赖,其中的真的是你应该尝试有你的网站的优雅降级,因此它可以通过禁用了javascript游客使用。有实现这一目标,同时满足设计要求,但可以大举进军客户端code这可能是你想要什么,你的样品之外的也许更先进的方式。

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.

这篇关于从文本框在视图参数传递给在ASP.Net MVC2控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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