将参数从视图中的文本框传递到 ASP.Net MVC2 中的控制器

发布于 2024-10-04 15:37:34 字数 1612 浏览 3 评论 0原文

我正在通过构建一个小型示例网站来尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独患者 2024-10-11 15:37:35

实际上,实现你想要的比在你的示例中更容易。从未听说过模型类、模型绑定器和强类型视图?这里是

Model 类

public class ContactUsModel
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Subject { get; set; }
    public string Message { get; set; }
}

然后在您的控制器中您应该有两个操作:第一个操作显示具有默认值的表单,第二个操作接收包含用户放置的数据的表单。这两个操作完全映射到 HttpGet 和 HttPost 动词。

[HttpGet]
public virtual ActionResult ContactUs() {
    ContactUsModel model = new ContactUsModel();
    return View(model);
}


[HttpPost]
public virtual ActionResult ContactUs( ContactUsModel model ) {
    //e.g. Save the contact request to database
}

要使用它,您的视图应该强类型化为 ContactUsModel 类,

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ContactUsModel>" %>

<% using (Html.BeginForm()) { %>
    <div>
        <%: Html.LabelFor(model => model.Name) %><br />
        <%: Html.TextBoxFor(model => model.Name, new { style = "width:100%" })%>
    </div>
    <div>
        <%: Html.LabelFor(model => model.Email) %><br />
        <%: Html.TextBoxFor(model => model.EMail, new { style = "width:100%" })%>
    </div>
    <div>
        <%: Html.LabelFor(model => model.Subject) %><br />
        <%: Html.TextBoxFor(model => model.Subject, new { style = "width:100%" })%>
    </div>
    <div>
        <%: Html.LabelFor(model => model.Message) %><br />
        <%: Html.TextAreaFor(model => model.Message, new { style = "width:100%" })%>
    </div>
    <div>
        <input type="submit" value="Save" />
    </div>
<% } %>

这就是所谓的 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

public class ContactUsModel
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Subject { get; set; }
    public string Message { get; set; }
}

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.

[HttpGet]
public virtual ActionResult ContactUs() {
    ContactUsModel model = new ContactUsModel();
    return View(model);
}


[HttpPost]
public virtual ActionResult ContactUs( ContactUsModel model ) {
    //e.g. Save the contact request to database
}

To use this your view shal be strong typed to the ContactUsModel class

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ContactUsModel>" %>

<% using (Html.BeginForm()) { %>
    <div>
        <%: Html.LabelFor(model => model.Name) %><br />
        <%: Html.TextBoxFor(model => model.Name, new { style = "width:100%" })%>
    </div>
    <div>
        <%: Html.LabelFor(model => model.Email) %><br />
        <%: Html.TextBoxFor(model => model.EMail, new { style = "width:100%" })%>
    </div>
    <div>
        <%: Html.LabelFor(model => model.Subject) %><br />
        <%: Html.TextBoxFor(model => model.Subject, new { style = "width:100%" })%>
    </div>
    <div>
        <%: Html.LabelFor(model => model.Message) %><br />
        <%: Html.TextAreaFor(model => model.Message, new { style = "width:100%" })%>
    </div>
    <div>
        <input type="submit" value="Save" />
    </div>
<% } %>

the magic of everything this is called ModelBinder. Please read more and more about MVC here.

舞袖。长 2024-10-11 15:37:35

操作链接不会触发 http post,也不会传递表单字段的值,只是一个 http get,不会传递任何表单数据 - 理想情况下,您可以使用输入提交按钮来发布数据。可以肯定的是,任何导致创建/更新数据的请求都应该通过 http post 完成,这是一种很好的做法。

提交按钮就像这样。

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

然后,您可以通过多种方式访问​​表单数据,首先您可以使用 FormCollection 来访问数据

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

其次,您可以使用方法参数并依赖模型绑定,但您必须确保字段名称与参数名称匹配,因此

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

...需要...

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

如果此表单没有发布到返回视图的同一操作,那么您还需要更改开始表单标记,理想情况下您也应该使用“using”。所以你会得到:

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

如果按钮不适合你的设计,你可以使用类似的东西:

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

这会产生相同的效果。要从 a 标签触发帖子,虽然你需要使用 javascript,我不记得确切的语法,但我想如果你使用 jquery,你会看到类似的东西:(形成一个单一的表单但

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

随后您创建了对 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.

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

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

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>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文